display control

This commit is contained in:
2025-05-17 13:08:17 +02:00
parent 0e87650257
commit 226f2a3e3f
4 changed files with 219 additions and 61 deletions

View File

@ -1,5 +1,17 @@
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeMonoBold24pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <Adafruit_SSD1306.h>
#include "global.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define PIN_ROTATOR1_WHITE 2
#define PIN_ROTATOR1_GREEN 4
@ -10,18 +22,30 @@
#define SKIP_ROTARY_INPUTS 50
// needs global define
#define DATA_STOP_BYTE 0x00
#define DATA_RESET_BYTE 0xFF
#define ROTATOR1_TRIGGER_BIT 1
#define ROTATOR1_HIGH_BIT 2
#define ROTATOR2_TRIGGER_BIT 4
#define ROTATOR2_HIGH_BIT 8
#define PIN_ADDRESS_1 11
#define PIN_ADDRESS_2 12
#define PIN_ADDRESS_3 13
#define INTERNAL_STATE_UNKNOWN 0
#define INTERNAL_STATE_INITIALIZED 1
int lastClk = HIGH;
#define PIN_ADDRESS_1 14
#define PIN_ADDRESS_2 15
#define PIN_ADDRESS_3 16
#define PIN_DISPLAY1_DC 8
#define PIN_DISPLAY1_RST 9
#define PIN_DISPLAY1_CS 10
#define PIN_DISPLAY2_DC 6
#define PIN_DISPLAY2_RST 7
#define PIN_DISPLAY2_CS 17
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT,
&SPI, PIN_DISPLAY1_DC, PIN_DISPLAY1_RST, PIN_DISPLAY1_CS);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT,
&SPI, PIN_DISPLAY2_DC, PIN_DISPLAY2_RST, PIN_DISPLAY2_CS);
char name1[] = {'X', 'X', 'X'};
char name2[] = {'Y', 'Y', 'Y'};
int internalState = INTERNAL_STATE_UNKNOWN;
uint8_t lastClk = HIGH;
// ringbuffer of trigger and direction values
byte valueBuffer[VALUE_BUFFER] = { 0 };
@ -29,9 +53,21 @@ uint8_t readerPos, writerPos = 0;
void addValue(uint8_t value) {
valueBuffer[writerPos++] = value;
// check for writer position overflow
if (writerPos >= VALUE_BUFFER) {
writerPos = 0;
}
}
// check if writer overtooks reader
// if so, increment reader and check
// for reader position overflow
if (writerPos == readerPos) {
readerPos++;
if (readerPos >= VALUE_BUFFER) {
readerPos = 0;
}
}
}
void falling(uint8_t pin, byte triggerBit, byte highBit) {
@ -51,15 +87,18 @@ void falling(uint8_t pin, byte triggerBit, byte highBit) {
}
void rotator1Falling() {
falling(PIN_ROTATOR1_GREEN, ROTATOR1_TRIGGER_BIT, ROTATOR1_HIGH_BIT);
falling(PIN_ROTATOR1_GREEN, TRIGGER_BIT_1, HIGH_BIT_1);
}
void rotator2Falling() {
falling(PIN_ROTATOR2_GREEN, ROTATOR2_TRIGGER_BIT, ROTATOR2_HIGH_BIT);
falling(PIN_ROTATOR2_GREEN, TRIGGER_BIT_2, HIGH_BIT_2);
}
int eventCount = 0;
byte lastValue = 0;
// this method blocks the parallel use of rotators
// if parallel use should be possible
// lastValue and eventCount must be stored per rotator
bool useInput(byte value) {
if (lastValue == value) {
// same event as last event
@ -80,35 +119,68 @@ bool useInput(byte value) {
}
}
void i2cRequest() {
// if write is ahead, send data
if (writerPos != readerPos) {
byte value = valueBuffer[readerPos++];
if (readerPos >= VALUE_BUFFER) {
readerPos = 0;
}
Wire.write(value);
if (internalState == INTERNAL_STATE_UNKNOWN) {
// looks like we were restarted
// request initialization first
Serial.println("Requesting init");
Wire.write(DATA_RESET_BYTE);
internalState = INTERNAL_STATE_INITIALIZED;
} else {
Wire.write(DATA_STOP_BYTE);
// if write is ahead, send data
if (writerPos != readerPos) {
byte value = valueBuffer[readerPos++];
if (readerPos >= VALUE_BUFFER) {
readerPos = 0;
}
Wire.write(value);
} else {
Wire.write(DATA_STOP_BYTE);
}
}
}
void i2cReceive(int bytes) {
for (int i = 0; i < bytes && Wire.available(); i++) {
while (Wire.available()) {
byte data = Wire.read();
if (data == DATA_RESET_BYTE) {
Serial.println("Received reset request");
writerPos = readerPos = 0;
} else if (data == DATA_INIT_BYTE_1) {
Serial.println("Received init 1");
// next three bytes are the name of the first rotator
for (int i = 0; i < 3 && Wire.available(); i++) {
name1[i] = Wire.read();
}
} else if (data == DATA_INIT_BYTE_2) {
Serial.println("Received init 2");
// next three bytes are the name of the second rotator
for (int i = 0; i < 3 && Wire.available(); i++) {
name2[i] = Wire.read();
}
} else {
// nothing else yet implemented
// not yet implemented?
Serial.println("Received unknown");
}
}
}
uint8_t address = 0;
void displayTestScreen(Adafruit_SSD1306 display) {
display.clearDisplay();
for (int x = 0; x < SCREEN_WIDTH; x++) {
for (int y = 0; y < SCREEN_HEIGHT; y++) {
display.drawPixel(x, y, SSD1306_WHITE);
}
}
display.display();
}
uint8_t address = I2C_ADDRESS_START;
void setup() {
// https://support.arduino.cc/hc/en-us/articles/4839084114460-If-your-board-runs-the-sketch-twice
delay(150);
Serial.begin(115200);
// setup rotator GPIOs
@ -123,9 +195,9 @@ void setup() {
pinMode(PIN_ADDRESS_3, INPUT_PULLUP);
// calculate address by LOW GPIOs
address = digitalRead(PIN_ADDRESS_1) == LOW;
address |= (digitalRead(PIN_ADDRESS_2) == LOW) << 1;
address |= (digitalRead(PIN_ADDRESS_3) == LOW) << 2;
address |= (digitalRead(PIN_ADDRESS_1) == LOW) << 1;
address |= (digitalRead(PIN_ADDRESS_2) == LOW) << 2;
address |= (digitalRead(PIN_ADDRESS_3) == LOW) << 3;
attachInterrupt(digitalPinToInterrupt(PIN_ROTATOR1_WHITE), rotator1Falling, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_ROTATOR2_WHITE), rotator2Falling, FALLING);
@ -133,8 +205,60 @@ void setup() {
Wire.begin(address);
Wire.onRequest(i2cRequest);
Wire.onReceive(i2cReceive);
if(!display1.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed (1)"));
for(;;); // Don't proceed, loop forever
}
displayTestScreen(display1);
if(!display2.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed (2)"));
for(;;); // Don't proceed, loop forever
}
displayTestScreen(display2);
// make sure, test screen is at least displayed 2 seconds
delay(2000);
}
void loop() {
delay(10000);
display1.clearDisplay();
display1.setTextColor(SSD1306_WHITE); // Draw white text
display1.cp437(true); // Use full 256 char 'Code Page 437' font
display1.setTextSize(1); // Normal 1:1 pixel scale
display1.setCursor(0, 8); // Start at top-left corner
display1.setFont(NULL);
display1.write(name1[0]);
display1.write(name1[1]);
display1.write(name1[2]);
display1.setTextSize(2); // Normal 1:1 pixel scale
display1.setCursor(0, 56); // Start at top-left corner
display1.setFont(&FreeMonoBold18pt7b);
display1.write('8');
display1.write('9');
display1.write('0');
display1.display();
display2.clearDisplay();
display2.setTextColor(SSD1306_WHITE); // Draw white text
display2.cp437(true); // Use full 256 char 'Code Page 437' font
display2.setTextSize(1); // Normal 1:1 pixel scale
display2.setCursor(0, 8); // Start at top-left corner
display2.setFont(NULL);
display2.write(name2[0]);
display2.write(name2[1]);
display2.write(name2[2]);
display2.setTextSize(2); // Normal 1:1 pixel scale
display2.setCursor(0, 56); // Start at top-left corner
display2.setFont(&FreeMonoBold18pt7b);
display2.write('1');
display2.write('2');
display2.write('3');
display2.display();
delay(1000);
}