Add code for demuxing 4 switch signals from adc pin
- create new files with code for demultiplexing the 4 switches from the one analog signal - define lookup table with measurements done with the build pcb, test switches and esp32 breakout board - add note do config that gpio used for digital in is now used for this multiplexer - main.cpp: disable control task, run new handle function in a loop for testing
This commit is contained in:
parent
f52a58aa1c
commit
09ee67f583
@ -6,6 +6,7 @@ idf_component_register(
|
||||
"buzzer.cpp"
|
||||
"vfd.cpp"
|
||||
"display.cpp"
|
||||
"switchesAnalog.cpp"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
)
|
||||
|
@ -28,7 +28,7 @@ extern "C" {
|
||||
#define GPIO_SW_RESET GPIO_NUM_25
|
||||
#define GPIO_SW_SET GPIO_NUM_33
|
||||
#define GPIO_SW_PRESET1 GPIO_NUM_32
|
||||
#define GPIO_SW_PRESET2 GPIO_NUM_34
|
||||
#define GPIO_SW_PRESET2 GPIO_NUM_32 //gpio 34 currently used fir testing switches-analog
|
||||
#define GPIO_SW_PRESET3 GPIO_NUM_39
|
||||
|
||||
#define GPIO_POTI GPIO_NUM_36
|
||||
|
@ -14,6 +14,8 @@ extern "C"
|
||||
#include "control.hpp"
|
||||
#include "buzzer.hpp"
|
||||
|
||||
#include "switchesAnalog.hpp"
|
||||
|
||||
|
||||
//=================================
|
||||
//=========== functions ===========
|
||||
@ -80,11 +82,16 @@ extern "C" void app_main()
|
||||
esp_log_level_set("buzzer", ESP_LOG_ERROR);
|
||||
esp_log_level_set("control", ESP_LOG_INFO);
|
||||
|
||||
//create task for controlling the machine
|
||||
xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
|
||||
//create task for handling the buzzer
|
||||
xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
|
||||
// //create task for controlling the machine
|
||||
// xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
|
||||
// //create task for handling the buzzer
|
||||
// xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
|
||||
|
||||
//beep at startup
|
||||
buzzer.beep(3, 70, 50);
|
||||
|
||||
while(1){
|
||||
switchesAnalog_handle();
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
97
main/switchesAnalog.cpp
Normal file
97
main/switchesAnalog.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include "switchesAnalog.hpp"
|
||||
|
||||
#define CHECK_BIT(var,pos) (((var)>>(pos)) & 1) //TODO duplicate code: same macro already used in vfd.cpp
|
||||
|
||||
|
||||
|
||||
//=============================
|
||||
//========= readAdc ===========
|
||||
//=============================
|
||||
//TODO add this to gpio library - used multiple times in this project
|
||||
//function for multisampling an anlog input
|
||||
int readAdc(adc1_channel_t adc_channel, bool inverted = false) {
|
||||
//make multiple measurements
|
||||
int adc_reading = 0;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
adc_reading += adc1_get_raw(adc_channel);
|
||||
}
|
||||
adc_reading = adc_reading / 16;
|
||||
//return original or inverted result
|
||||
if (inverted) {
|
||||
return 4095 - adc_reading;
|
||||
} else {
|
||||
return adc_reading;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//=====================
|
||||
//===== Variables =====
|
||||
//=====================
|
||||
static const char *TAG = "switches-analog"; //tag for logging
|
||||
int diffMin = 4095;
|
||||
int adcValue;
|
||||
int match_index = 0;
|
||||
|
||||
//array that describes voltages for all combinations of the 4 inputs
|
||||
const int lookup_voltages[] = {
|
||||
//ADC, S3 S2 S1 S0
|
||||
4095, //0000
|
||||
3642, //0001
|
||||
3243, //0010
|
||||
2887, //0011
|
||||
2628, //0100
|
||||
2413, //0101
|
||||
2274, //0110
|
||||
2112, //0111
|
||||
1864, //1000
|
||||
1748, //1001
|
||||
1671, //1010
|
||||
1579, //1011
|
||||
1488, //1100
|
||||
1418, //1101
|
||||
1360, //1110
|
||||
1294 //1111
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
//===========================
|
||||
//===== handle function =====
|
||||
//===========================
|
||||
void switchesAnalog_handle(){
|
||||
//read current voltage
|
||||
adcValue = readAdc(ADC_CHANNEL_BUTTONS);
|
||||
ESP_LOGI(TAG, "voltage read: %d", adcValue);
|
||||
|
||||
//find closest match in lookup table
|
||||
for (int i=0; i<16; i++){
|
||||
int diff = fabs(adcValue - lookup_voltages[i]);
|
||||
if (diff < diffMin){
|
||||
diffMin = diff;
|
||||
match_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
//get bool values for each input from matched index
|
||||
bool s0 = CHECK_BIT(match_index, 0);
|
||||
bool s1 = CHECK_BIT(match_index, 1);
|
||||
bool s2 = CHECK_BIT(match_index, 2);
|
||||
bool s3 = CHECK_BIT(match_index, 3);
|
||||
//bool s1 = ((match_index & 0b1000) == 0b1000);
|
||||
//bool s2 = ((match_index & 0b0100) == 0b0100);
|
||||
//bool s3 = ((match_index & 0b0010) == 0b0010);
|
||||
//bool s4 = ((match_index & 0b0001) == 0b0001);
|
||||
|
||||
|
||||
//log results
|
||||
ESP_LOGI(TAG, "adcRead: %d, closest-match: %d, diff: %d, index: %d, switches: %d%d%d%d",
|
||||
adcValue, lookup_voltages[match_index], diffMin, match_index, (int)s3, (int)s2, (int)s1, (int)s0);
|
||||
|
||||
|
||||
}
|
||||
|
20
main/switchesAnalog.hpp
Normal file
20
main/switchesAnalog.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
extern "C"
|
||||
{
|
||||
#include <stdio.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include "esp_log.h"
|
||||
#include "driver/adc.h"
|
||||
#include <math.h>
|
||||
|
||||
}
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
|
||||
#define GPIO_BUTTONS GPIO_NUM_34
|
||||
#define ADC_CHANNEL_BUTTONS ADC1_CHANNEL_6 //gpio 34
|
||||
|
||||
|
||||
|
||||
void switchesAnalog_handle();
|
Loading…
x
Reference in New Issue
Block a user