minuteur-esp/src/main.cpp

128 lines
3.8 KiB
C++
Raw Normal View History

2023-09-02 23:58:11 +02:00
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h>
2023-09-02 23:58:11 +02:00
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
2023-09-02 18:54:12 +02:00
#define SDA 12
#define SCL 13
#define SORTIE_RELAIS 16
#define LED_FLASH 4
#define LED_BUILTIN 33
#define ENCROT_A 14
#define ENCROT_B 15
#define ENCROT_BP 2
// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(ENCROT_A, ENCROT_B, RotaryEncoder::LatchMode::TWO03);
2023-09-02 19:18:39 +02:00
2023-09-02 23:58:11 +02:00
// durée par défaut en secondes
int count = 12;
bool decompteStatut = false;
unsigned long LastUpdate = 0;
unsigned long LastReadBP = 0;
2023-09-02 19:18:39 +02:00
2023-09-02 23:58:11 +02:00
#define LOGO_HEIGHT 32
#define LOGO_WIDTH 32
const unsigned char bitmap_K [] PROGMEM = {
//uint8_t bitmap_K [] PROGMEM = {
// 'K, 32x32px
0xff, 0xff, 0xf8, 0x03, 0x80, 0x7f, 0xf0, 0x07, 0x80, 0x7f, 0xe0, 0x0f, 0x80, 0x7f, 0xc0, 0x1f,
0x80, 0x7f, 0x80, 0x3f, 0x80, 0x7f, 0x00, 0x7f, 0x80, 0x7e, 0x00, 0xff, 0x80, 0x7c, 0x01, 0xff,
0x80, 0xf8, 0x03, 0xff, 0x00, 0xf0, 0x07, 0xff, 0x80, 0xe0, 0x0f, 0xff, 0x80, 0xc0, 0x1f, 0xff,
0x80, 0x00, 0x3f, 0xff, 0x00, 0x00, 0x3f, 0xff, 0x00, 0x00, 0x1f, 0xff, 0x00, 0x00, 0x0f, 0xff,
0x00, 0x00, 0x0f, 0xff, 0x00, 0x00, 0x07, 0xff, 0x00, 0x00, 0x03, 0xff, 0x00, 0x38, 0x01, 0xff,
0x00, 0x7c, 0x01, 0xff, 0x00, 0xfe, 0x00, 0xff, 0x00, 0xff, 0x00, 0x7f, 0x00, 0xff, 0x00, 0x3f,
0x00, 0xff, 0x80, 0x3f, 0x00, 0xff, 0xc0, 0x1f, 0x00, 0xff, 0xe0, 0x0f, 0x00, 0xff, 0xe0, 0x07,
0x00, 0xff, 0xf0, 0x07, 0x00, 0xff, 0xf8, 0x03, 0x00, 0xff, 0xf8, 0x01, 0x00, 0xff, 0xfc, 0x01
};
void setup() {
pinMode(ENCROT_A, INPUT);
pinMode(ENCROT_B, INPUT);
pinMode(ENCROT_BP, INPUT);
2023-09-02 23:58:11 +02:00
pinMode(SORTIE_RELAIS, OUTPUT);
digitalWrite(SORTIE_RELAIS, LOW);
Wire.begin(SDA, SCL); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Show the 'K' splash screen
display.drawBitmap(
(display.width() - LOGO_WIDTH ) / 2,
(display.height() - LOGO_HEIGHT) / 2,
bitmap_K, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
delay(2000);
}
2023-09-02 19:18:39 +02:00
void loop() {
//digitalWrite(SORTIE_RELAIS, HIGH);
display.setTextSize(6);
display.setTextColor(SSD1306_WHITE); // Draw white text
unsigned long currentMillis = millis();
if (decompteStatut) {
if (count > 0) {
if (currentMillis - LastUpdate >= 1000) {
--count;
LastUpdate = currentMillis;
display.clearDisplay();
display.setCursor(0, 20);
display.println(count);
display.display();
}
}
else {
decompteStatut = 0;
}
}
else {
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
if (newPos < 0) {
newPos = 0;
encoder.setPosition(0);
}
pos = newPos;
count = newPos;
display.clearDisplay();
display.setCursor(0, 20);
display.println(count);
display.display();
} // if
}
if (!digitalRead(ENCROT_BP) && (LastReadBP - currentMillis > 250)){
LastReadBP = currentMillis;
decompteStatut = !decompteStatut;
2023-09-02 19:18:39 +02:00
}
2023-09-02 23:58:11 +02:00
digitalWrite(SORTIE_RELAIS, decompteStatut);
}