// For ESP32C3 SuperMini & M5Stamp C3U (using "ESP32C3 Dev Module" board)
// -> USB off + Left Button pressed -> USB on + Left button pressed -> Release Left button
// -> After Upload the code, press Right button (reset)
#include <Wire.h>
#include <U8g2lib.h>
#define I2C_SDA 8 //4 //21 //GPIO 8 also connected with BUILD-IN LED
#define I2C_SCL 9 //5 //22
#define LED_BLUE 8 //Built-in LED Blue SuperMini Board
#define Pin_AnalogSignal 1 //21 // Pin por el que leeremos el valor de la señal externa.
#define wavelength 40
int AnalogSignal[wavelength];
int printAnalogSignal[wavelength];
bool flagDACbuffer = 0;
// OLED display 0.96 128x64 SSD1306 BiColor 0..14 Yellow 15..63 Blue
// --> u8g2.setFont(u8g2_font_scrum_tr); //https://github.com/olikraus/u8g2/wiki/fntlist8 fuente muy equilibrada, la _tf tambien
// --> u8g2.setFont(u8g2_font_bpixeldouble_tr); fuente un poco más BOLD (negrita)
//U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ SCL, /* data=*/ SDA); // 0.91 Display
//U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //M0/ESP32/ESP8266/mega2560/Uno/Leonardo
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // 0.91 Display
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // 1.3 Display
// Trigonometría https://www.disfrutalasmatematicas.com/seno-coseno-tangente.html
#define gaugeRadio 50 // radio de la circunferencia del Gauge
#define needle_Xpos 82 // centro X del needle en pixels
#define needle_Ypos 90 // centro Y del needle en pixels
#define needle_Dist 54 // distancia del needle en pixels
#define needle_L_limit 140 // 156 // ángulo grados
#define needle_R_limit 40 // 24 // ángulo grados
float RAD2GRA = PI/180; // Las funciones sin, cos, tan trabajan en Radianes y hay que convertirlas a Grados
float needle_Angle = 90; // Ángulo por defecto...
void draw_welcome_display()
{
u8g2.firstPage();
do {
//keep this the same as it pages through the data generating the screen
//u8g2.setFont(u8g2_font_t0_18_tr);
//u8g2.setFont(u8g2_font_balthasar_titling_nbp_tr);
u8g2.setFont(u8g2_font_scrum_tr);
u8g2.drawStr(4, 14, "ADN electronics");
u8g2.setFont(u8g2_font_profont22_tr); //https://github.com/olikraus/u8g2/wiki/fntlist8
u8g2.drawStr(14, 40, "ESP32C3");
u8g2.drawStr(22, 60, "U8g2lib");
//u8g2.setFont(u8g2_font_profont15_tr); //https://github.com/olikraus/u8g2/wiki/fntlist8
//u8g2.drawStr(6,60,"NPDC development");
} while ( u8g2.nextPage() );
delay(4000);
u8g2.clear();
}
void setup()
{
Serial.begin(115200);
pinMode(Pin_AnalogSignal, INPUT);
pinMode(LED_BLUE, OUTPUT);
for (int i=0; i<7; i++)
{
if (digitalRead(LED_BLUE) == HIGH) {digitalWrite(LED_BLUE, LOW);} // Set to LOW --> LED ON (lógica inversa)
else {digitalWrite(LED_BLUE, HIGH);} // Set to HIGH --> LED OFF (lógica inversa)
delay(500);
}
Wire.setPins(I2C_SDA, I2C_SCL);
Wire.begin();
u8g2.begin();
draw_welcome_display();
}
void loop()
{
f_DAC_getAnalogSignal();
f_DAC_U8g2refresh();
}
void f_DAC_getAnalogSignal()
{
if (flagDACbuffer==0)
{
for (int i=0; i<wavelength; i++)
{
//AnalogSignal[i] = random(0,255);
AnalogSignal[i] = analogRead(Pin_AnalogSignal);
//Serial.println(AnalogSignal[i]);
}
flagDACbuffer = 1;
}
else
{
for (int i=0; i<wavelength; i++)
{
AnalogSignal[i] = printAnalogSignal[i];
}
}
for (int i=0; i<wavelength; i++) // Movemos los puntos ya leídos una posición a la izquienda.
{
if (i>0) {printAnalogSignal[i-1] = AnalogSignal[i];}
}
//printAnalogSignal[wavelength-1] = random(0,255); // Nueva lectura del último punto [wavelength-1]
printAnalogSignal[wavelength-1] = analogRead(Pin_AnalogSignal);
}
void f_DAC_U8g2refresh()
{
u8g2.firstPage();
do { //keep this the same as it pages through the data generating the screen
//u8g2.setFont(u8g2_font_scrum_tr);
//u8g2.setFont(u8g2_font_glasstown_nbp_tr);
u8g2.setFont(u8g2_font_tallpixelextended_tr);
u8g2.drawStr(0, 10, "DAC ch.01");
//u8g2.setFont(u8g2_font_scrum_tr);
u8g2.setFont(u8g2_font_glasstown_nbp_tr);
u8g2.drawStr(0, 22, "input signal :");
u8g2.setFont(u8g2_font_profont22_tr); //https://github.com/olikraus/u8g2/wiki/fntlist8
u8g2.setCursor(73, 18);
u8g2.println(printAnalogSignal[wavelength-1],1); // El 1 es para definir el redondeo a 1 decimal
byte x = 0;
byte y = 0;
for (int i=0; i<wavelength; i++)
{
x = i + 0; // El + 0 es un offset para no dibujar en el borde.
y = 63 - map(printAnalogSignal[i], 0, 4095, 0, 32);
u8g2.drawPixel(x,y);
}
u8g2.drawCircle(needle_Xpos, needle_Ypos, gaugeRadio, U8G2_DRAW_UPPER_LEFT | U8G2_DRAW_UPPER_RIGHT);
needle_Angle = map(printAnalogSignal[wavelength-1], 0, 4095, needle_L_limit, needle_R_limit);
u8g2.drawLine(needle_Xpos, needle_Ypos, (needle_Xpos + needle_Dist*cos(needle_Angle*RAD2GRA)), (needle_Ypos-needle_Dist*sin(needle_Angle*RAD2GRA)));
} while (u8g2.nextPage());
//delay(10);
}