NanoLux (Device) 3.0
Codebase for the open-source AudioLux device.
Loading...
Searching...
No Matches
nanolux_util.cpp File Reference
#include "cmath"
#include <FastLED.h>
#include <Arduino.h>
#include "arduinoFFT.h"
#include "patterns.h"
#include "nanolux_types.h"
#include "nanolux_util.h"
#include "storage.h"
#include <AiEsp32RotaryEncoder.h>

Functions

void nanolux_serial_print (char *msg)
 Prints a character array message if allowed to.
 
void bound_byte (uint8_t *val, int lower, int upper)
 Bounds a byte between an upper and a lower value.
 
int remap (double x, double oMin, double oMax, double nMin, double nMax)
 Remaps a value in one range to another range.
 
int largest (double arr[], int n)
 Returns the index of the largest array element.
 
void reset_button_state ()
 Resets the button state to false.
 
void IRAM_ATTR buttonISR ()
 A debounced interrupt to set the button state.
 
void led_on_forever ()
 Causes the LED to turn on/off forever.
 
void process_reset_button (int button_value)
 If the hardware button is held for RESET_TIME, clear the list of saves and call led_on_forever().
 
void begin_loop_timer (long ms)
 Logs the current program loop start time and the expected loop end time.
 
long timer_overrun ()
 Returns the amount of time since expected loop termination.
 
void IRAM_ATTR readEncoderISR ()
 Processes the interrupt for the rotary encoder on 2.0 hardware.
 
void setup_rotary_encoder ()
 Performs initial setup for the rotary encoder.
 
int calculate_pattern_index ()
 Calculates the pattern index the rotary encoder currently corresponds to.
 
bool isEncoderButtonPressed ()
 Returns the current state of the rotary encoder button.
 

Variables

Config_Data config
 The currently-loaded device config.
 
bool button_pressed
 Contains the current state of the button (true is pressed).
 
int NUM_PATTERNS
 
unsigned long start_millis = NULL
 
long loop_start_time = 0
 Holds the start time of the current program loop.
 
long loop_end_time = 0
 Holds the expected end time of the current program loop.
 
AiEsp32RotaryEncoder rotaryEncoder
 Defines the rotary encoder object for the 2.0 boards.
 

Detailed Description

This file contains functions that interface with the phyiscal on-board hardware, such as encoders or buttons.

This file also contains helper functions that don't make sense to define elsewhere.

Function Documentation

◆ begin_loop_timer()

void begin_loop_timer ( long ms)

Logs the current program loop start time and the expected loop end time.

Parameters
msThe user-specified loop time.

◆ bound_byte()

void bound_byte ( uint8_t * val,
int lower,
int upper )

Bounds a byte between an upper and a lower value.

Parameters
valThe pointer to the value to modify.
lowerThe lower value the value can be.
upperThe upper value the value can be.

◆ buttonISR()

void IRAM_ATTR buttonISR ( )

A debounced interrupt to set the button state.

If the device button is pressed, wait 5 milliseconds. If the button pin still reads a press, set button_pressed to True.

◆ calculate_pattern_index()

int calculate_pattern_index ( )

Calculates the pattern index the rotary encoder currently corresponds to.

Returns
The pattern index the encoder is set to.

◆ isEncoderButtonPressed()

bool isEncoderButtonPressed ( )

Returns the current state of the rotary encoder button.

Returns
True if pressed, false if not.

◆ largest()

int largest ( double arr[],
int n )

Returns the index of the largest array element.

Parameters
arrThe array to iterate through.
nThe number of elements to check.
Returns
The index of the largest element in arr.

◆ led_on_forever()

void led_on_forever ( )

Causes the LED to turn on/off forever.

This function is an infinite loop that never terminates. It clears the LED strip, then causes the built-in LED to turn on and off with 1 second states.

◆ nanolux_serial_print()

void nanolux_serial_print ( char * msg)

Prints a character array message if allowed to.

Parameters
msgThe message to print out to serial.

◆ process_reset_button()

void process_reset_button ( int button_value)

If the hardware button is held for RESET_TIME, clear the list of saves and call led_on_forever().

The inital button press sets start_millis to the current device time. If the delta between the current time and start_millis is equal to 1.5 seconds, turn on the LED.

If the delta is more than RESET_TIME, reset the device and call led_on_forever(). This is an infinite loop, which requires a device reset to clear. This is intended behavior.

This function requires constant monitoring of the button, so direct hardware calls are used here.

◆ remap()

int remap ( double x,
double oMin,
double oMax,
double nMin,
double nMax )

Remaps a value in one range to another range.

Parameters
xThe value to remap.
oMinThe old range minimum.
oMaxThe old range maximum.
nMinThe new range minimum.
nMaxThe new range maximum.
Returns
The value mapped to it's new range.

◆ reset_button_state()

void reset_button_state ( )

Resets the button state to false.

If the button state is currently true, print to serial if debug is enabled in the device config.

◆ timer_overrun()

long timer_overrun ( )

Returns the amount of time since expected loop termination.

Returns 0 if the expected end time has not been reached, or a -1 if an error has occured.

If the loop end time has been reached, return the delta between the expected time plus 1.

1 is added to the output time to distinguish between positive and negative 0. This probably should be refactored in the future.

Variable Documentation

◆ button_pressed

bool button_pressed
extern

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
extern

The currently-loaded device config.

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

◆ rotaryEncoder

AiEsp32RotaryEncoder rotaryEncoder
Initial value:
= AiEsp32RotaryEncoder(
ROTARY_ENCODER_A_PIN,
ROTARY_ENCODER_B_PIN,
ROTARY_ENCODER_BUTTON_PIN,
ROTARY_ENCODER_VCC_PIN,
ROTARY_ENCODER_STEPS
)

Defines the rotary encoder object for the 2.0 boards.

◆ start_millis

unsigned long start_millis = NULL

Stores the inital time the button was pressed. Used to determine if the button has been held long enough to reset the device.