#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <led_strip.h> // https://github.com/UncleRus/esp-idf-lib/tree/master
#include "driver/adc.h" //ADC - Analog Digital Converter driver
#include "esp_adc_cal.h" //ADC kalibracija
#include "driver/gpio.h"
#include "driver/adc.h"
#define LED_TYPE LED_STRIP_WS2812 //vrsta LCD panela
#define LED_GPIO 14 //LCD panel data pin
#define LED_STRIP_LEN 256 //broj ledica u nizu - 64 jedan 8x8 panel
//#define CONFIG_LED_STRIP_FLUSH_TIMEOUT 10
//#define timeout 10
#define MIC_PIN ADC1_CHANNEL_5 //mic pin ADC1_C5 = D33
#define POT_PIN_SENS ADC2_CHANNEL_9 //pin za potenciometar osjetljivosti ADC2_9 D26
#define POT_PIN_BRGH ADC2_CHANNEL_7 //pin za potenciometar svjetline ADC2_7 D27
int sensitivity2 = 0;
int brightness2 = 0;
int brightness3 = 0;
static const rgb_t colors[] = {
{ .r = 0x00, .g = 0x00, .b = 0x00 }, //0 off
{ .r = 0xff, .g = 0x00, .b = 0x00 }, //1 r
{ .r = 0x00, .g = 0xff, .b = 0x00 }, //2 g
{ .r = 0x00, .g = 0x00, .b = 0xff }, //3 b
{ .r = 0x2f, .g = 0x2f, .b = 0x2f }, //4 white
{ .r = 0x30, .g = 0x00, .b = 0x30 }, //5 ljubicasta
{ .r = 0x2F, .g = 0x2F, .b = 0x00 }, //6 zuta
{ .r = 0xFF, .g = 0xA5, .b = 0x00 }, //7 narancasta
};
//ADC konfiguracija
static esp_adc_cal_characteristics_t *adc_chars;
static const adc_atten_t atten = ADC_ATTEN_DB_11; //11db za raspon 0-3.3V, radi i sa 5v, ali je zadnja 1/3 pot@max
static const adc_unit_t unit = ADC_UNIT_2; //kako koristimo ESP na breadbordu, radi dimenzija možemo spajati pinove samo na jednu stranu, +3,3V i dispej pinovi su na istoj strani, stoga koistimo ADC2 kanal koji ima tri pina na istoj strani kao i +3,3V i disp.
void pot_sens_reading_task(void *pvParameters) {
//adc1_config_width(ADC_WIDTH_BIT_12); //dok je bilo prikopcano na ADC1, za ADC2 -The reading width of ADC2 is configured every time you take the reading
adc2_config_channel_atten(POT_PIN_SENS, atten);
while (1) {
int pot_value_sens;
adc2_get_raw(POT_PIN_SENS, ADC_WIDTH_BIT_12, &pot_value_sens);
float sensitivity = (float)pot_value_sens / 4095.0;
//preracunavanje vrijednosti (0-1) u (0-100) *5,*10,*15, x3 max@1500
sensitivity2 = (int)(sensitivity * 99);
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void pot_brgh_reading_task(void *pvParameters) {
adc2_config_channel_atten(POT_PIN_BRGH, atten);
while (1) {
int pot_value_brgh;
adc2_get_raw(POT_PIN_BRGH, ADC_WIDTH_BIT_12, &pot_value_brgh);
float brightness = (float)pot_value_brgh / 4095.0;
//preracunavanje vrijednosti (0-1) u (0-100) *5,*10,*15, x3 max@1500
brightness2 = (int)(brightness * 99);
brightness3 = (int)((brightness * 250)+2);
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void app_main() {
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, 1100, adc_chars);
xTaskCreate(pot_sens_reading_task, "pot_sens_reading_task", configMINIMAL_STACK_SIZE * 5, NULL, 5, NULL);
xTaskCreate(pot_brgh_reading_task, "pot_brgh_reading_task", configMINIMAL_STACK_SIZE * 5, NULL, 5, NULL);
led_strip_install();
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(MIC_PIN, ADC_ATTEN_DB_11);
led_strip_t strip = {
.type = LED_TYPE,
.length = LED_STRIP_LEN,
.gpio = LED_GPIO,
.buf = NULL,
#ifdef LED_STRIP_BRIGHTNESS //mozda nepotrebno???
//.brightness = brightness3, //jacina svjetla
.brightness = brightness3, //jacina svjetla
#endif //mozda nepotrebno???
};
led_strip_init(&strip);
while (1) {
// Update LED strip brightness
strip.brightness = brightness3;
int mic_raw = adc1_get_raw(MIC_PIN); //mic read raw analog
int mid = 1860; //mic srednja vrijednost za definiranje nulte pozicije
int amplitude = abs(mic_raw - mid); //mic convert raw to amplitude
//int min = 30; //mic parametar najmanji signal koji aktivira
//int max = 1000; //mic parametar najjaci signal koji aktivira zadnji led blok
if (amplitude < sensitivity2) {
led_strip_fill(&strip, 0, strip.length, colors[0]); //led_strip_fill(&strip, 0, strip.length, colors[0]); //&strip, prva ledica, broj ledica, boja
led_strip_flush(&strip);
printf("Sensitivity: %02d, brightness: %02d, Raw Value: %04d, Amplitude: %04d, Color: off\n", sensitivity2, brightness3, mic_raw, amplitude); //prikaz u konzoli raw, amp
}
else if (amplitude < (sensitivity2*5)) {
led_strip_fill(&strip, 0, 64, colors[5]);
led_strip_fill(&strip, 64, 192, colors[0]);
led_strip_flush(&strip);
printf("Sensitivity: %02d, brightness: %02d, Raw Value: %04d, Amplitude: %04d, Color: purple\n", sensitivity2, brightness3, mic_raw, amplitude); //prikaz u konzoli raw, amp
}
else if (amplitude < (sensitivity2*10)) {
led_strip_fill(&strip, 0, 64, colors[0]);
led_strip_fill(&strip, 64, 64, colors[2]);
led_strip_fill(&strip, 128, 128, colors[0]);
led_strip_flush(&strip);
printf("Sensitivity: %02d, brightness: %02d, Raw Value: %04d, Amplitude: %04d, Color: green\n", sensitivity2, brightness3, mic_raw, amplitude); //prikaz u konzoli raw, amp
}
else if (amplitude < (sensitivity2*15)) {
led_strip_fill(&strip, 0, 128, colors[0]);
led_strip_fill(&strip, 128, 64, colors[7]);
led_strip_fill(&strip, 192, 64, colors[0]);
led_strip_flush(&strip);
printf("Sensitivity: %02d, brightness: %02d, Raw Value: %04d, Amplitude: %04d, Color: yellow\n", sensitivity2, brightness3, mic_raw, amplitude); //prikaz u konzoli raw, amp
}
else {
led_strip_fill(&strip, 0, 192, colors[0]);
led_strip_fill(&strip, 192, 64, colors[1]);
led_strip_flush(&strip);
printf("Sensitivity: %02d, brightness: %02d, Raw Value: %04d, Amplitude: %04d, Color: red\n", sensitivity2, brightness3, mic_raw, amplitude); //prikaz u konzoli raw, amp
}
vTaskDelay(pdMS_TO_TICKS(50)); //delay za iduce ocitanje mic i paljenje led
}
}