diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 72bdf8d..99f3269 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(COMPONENT_SRCS "app_main.c" "nokia.c" "knob.c" "gui.c") +set(COMPONENT_SRCS "app_main.c" "nokia.c" "knob.c" "gui.c" "analog.c") set(COMPONENT_ADD_INCLUDEDIRS "") register_component() diff --git a/main/analog.c b/main/analog.c new file mode 100644 index 0000000..e6ae5d9 --- /dev/null +++ b/main/analog.c @@ -0,0 +1,72 @@ +#include +#include +#include "analog.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/gpio.h" +#include "driver/adc.h" +#include "esp_adc_cal.h" + +static esp_adc_cal_characteristics_t *adc_chars; + +// 32-39 +static const adc1_channel_t channel = ADC1_CHANNEL_4; //GPIO34 if ADC1, GPIO14 if ADC2 + +static float measurement_celsius; +static const adc_atten_t atten = ADC_ATTEN_DB_0; +static const adc_unit_t unit = ADC_UNIT_1; + +static void analog_service(void *arg); + +static TaskHandle_t hAnalog; + +#define DEFAULT_VREF 1100 +#define NO_OF_SAMPLES 64 + +void analog_init() { + printf("Analog init\n"); + + adc1_config_width(ADC_WIDTH_BIT_12); + adc1_config_channel_atten(channel, atten); + adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); + esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars); + + int rv = xTaskCreate(analog_service, "analog", 4096, NULL, 6, &hAnalog); + assert (rv == pdPASS); +} + + +static void __attribute__((noreturn)) analog_service(void *arg) { + while (1) { + uint32_t adc_reading = 0; + //Multisampling + for (int i = 0; i < NO_OF_SAMPLES; i++) { + adc_reading += adc1_get_raw(channel); + } + adc_reading /= NO_OF_SAMPLES; + + //Convert adc_reading to voltage in mV + uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); + +#define CORRECT -10; + voltage += CORRECT; + +// printf("Raw: %d ... Voltage: %dmV ...", adc_reading, voltage); + + float volts = voltage * 0.001f; + +#define R1 4750 +#define V1 3.3f + float r_pt100 = (volts * R1)/(V1 - volts); + float celsius = (r_pt100/100.0f - 1.0f) / 3.9083E-3f; +// printf("Rpt %.3f, Celsius: %.1f\n", r_pt100, celsius); + + measurement_celsius = celsius; + + vTaskDelay(pdMS_TO_TICKS(100)); + } +} + +float analog_read() { + return measurement_celsius; +} diff --git a/main/analog.h b/main/analog.h new file mode 100644 index 0000000..875ec16 --- /dev/null +++ b/main/analog.h @@ -0,0 +1,14 @@ +/** + * TODO file description + * + * Created on 2020/01/03. + */ + +#ifndef REFLOWER_ANALOG_H +#define REFLOWER_ANALOG_H + +void analog_init(); + +float analog_read(); + +#endif //REFLOWER_ANALOG_H diff --git a/main/app_main.c b/main/app_main.c index 254137b..6c3d6be 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -14,6 +14,7 @@ #include "nokia.h" #include "knob.h" #include "gui.h" +#include "analog.h" void __attribute__((noreturn)) app_main() @@ -28,8 +29,8 @@ void __attribute__((noreturn)) app_main() gui_init(); - knob_init(); + analog_init(); bool level = 0; while (1) { diff --git a/main/gui.c b/main/gui.c index f5fb85b..e3dd218 100644 --- a/main/gui.c +++ b/main/gui.c @@ -1,5 +1,6 @@ #include "gui.h" #include "nokia.h" +#include "analog.h" #include #include @@ -32,12 +33,15 @@ void gui_init() { * @param arg */ static void __attribute__((noreturn)) gui_thread(void *arg) { - uint32_t pos = 20; + uint32_t pos = 0; bool btn = 0; + + uint32_t last_wheel_time = 0; #define NMAX 60 while (1) { uint32_t value = 0; - xTaskNotifyWait(0, ULONG_MAX, &value, portMAX_DELAY); + xTaskNotifyWait(0, ULONG_MAX, &value, pdMS_TO_TICKS(250)); + // printf("Knob event 0x%02x ", value); if (value & 0b1000) { @@ -47,23 +51,27 @@ static void __attribute__((noreturn)) gui_thread(void *arg) { btn = 0; } - if (value & 0b01) { -// printf("FWD "); - if (pos == NMAX) { - pos = 0; - } - else { - pos += 1; + if (value & 0b11) { + uint32_t time = xTaskGetTickCount(); + uint32_t increment = 1; + if (last_wheel_time != 0) { + uint32_t ela = time - last_wheel_time; + if (ela < pdMS_TO_TICKS(20)) { + increment = 25; + } else if (ela < pdMS_TO_TICKS(35)) { + increment = 10; + } else if (ela < pdMS_TO_TICKS(75)) { + increment = 5; + } } - } + last_wheel_time = time; - if (value & 0b10) { -// printf("BACK "); - if (pos == 0) { - pos = NMAX; + if (value & 0b01) { + pos += increment; } - else { - pos--; + + if (value & 0b10) { + pos -= increment; } } @@ -71,6 +79,8 @@ static void __attribute__((noreturn)) gui_thread(void *arg) { char buf[10]; sprintf(buf, "%3d %s", pos, btn?"BTN":""); LCD_setStr(buf, 2, 17, 0); + sprintf(buf, "%.0f C", analog_read()); + LCD_setStr(buf, 2, 26, 0); LCD_updateDisplay(); // printf(">");