NanoLux (Device) 3.0
Codebase for the open-source AudioLux device.
Loading...
Searching...
No Matches
main.ino File Reference
#include <ArduinoJson.h>
#include <ArduinoJson.hpp>
#include <FastLED.h>
#include <Arduino.h>
#include "patterns.h"
#include "nanolux_types.h"
#include "nanolux_util.h"
#include "core_analysis.h"
#include "ext_analysis.h"
#include "storage.h"
#include "globals.h"
#include <AiEsp32RotaryEncoder.h>
#include "WebServer.h"
#include "api.h"

Functions

void setup ()
 Sets up various objects needed by the device.
 
void loop ()
 Runs the main program loop.
 
void audio_analysis ()
 Performs audio analysis by running audio_analysis.cpp's audio processing functions.
 
void calculate_layering (CRGB *a, CRGB *b, CRGB *out, int length, uint8_t blur)
 Layers two CRGB arrays together and places the result in another array.
 
void update_web_server ()
 Checks if the web server has new data, and marks settings as dirty if required.
 
void scale_crgb_array (CRGB *arr, uint8_t len, uint8_t factor)
 Scales all LEDs in a CRGB array by (factor)/255.
 
void reverse_buffer (CRGB *buf, uint8_t len)
 Reverses the LED buffer supplied given a length.
 
void unfold_buffer (CRGB *buf, uint8_t len, bool even)
 Unfolds the buffer by mirroring it across the right side.
 
void process_pattern (Pattern_Data *p, Strip_Buffer *buf, uint8_t len)
 Runs a specified pattern and performs postprocessing effects on it.
 
void run_strip_splitting ()
 Runs the strip splitting LED strip mode.
 
void run_pattern_layering ()
 Runs the pattern layering output mode.
 
void print_buffer (CRGB *buf, uint8_t len)
 Prints a buffer to serial.
 
void update_hardware ()
 Processes all calls to hardware and manages the results of those calls.
 

Variables

CRGB output_buffer [MAX_LEDS]
 Unprocessed output buffer.
 
CRGB smoothed_output [MAX_LEDS]
 Postprocessed output buffer.
 
bool button_pressed = false
 Contains the current state of the button (true is pressed).
 
double peak = 0.
 Contains the peak frequency detected by the FFT.
 
uint8_t fHue = 0
 Contains the "base" hue, calculated from the peak frequency.
 
double volume = 0.
 Contains the current volume detected by the FFT.
 
uint8_t vbrightness = 0
 Contains the "base" brightness value, calculated from the current volume.
 
volatile bool pattern_changed = false
 Updated to "true" when the web server changes significant pattern settings.
 
Strip_Data loaded_patterns
 The current strip configuration being ran by the device.
 
Strip_Data saved_patterns [NUM_SAVES]
 All patterns and strip configuration currently loaded from storage.
 
Config_Data config
 The currently-loaded device config.
 
Strip_Buffer histories [PATTERN_LIMIT]
 History of all currently-running patterns.
 
Pattern mainPatterns []
 The current list of patterns, externed from globals.h.
 
volatile bool manual_control_enabled = false
 MANUAL CONTROL VARIABLES.
 
Strip_Buffer manual_strip_buffer
 
Pattern_Data manual_pattern
 
bool lastEncoderBtnPressed = false
 Stores the last state of the rotary encoder button.
 

Detailed Description

This file is the main driver of the NanoLux codebase.

Function Documentation

◆ audio_analysis()

void audio_analysis ( )

Performs audio analysis by running audio_analysis.cpp's audio processing functions.

If the macro SHOW_TIMINGS is defined, it will print out the amount of time audio processing takes via serial.

◆ calculate_layering()

void calculate_layering ( CRGB * a,
CRGB * b,
CRGB * out,
int length,
uint8_t blur )

Layers two CRGB arrays together and places the result in another array.

Parameters
aThe first array to layer.
bThe second array to layer.
outThe array to output to.
lengthThe number of pixels to layer.
blurThe ratio between the first and second array in the output. 0-255.

◆ loop()

void loop ( )

Runs the main program loop.

Carries out functions related to timing and updating the LED strip.

◆ print_buffer()

void print_buffer ( CRGB * buf,
uint8_t len )

Prints a buffer to serial.

Parameters
bufThe CRGB buffer to print.
lenThe number of RGB values to print.

◆ process_pattern()

void process_pattern ( Pattern_Data * p,
Strip_Buffer * buf,
uint8_t len )

Runs a specified pattern and performs postprocessing effects on it.

Parameters
pThe pattern to run.
bufThe buffer to read/write to/from.
lenHow many pixels this pattern can run on.

◆ reverse_buffer()

void reverse_buffer ( CRGB * buf,
uint8_t len )

Reverses the LED buffer supplied given a length.

Parameters
bufThe LED buffer to reverse.
lenThe length of the buffer to reverse.

◆ run_pattern_layering()

void run_pattern_layering ( )

Runs the pattern layering output mode.

This function layers two patterns onto each other, covering the entire length of the LED strip.

Each pattern buffer is moved into a temp buffer, which scales the brightness of each pattern according to user settings.

Then, the patterns are merged into the unsmoothed buffer, which then is combined with the smoothed buffer.

◆ run_strip_splitting()

void run_strip_splitting ( )

Runs the strip splitting LED strip mode.

This function allocates a number of LEDs per pattern, then moves LED data from the pattern's history buffer to the unsmoothed output buffer.

Once the data is in the unsmoothed buffer, the buffer is "smoothed" into the output smoothed buffer.

◆ scale_crgb_array()

void scale_crgb_array ( CRGB * arr,
uint8_t len,
uint8_t factor )

Scales all LEDs in a CRGB array by (factor)/255.

Parameters
arrThe array to scale
lenThe number of LEDs to scale
factorThe factor to scale by. Divided by 255.

◆ setup()

void setup ( )

Sets up various objects needed by the device.

Initalizes the button pin, serial, pattern index, the FastLED object, and all stored saves.

◆ unfold_buffer()

void unfold_buffer ( CRGB * buf,
uint8_t len,
bool even )

Unfolds the buffer by mirroring it across the right side.

Parameters
bufThe buffer to unfold.
lenThe length to unfold.
evenIf the LED strip length is even/odd. When odd, duplicates the middle pixel a second time to fill space.

◆ update_hardware()

void update_hardware ( )

Processes all calls to hardware and manages the results of those calls.

This function includes an ifdef else directive which checks for a macro called VERSION_2_HARDWARE. This macro is defined or commented out at the start of nanolux_util.h. If your hardware has a rotary encoder and button combo, ensure this macro is not commented.

◆ update_web_server()

void update_web_server ( )

Checks if the web server has new data, and marks settings as dirty if required.

Some settings are updated inside a more constrained context without enough stack space for a "disk" operation so we defer the save to here.

Variable Documentation

◆ button_pressed

bool button_pressed = false

Contains the current state of the button (true is pressed).

Checks if the device button is pressed, defined in main.ino.

◆ config

Config_Data config

The currently-loaded device config.

The current config of the device, defined in main.ino. Used to check if serial printing is allowed.

◆ loaded_patterns

Strip_Data loaded_patterns

The current strip configuration being ran by the device.

The currently strip configuration, externed from main.ino.

◆ peak

double peak = 0.

Contains the peak frequency detected by the FFT.

Global variable used to store preak audio frequency.

◆ saved_patterns

Strip_Data saved_patterns[NUM_SAVES]

All patterns and strip configuration currently loaded from storage.

The array for storing saved strip configurations, externed from main.ino.

◆ volume

double volume = 0.

Contains the current volume detected by the FFT.

Global variable used to access the current volume.