Skip to content

Commit

Permalink
support 32bit touchRead() on ESP32-S2 and S3, allow disabling touchpa…
Browse files Browse the repository at this point in the history
…ddles (#105)
  • Loading branch information
haklein authored Nov 26, 2024
1 parent 545227c commit c4cf052
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Software/src/Version 6/MorsePreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ uint8_t MorsePreferences::loraPower = 14; // default 14 dBm =

uint32_t MorsePreferences::fileWordPointer = 0; // remember how far we have read the file in player mode / reset when loading new file
//uint8_t MorsePreferences::promptPause = 2; // in echoTrainer mode, length of pause before we send next word; multiplied by interWordSpace
uint8_t MorsePreferences::tLeft = 20; // threshold for left paddle
uint8_t MorsePreferences::tRight = 20; // threshold for right paddle
touch_value_t MorsePreferences::tLeft = 20; // threshold for left paddle
touch_value_t MorsePreferences::tRight = 20; // threshold for right paddle

uint8_t MorsePreferences::vAdjust = 180; // correction value: 155 - 250

Expand Down
4 changes: 2 additions & 2 deletions Software/src/Version 6/MorsePreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ namespace MorsePreferences

extern uint32_t fileWordPointer;
extern uint8_t promptPause;
extern uint8_t tLeft;
extern uint8_t tRight;
extern touch_value_t tLeft;
extern touch_value_t tRight;
extern uint8_t vAdjust;
extern uint8_t loraBand;
#define QRG433 434.15E6
Expand Down
39 changes: 27 additions & 12 deletions Software/src/Version 6/m32_v6.ino
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ int8_t maxMemCount = 0;
unsigned int interCharacterSpace, interWordSpace; // need to be properly initialised!
unsigned int halfICS; // used for word doubling: half of extra ICS
unsigned int effWpm; // calculated effective speed in WpM
unsigned int lUntouched = 0; // sensor values (in untouched state) will be stored here
unsigned int rUntouched = 0;
touch_value_t lUntouched = 0; // sensor values (in untouched state) will be stored here
touch_value_t rUntouched = 0;

boolean alternatePitch = false; // to change pitch in CW generator / file player

Expand Down Expand Up @@ -1275,9 +1275,12 @@ void togglePolarity () {
/// binary: 00 01 10 11

uint8_t readSensors(int left, int right, boolean init) {
#ifdef TOUCHPADDLES_DISABLED
return 0;
#else
//long int timer = micros();
//static boolean first = true;
uint8_t v, lValue, rValue;
touch_value_t v, lValue, rValue;

while ( !(v=touchRead(left)) )
; // ignore readings with value 0
Expand All @@ -1287,13 +1290,17 @@ uint8_t readSensors(int left, int right, boolean init) {
rValue = v;

if (init == false) {
if (lValue < (MorsePreferences::tLeft+10)) { //adaptive calibration
MorsePreferences::tLeft = ( 7*MorsePreferences::tLeft + ((lValue+lUntouched) / SENS_FACTOR) ) / 8;
}
if (rValue < (MorsePreferences::tRight+10)) { //adaptive calibration
MorsePreferences::tRight = ( 7*MorsePreferences::tRight + ((rValue+rUntouched) / SENS_FACTOR) ) / 8;
if (sizeof(touch_value_t) < 4) { // pre ESP32-S2/S3 chips use touch values below 32bit so calibration is needed
if (lValue < (MorsePreferences::tLeft+10)) { //adaptive calibration
MorsePreferences::tLeft = ( 7*MorsePreferences::tLeft + ((lValue+lUntouched) / SENS_FACTOR) ) / 8;
}
if (rValue < (MorsePreferences::tRight+10)) { //adaptive calibration
MorsePreferences::tRight = ( 7*MorsePreferences::tRight + ((rValue+rUntouched) / SENS_FACTOR) ) / 8;
}
return ( lValue < MorsePreferences::tLeft ? 2 : 0 ) + (rValue < MorsePreferences::tRight ? 1 : 0 );
} else { // ESP32-S2 & S3
return ( lValue > MorsePreferences::tLeft ? 2 : 0 ) + (rValue > MorsePreferences::tRight ? 1 : 0 );
}
return ( lValue < MorsePreferences::tLeft ? 2 : 0 ) + (rValue < MorsePreferences::tRight ? 1 : 0 );
} else {
//DEBUG("@1216: tLeft: " + String(MorsePreferences::tLeft));
//lValue -=25; rValue -=25;
Expand All @@ -1303,11 +1310,13 @@ uint8_t readSensors(int left, int right, boolean init) {
else
return 0;
}
#endif
}


void initSensors() {
int v;
#ifndef TOUCHPADDLES_DISABLED
touch_value_t v;
lUntouched = rUntouched = 60; /// new: we seek minimum
for (int i=0; i<8; ++i) {
while ( !(v=touchRead(LEFT)) )
Expand All @@ -1321,8 +1330,14 @@ void initSensors() {
}
lUntouched /= 8;
rUntouched /= 8;
MorsePreferences::tLeft = lUntouched - 9;
MorsePreferences::tRight = rUntouched - 9;
if (sizeof(touch_value_t) < 4) { // ESP32 pre S2/S3
MorsePreferences::tLeft = lUntouched - 9;
MorsePreferences::tRight = rUntouched - 9;
} else { // fixed threshold works fine on ESP32 S2&S3 due to 32 bit value ranges
MorsePreferences::tLeft = lUntouched + 10000;
MorsePreferences::tRight = rUntouched + 10000;
}
#endif
}


Expand Down

0 comments on commit c4cf052

Please sign in to comment.