1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <LiquidCrystal_I2C.h>
template <uint8_t Cols, uint8_t Rows>
class Tools : public LiquidCrystal_I2C {
private:
char display[Rows][Cols] = {}; // Tableau 2D pour stocker les données
uint8_t currentCol = 0; // Colonne actuelle du curseur
uint8_t currentRow = 0; // Ligne actuelle du curseur
public:
Tools(uint8_t address) : LiquidCrystal_I2C(address, Cols, Rows) {}
void init() {
LiquidCrystal_I2C::init();
backlight();
clear();
}
void setCursor(uint8_t col, uint8_t row) {
if (col < Cols && row < Rows) {
currentCol = col;
currentRow = row;
Serial.print("col:"); Serial.print(col);
Serial.print("\trow:"); Serial.println(row);
LiquidCrystal_I2C::setCursor(col, row);
}
}
size_t write(uint8_t value) {
if (currentCol < Cols && currentRow < Rows) {
display[currentRow][currentCol] = value;
// on ne gère pas comme un LCD le dépassement en fin de ligne
currentCol++;
Serial.write(value);
}
return LiquidCrystal_I2C::write(value);
}
void clear() {
for (uint8_t row = 0; row < Rows; row++) {
for (uint8_t col = 0; col < Cols; col++) {
display[row][col] = ' ';
}
}
LiquidCrystal_I2C::clear();
}
void afficherBuffer() {
Serial.println();
Serial.write(' ');
for (uint8_t col = 0; col < Cols; col++) Serial.write(col % 5 == 0 ? '|' : '.');
Serial.println();
for (uint8_t row = 0; row < Rows; row++) {
Serial.write('|');
for (uint8_t col = 0; col < Cols; col++) Serial.write(display[row][col]);
Serial.println('|');
}
}
};
Tools<20, 4> lcd(0x27);
void setup() {
Serial.begin(115200);
lcd.init();
lcd.setCursor(3, 0);
lcd.print("Hello, world!");
lcd.afficherBuffer();
lcd.setCursor(2, 2);
lcd.print("BONJOUR A TOUS!");
lcd.afficherBuffer();
}
void loop() {} |
Partager