Make the controller save all the configurable values to EEPROM and make the OpeNITHMConfig tool load the values from EEPROM and initialize the UI with the current controller values

This commit is contained in:
skogaby
2020-03-20 19:06:44 -05:00
parent 3ba3e3e4c3
commit d88e3c51d1
14 changed files with 233 additions and 84 deletions
+12 -1
View File
@@ -135,7 +135,10 @@ AirSensor::AirSensor(int requiredSamples, int skippedSamples) : thresholds{ 1000
}
}
analogSensitivity = 40;
EEPROM.get(66, analogSensitivity);
if (analogSensitivity == 0)
setAnalogSensitivity(DEFAULT_SENSITIVITY);
}
// Check if all IR sensors are calibrated. If they are, set a flag to not need to re-check it
@@ -225,10 +228,12 @@ float AirSensor::getHandPosition()
uint8_t AirSensor::getSensorReadings()
{
uint8_t reading = 0;
for (int i = 0; i < 6; i++)
{
reading |= ((int)getSensorState(i) << i);
}
return reading;
}
@@ -240,6 +245,12 @@ bool AirSensor::getSensorCalibrated(int i)
void AirSensor::setAnalogSensitivity(uint8_t analogSensitivity)
{
this->analogSensitivity = analogSensitivity;
EEPROM.put(66, analogSensitivity);
}
uint8_t AirSensor::getAnalogSensitivity()
{
return analogSensitivity;
}
void AirSensor::recalibrate()
+3
View File
@@ -12,6 +12,8 @@
#include "PinConfig.h"
#include <EEPROM.h>
#define DEFAULT_SENSITIVITY 40
class AirSensor
{
private:
@@ -40,6 +42,7 @@ class AirSensor
uint8_t getSensorReadings();
bool getSensorCalibrated(int i);
void setAnalogSensitivity(uint8_t analogSensitivity);
uint8_t getAnalogSensitivity();
void recalibrate();
};
+13 -3
View File
@@ -39,7 +39,7 @@ void AutoTouchboard::saveConfig()
EEPROM.put((i * 2) + 32, double_thresholds[i]);
}
EEPROM.put(64, CALIBRATION_FLAG);
EEPROM.put(64, (byte) CALIBRATION_FLAG);
}
void AutoTouchboard::calibrateKeys(bool forceCalibrate = false)
@@ -49,7 +49,7 @@ void AutoTouchboard::calibrateKeys(bool forceCalibrate = false)
// * the calibration flag is not set in EEPROM
bool needsCalibration = forceCalibrate;
int calibrationFlag;
byte calibrationFlag;
EEPROM.get(64, calibrationFlag);
// only check the last 4 keys if we've calibrated at least once
@@ -179,6 +179,12 @@ uint16_t AutoTouchboard::getRawValue(int key)
void AutoTouchboard::setSensitivity(uint8_t sensitivity)
{
this->sensitivity = sensitivity;
EEPROM.put(65, sensitivity);
}
uint8_t AutoTouchboard::getSensitivity()
{
return sensitivity;
}
AutoTouchboard::AutoTouchboard()
@@ -189,7 +195,11 @@ AutoTouchboard::AutoTouchboard()
pinMode(MUX_0, OUTPUT);
pinMode(MUX_1, OUTPUT);
pinMode(MUX_2, OUTPUT);
sensitivity = 76;
EEPROM.get(65, sensitivity);
if (sensitivity == 0)
setSensitivity(DEFAULT_SENSITIVITY);
calibrateKeys();
}
+3 -1
View File
@@ -14,7 +14,8 @@
#define CALIBRATION_SAMPLES 25
#define CALIBRATION_DETECTION_THRESHOLD 55
#define CALIBRATION_FLAG 0xFFFF
#define CALIBRATION_FLAG 0xFF
#define DEFAULT_SENSITIVITY 80
extern CRGB leds[16];
@@ -38,6 +39,7 @@ class AutoTouchboard
uint16_t getRawValue(int key);
void calibrateKeys(bool forceCalibrate = false);
void setSensitivity(uint8_t sensitivity);
uint8_t getSensitivity();
};
#endif
+3 -3
View File
@@ -7,7 +7,7 @@
// *** USER EDITABLE CONFIG OPTIONS ***
// Select which board you're using (ignore if not using Teensy)
#define TEESNY_V1_0 // Version 1.0 (No version number on board)
#define TEENSY_V1_0 // Version 1.0 (No version number on board)
//#define TEENSY_V1_1 // Version 1.1 (v1.1 on board under logo)
// Uncomment this line to force the processor to use Serial Output
@@ -50,9 +50,9 @@
#endif
// Spit out errors for board definitions
#if !defined(TEESNY_V1_0) && !defined(TEENSY_V1_1)
#if !defined(TEENSY_V1_0) && !defined(TEENSY_V1_1)
#error "For Teensy, EITHER v1.0 OR v1.1 must be defined in Config.h"
#elif defined(TEESNY_V1_0) && defined(TEENSY_V1_1)
#elif defined(TEENSY_V1_0) && defined(TEENSY_V1_1)
#error "For Teensy, cannot define BOTH v1.0 AND v1.1 in Config.h"
#endif
#endif
+17 -2
View File
@@ -21,8 +21,8 @@ bool updateLeds = false;
bool useSerialLeds = false;
int serialLightsCounter;
CRGB led_on = CRGB::Purple;
CRGB led_off = CRGB::Yellow;
CRGB led_on;
CRGB led_off;
float lightIntensity[16];
bool activated = true;
@@ -58,6 +58,21 @@ void setup() {
// Initialize the serial LED processor
serialLeds = new SerialLeds();
// Initialize the reactive lighting colors, and if they're not in EEPROM, save them
byte lightsFlag;
EEPROM.get(67, lightsFlag);
if (lightsFlag == LIGHTS_FLAG)
{
serialLeds->loadLights();
}
else
{
led_on = CRGB::Purple;
led_off = CRGB::Yellow;
serialLeds->saveLights();
}
// Initialize air sensor
// Digital mode calibrations in the constructor
// Analog mode will automatically calibrate as it starts being read
+5 -5
View File
@@ -6,7 +6,7 @@
// Multiplexer pin settings
#ifdef TEENSY // Teensy LC
#if defined(TEESNY_V1_0)
#if defined(TEENSY_V1_0)
#define MUX_0 20
#define MUX_1 19
#define MUX_2 18
@@ -23,7 +23,7 @@
// Sensor pin settings
#ifdef TEENSY
#if defined(TEESNY_V1_0)
#if defined(TEENSY_V1_0)
#define LED_0 16
#define LED_1 21
#define LED_2 5
@@ -42,7 +42,7 @@
#define SENSOR_IN A0
#else
#ifdef TEENSY
#if defined(TEESNY_V1_0)
#if defined(TEENSY_V1_0)
#define AIR_SENSOR_0_PIN 10
#define AIR_SENSOR_1_PIN 11
#define AIR_SENSOR_2_PIN 12
@@ -73,7 +73,7 @@
#define SEND 10
#ifdef TEENSY // Teensy LC
#if defined(TEESNY_V1_0)
#if defined(TEENSY_V1_0)
#define TOUCH_0 22
#define TOUCH_1 23
#elif defined(TEENSY_V1_1)
@@ -86,7 +86,7 @@
#define LED_TYPE WS2812B
#define LED_ORDER GRB
#ifdef TEENSY // Teensy LC
#if defined(TEESNY_V1_0)
#if defined(TEENSY_V1_0)
#define RGBPIN 4
#elif defined(TEENSY_V1_1)
#define RGBPIN 11
+19
View File
@@ -3,6 +3,25 @@
SerialLeds::SerialLeds()
{ }
void SerialLeds::saveLights() {
EEPROM.put(67, (byte) LIGHTS_FLAG);
EEPROM.put(68, led_on.r);
EEPROM.put(69, led_on.g);
EEPROM.put(70, led_on.b);
EEPROM.put(71, led_off.r);
EEPROM.put(72, led_off.g);
EEPROM.put(73, led_off.b);
}
void SerialLeds::loadLights() {
EEPROM.get(68, led_on.r);
EEPROM.get(69, led_on.g);
EEPROM.get(70, led_on.b);
EEPROM.get(71, led_off.r);
EEPROM.get(72, led_off.g);
EEPROM.get(73, led_off.b);
}
void SerialLeds::processBulk(uint8_t *buf)
{
if (memcmp(buf + 2, lastBuffer + 2, 98) != 0)
+9
View File
@@ -7,7 +7,14 @@
#include "WProgram.h"
#endif
#include <EEPROM.h>
#include <FastLED.h>
#define LIGHTS_FLAG 0xFF
extern bool updateLeds;
extern CRGB led_on;
extern CRGB led_off;
typedef struct {
uint8_t b;
@@ -22,6 +29,8 @@ class SerialLeds
public:
SerialLeds();
void saveLights();
void loadLights();
void processBulk(uint8_t *buf);
RGBLed getKey(uint8_t key); // Left to Right
+28 -15
View File
@@ -8,32 +8,45 @@ void SerialProcessor::processConfigCommand(uint8_t* buf)
// figure out which command we're running
switch (buf[2])
{
case CMD_CHANGE_LIGHT_MODE:
if (buf[3] == LIGHT_MODE_SERIAL)
{
useSerialLeds = true;
// print the controller info
case CMD_PRINT_INFO:
byte bytes[100];
// whether the air sensor is analog or not
#ifdef IR_SENSOR_ANALOG
bytes[0] = 0x11;
#else
bytes[0] = 0x00;
#endif
// slider on color
bytes[1] = led_on.r;
bytes[2] = led_on.g;
bytes[3] = led_on.b;
// slider off color
bytes[4] = led_off.r;
bytes[5] = led_off.g;
bytes[6] = led_off.b;
// slider sensitivity
bytes[7] = touchboard->getSensitivity();
// air sensitivity
bytes[8] = sensor->getAnalogSensitivity();
// Set LEDs blue for "ready"
for (CRGB& led : leds)
led = CRGB::Blue;
FastLED.show();
}
else if (buf[3] == LIGHT_MODE_REACTIVE)
{
useSerialLeds = false;
}
Serial.write(bytes, 100);
break;
case CMD_CHANGE_ON_COLOR:
led_on.r = buf[3];
led_on.g = buf[4];
led_on.b = buf[5];
serialLeds->saveLights();
break;
case CMD_CHANGE_OFF_COLOR:
led_off.r = buf[3];
led_off.g = buf[4];
led_off.b = buf[5];
serialLeds->saveLights();
break;
case CMD_CALIBRATE_SLIDER:
touchboard->setSensitivity(buf[3]);
+1 -5
View File
@@ -15,16 +15,12 @@
#define CONFIG_FLAG 0xBB
// config command opcodes
#define CMD_CHANGE_LIGHT_MODE 0x11
#define CMD_PRINT_INFO 0x11
#define CMD_CHANGE_ON_COLOR 0x22
#define CMD_CHANGE_OFF_COLOR 0x33
#define CMD_CALIBRATE_SLIDER 0x44
#define CMD_CALIBRATE_AIR_SENSORS 0x55
// light modes for the change light mode command
#define LIGHT_MODE_SERIAL 0x00
#define LIGHT_MODE_REACTIVE 0x01
extern AirSensor *sensor;
extern AutoTouchboard *touchboard;
extern SerialLeds *serialLeds;