mirror of
https://github.com/skogaby/OpeNITHM.git
synced 2026-09-22 22:58:16 +03:00
Fix a pin assignment in the v1.1 PinConfig.h. Remove the Touchboard class and make AutoTouchboard the canonical input method for the slider
This commit is contained in:
@@ -36,7 +36,6 @@ class AutoTouchboard
|
||||
void saveConfig();
|
||||
KeyState update(int key);
|
||||
uint16_t getRawValue(int key);
|
||||
uint16_t getNeutralValue(int key);
|
||||
void calibrateKeys(bool forceCalibrate = false);
|
||||
};
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ KeyState key_states[16];
|
||||
CRGB leds[16];
|
||||
bool updateLeds = false;
|
||||
|
||||
CRGB led_on = 0xFF00FF; // purple
|
||||
CRGB led_off = 0xFFFF00; // yellow
|
||||
CRGB led_on = CRGB::Purple;
|
||||
CRGB led_off = CRGB::Yellow;
|
||||
|
||||
float lightIntensity[16];
|
||||
bool activated = true;
|
||||
@@ -32,8 +32,6 @@ Output *output;
|
||||
|
||||
char command[32];
|
||||
|
||||
// Parse configuration command. For now, a serial terminal is required (like the monitor in Arduino IDE)
|
||||
// Eventually I will make a config tool
|
||||
void parseCommand()
|
||||
{
|
||||
char input1 = Serial.read();
|
||||
@@ -157,11 +155,14 @@ void setup() {
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
for (CRGB& led : leds)
|
||||
led = 0xFF5F00;
|
||||
led = CRGB::Orange;
|
||||
|
||||
FastLED.show();
|
||||
delay(1000);
|
||||
|
||||
for (CRGB& led : leds)
|
||||
led = 0x000000;
|
||||
led = CRGB::Black;
|
||||
|
||||
FastLED.show();
|
||||
delay(1000);
|
||||
}
|
||||
@@ -183,7 +184,8 @@ void setup() {
|
||||
|
||||
// Display the number of air sensors that were calibrated
|
||||
for (CRGB& led : leds)
|
||||
led = 0x000000;
|
||||
led = CFGB::Black;
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (sensor->getSensorCalibrated(i))
|
||||
@@ -191,12 +193,14 @@ void setup() {
|
||||
else
|
||||
leds[i] = CRGB::Red;
|
||||
}
|
||||
|
||||
FastLED.show();
|
||||
delay(3000);
|
||||
|
||||
// Set LEDs blue for "ready"
|
||||
for (CRGB& led : leds)
|
||||
led = 0x0000FF;
|
||||
led = CFGB::Blue;
|
||||
|
||||
FastLED.show();
|
||||
|
||||
// Initialize relevant output method / USB or serial
|
||||
@@ -229,7 +233,8 @@ void loop() {
|
||||
#endif
|
||||
|
||||
// If currently paused through a config command, do not execute main loop
|
||||
if (!activated) return;
|
||||
if (!activated)
|
||||
return;
|
||||
|
||||
// Scan touch keyboard and update lights
|
||||
touchboard->scan();
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
#define AIR_SENSOR_1_PIN 19
|
||||
#define AIR_SENSOR_2_PIN 18
|
||||
#define AIR_SENSOR_3_PIN 23
|
||||
#define AIR_SENSOR_4_PIN 21
|
||||
#define AIR_SENSOR_4_PIN 22
|
||||
#define AIR_SENSOR_5_PIN 21
|
||||
#endif
|
||||
#else
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
#include "Touchboard.h"
|
||||
|
||||
void Touchboard::scan()
|
||||
{
|
||||
// For each key, set multiplexers and poll both capacitive sensors simultaneously
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
digitalWrite(MUX_0, bitRead(i, 0));
|
||||
digitalWrite(MUX_1, bitRead(i, 1));
|
||||
digitalWrite(MUX_2, bitRead(i, 2));
|
||||
|
||||
#ifndef TEENSY
|
||||
unsigned int* values = sensor.sense(3);
|
||||
|
||||
// Store the values received from the sensor poll into their respective positions
|
||||
keys[i] = values[0];
|
||||
keys[i + 8] = values[1];
|
||||
#else
|
||||
keys[i] = (unsigned int)touchRead(TOUCH_0);
|
||||
keys[i+8] = (unsigned int)touchRead(TOUCH_1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void Touchboard::calibrateKeys()
|
||||
{
|
||||
// Reset calibration data for all keys
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
em_averages[i] = 0;
|
||||
neutral_values[i] = 0;
|
||||
key_states[i] = false;
|
||||
keys[i] = false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < CALIBRATION_SAMPLES; i++) {
|
||||
// Repeatedly scan all keys
|
||||
scan();
|
||||
// Store the lowest read value as our baseline
|
||||
for (int j = 0; j < 16; j++) {
|
||||
if (keys[j] > neutral_values[j]) neutral_values[j] = keys[j];
|
||||
}
|
||||
}
|
||||
// After calibration is complete, initialize the averages to the baseline values established previously
|
||||
for (int i = 0; i < 16; i++) {
|
||||
em_averages[i] = neutral_values[i];
|
||||
}
|
||||
}
|
||||
|
||||
bool Touchboard::update(int key)
|
||||
{
|
||||
int read_value = keys[key];
|
||||
float new_average = alpha * read_value + (1 - alpha) * em_averages[key];
|
||||
if (read_value - neutral_values[key] < deadzone)
|
||||
{
|
||||
// If we are in the deadzone, ignore any touch events, set the key to be untouched and update the moving average.
|
||||
if (key_states[key])
|
||||
em_averages[key] = neutral_values[key];
|
||||
else
|
||||
em_averages[key] = new_average;
|
||||
key_states[key] = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
//If we are outside of the deadzone:
|
||||
if (read_value > em_averages[key] + threshold)
|
||||
{
|
||||
// If we just detected a touch, make the key touched, discard the previous moving average and trigger the callback.
|
||||
em_averages[key] = read_value;
|
||||
onKeyPress(key, key_states[key]);
|
||||
key_states[key] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just update the average.
|
||||
em_averages[key] = new_average;
|
||||
}
|
||||
}
|
||||
return key_states[key];
|
||||
}
|
||||
|
||||
void Touchboard::setThreshold(uint16_t threshold)
|
||||
{
|
||||
this->threshold = threshold;
|
||||
EEPROM.put(0, threshold);
|
||||
}
|
||||
|
||||
void Touchboard::setDeadzone(uint16_t deadzone)
|
||||
{
|
||||
this->deadzone = deadzone;
|
||||
EEPROM.put(4, deadzone);
|
||||
}
|
||||
|
||||
void Touchboard::setAlpha(float alpha)
|
||||
{
|
||||
this->alpha = alpha;
|
||||
EEPROM.put(8, alpha);
|
||||
}
|
||||
|
||||
int Touchboard::getThreshold()
|
||||
{
|
||||
return threshold;
|
||||
}
|
||||
|
||||
int Touchboard::getDeadzone()
|
||||
{
|
||||
return deadzone;
|
||||
}
|
||||
|
||||
float Touchboard::getAlpha()
|
||||
{
|
||||
return alpha;
|
||||
}
|
||||
|
||||
float Touchboard::getNeutralValue(int key)
|
||||
{
|
||||
return neutral_values[key];
|
||||
}
|
||||
|
||||
float Touchboard::getEmAverages(int key)
|
||||
{
|
||||
return em_averages[key];
|
||||
}
|
||||
|
||||
float Touchboard::getRawValue(int key)
|
||||
{
|
||||
return keys[key];
|
||||
}
|
||||
|
||||
Touchboard::Touchboard(void(*keyPressCallback)(int, bool)) :
|
||||
#ifndef TEENSY
|
||||
sensor(CapacitiveSensor(SEND, RECEIVE_1, RECEIVE_2)),
|
||||
#endif
|
||||
onKeyPress(keyPressCallback)
|
||||
{
|
||||
EEPROM.get(0, threshold);
|
||||
EEPROM.get(4, deadzone);
|
||||
EEPROM.get(8, alpha);
|
||||
|
||||
pinMode(MUX_0, OUTPUT);
|
||||
pinMode(MUX_1, OUTPUT);
|
||||
pinMode(MUX_2, OUTPUT);
|
||||
|
||||
calibrateKeys();
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
// Touchboard.h
|
||||
|
||||
#ifndef _TOUCHBOARD_h
|
||||
#define _TOUCHBOARD_h
|
||||
|
||||
#include "PinConfig.h"
|
||||
#ifndef TEENSY
|
||||
#include "CapacitiveSensor.h"
|
||||
#endif
|
||||
#include <EEPROM.h>
|
||||
|
||||
#define EMA_TOUCHDETECT_ALPHA 0.1f
|
||||
#define CALIBRATION_SAMPLES 25
|
||||
|
||||
class Touchboard
|
||||
{
|
||||
private:
|
||||
#ifndef TEENSY
|
||||
CapacitiveSensor sensor;
|
||||
#endif
|
||||
|
||||
uint16_t threshold;
|
||||
uint16_t deadzone;
|
||||
float alpha;
|
||||
|
||||
float em_averages[16];
|
||||
unsigned int keys[16];
|
||||
int neutral_values[16];
|
||||
void (*onKeyPress)(int, bool);
|
||||
|
||||
public:
|
||||
bool key_states[16];
|
||||
|
||||
Touchboard(void(*keyPressCallback)(int, bool));
|
||||
void scan();
|
||||
bool update(int key);
|
||||
void setThreshold(uint16_t threshold);
|
||||
void setDeadzone(uint16_t deadzone);
|
||||
void setAlpha(float alpha);
|
||||
int getThreshold();
|
||||
int getDeadzone();
|
||||
float getAlpha();
|
||||
float getNeutralValue(int key);
|
||||
float getEmAverages(int key);
|
||||
float getRawValue(int key);
|
||||
void calibrateKeys();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user