Remove public handle func, add getState functions

switchesAnalog:
- Add functions for obtaining states from each switch
- Remove public handle function, (now run locally at each state request of
a pin)

- adjust main.cpp to work with new functions
This commit is contained in:
jonny_ji7 2022-09-12 09:55:20 +02:00
parent a932460924
commit 38ad266488
3 changed files with 38 additions and 3 deletions

View File

@ -91,7 +91,8 @@ extern "C" void app_main()
buzzer.beep(3, 70, 50);
while(1){
switchesAnalog_handle();
vTaskDelay(500 / portTICK_PERIOD_MS);
switchesAnalog_getState(0);
}
}

View File

@ -63,12 +63,14 @@ const int lookup_voltages[] = {
//===========================
//===== handle function =====
//===========================
void switchesAnalog_handle(){
//handle demuxing of 4 switches from 1 adc (has to be run repeatedly)
void handle(){
//read current voltage
adcValue = readAdc(ADC_CHANNEL_BUTTONS);
ESP_LOGI(TAG, "voltage read: %d", adcValue);
//find closest match in lookup table
diffMin = 4095; //reset diffMin each run
for (int i=0; i<16; i++){
int diff = fabs(adcValue - lookup_voltages[i]);
if (diff < diffMin){
@ -95,3 +97,30 @@ void switchesAnalog_handle(){
}
//====================
//===== getState =====
//====================
//get state of certain switch (0-3)
bool switchesAnalog_getState(int swNumber){
//run handle function to obtain all current input states
handle();
//get relevant bit
bool state = CHECK_BIT(match_index, swNumber);
ESP_LOGI(TAG, "returned state of switch No. %d = %i", swNumber, (int)state);
return state;
}
bool switchesAnalog_getState_sw0(){
return switchesAnalog_getState(0);
}
bool switchesAnalog_getState_sw1(){
return switchesAnalog_getState(1);
}
bool switchesAnalog_getState_sw2(){
return switchesAnalog_getState(2);
}
bool switchesAnalog_getState_sw3(){
return switchesAnalog_getState(3);
}

View File

@ -16,5 +16,10 @@ extern "C"
#define ADC_CHANNEL_BUTTONS ADC1_CHANNEL_6 //gpio 34
//get current state of certain switch
bool switchesAnalog_getState(int swNumber);
void switchesAnalog_handle();
bool switchesAnalog_getState_sw0();
bool switchesAnalog_getState_sw1();
bool switchesAnalog_getState_sw2();
bool switchesAnalog_getState_sw3();