#include "lab.h"
#include "freertos/queue.h"
#include "esp_log.h"
static QueueHandle_t samples;
static void sample_task(void *arg) {
    (void)arg;
    adc_init();
    TickType_t next=xTaskGetTickCount();
    for(;; ) {
        int raw=adc_read_raw();
        xQueueOverwrite(samples,&raw);
        vTaskDelayUntil(&next,pdMS_TO_TICKS(20));
    }
}
static void display_task(void *arg) {
    (void)arg;
    oled_init();
    int raw;
    for(;; ) {
        if(xQueueReceive(samples,&raw,portMAX_DELAY)==pdTRUE) {
            oled_clear();
            oled_number(28,16,(unsigned)raw,4);
            oled_show();
        }
        vTaskDelay(pdMS_TO_TICKS(200));
    }
}
void app_main(void) {
    samples=xQueueCreate(1,sizeof(int));
    assert(samples);
    BaseType_t a=xTaskCreate(sample_task,"sample",4096,NULL,5,NULL);
    assert(a==pdPASS);
    BaseType_t b=xTaskCreate(display_task,"display",4096,NULL,3,NULL);
    assert(b==pdPASS);
    ESP_LOGI("tasks","two tasks created");
}
