-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
145 lines (124 loc) · 3.38 KB
/
main.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Modbus bridge device V3
// Copyright 2020 by [email protected]
#include <Arduino.h>
#include "Blinker.h"
#include "Buttoner.h"
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include "TelnetLogAsync.h"
#include "Logging.h"
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1
#define LED_R GPIO_NUM_4
#define LED_B GPIO_NUM_25
#define BUTTON_A GPIO_NUM_27
#define BUTTON_B GPIO_NUM_26
Blinker rot(LED_R);
Blinker gruen(LED_B);
Buttoner ButtonA(BUTTON_A, HIGH, false, 2);
Buttoner ButtonB(BUTTON_B);
const uint16_t BL_QUICKLY(0x2);
const uint16_t BL_SLOW(0x3000);
const uint16_t BL_FLICK(0xFFFC);
SSD1306AsciiWire oled;
TelnetLog tl(23, 4);
void setup() {
Serial.begin(115200);
Serial.println("\n");
delay(5000);
Serial.println("_OK_\n");
WiFi.begin("FritzGandhi", "MahatmaNetzwerk");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
rot.start(BL_FLICK);
gruen.start(BL_SLOW);
Wire.begin();
Wire.setClock(400000L);
oled.begin(&Adafruit128x32, I2C_ADDRESS);
oled.setFont(System5x7);
oled.setScrollMode(SCROLL_MODE_AUTO);
oled.clear();
oled.printf("012345678901234567890123456789");
delay(3000);
oled.clear();
oled.setFont(fixed_bold10x15);
oled.printf("012345678901234567890123456789");
delay(3000);
oled.clear();
oled.setFont(Callibri10);
oled.printf("012345678901234567890123456789");
delay(3000);
oled.clear();
oled.setFont(System5x7);
tl.begin("Bridge-Test");
// LOGDEVICE = &tl;
MBUlogLvl = LOG_LEVEL_VERBOSE;
}
void loop() {
static uint32_t dTimer = millis();
static bool dOn = false;
static unsigned int oldClientNum = 999;
char buffer[64];
rot.update();
gruen.update();
if (oldClientNum != tl.getActiveClients()) {
oldClientNum = tl.getActiveClients();
Serial.printf("%d clients.\n", oldClientNum);
}
if (ButtonA.update() > 0) {
if (!dOn) oled.ssd1306WriteCmd(SSD1306_DISPLAYON);
dOn = true;
dTimer = millis();
switch (ButtonA.getEvent()) {
case BE_CLICK:
snprintf(buffer, 64, "A clicked.(%d)", ButtonA.qSize());
break;
case BE_DOUBLECLICK:
snprintf(buffer, 64, "A doubly clicked.(%d)", ButtonA.qSize());
break;
case BE_PRESS:
snprintf(buffer, 64, "A held down.(%d)", ButtonA.qSize());
oled.setContrast(0);
break;
default:
snprintf(buffer, 64, "Huh? A?(%d)", ButtonA.qSize());
break;
}
oled.println();
oled.print(buffer);
LOG_D("Button %s\n", buffer);
HEXDUMP_V("Buffer", (uint8_t *)buffer, strlen(buffer));
}
if (ButtonB.update() > 0) {
if (!dOn) oled.ssd1306WriteCmd(SSD1306_DISPLAYON);
dOn = true;
dTimer = millis();
switch (ButtonB.getEvent()) {
case BE_CLICK:
snprintf(buffer, 64, "B clicked.(%d)", ButtonB.qSize());
break;
case BE_DOUBLECLICK:
snprintf(buffer, 64, "B doubly clicked.(%d)", ButtonB.qSize());
break;
case BE_PRESS:
snprintf(buffer, 64, "B held down.(%d)", ButtonB.qSize());
oled.setContrast(255);
break;
default:
snprintf(buffer, 64, "Huh? B?(%d)", ButtonB.qSize());
break;
}
oled.println();
oled.print(buffer);
tl.println(buffer);
}
if (dOn && millis() - dTimer > 5000) {
oled.ssd1306WriteCmd(SSD1306_DISPLAYOFF);
dOn = false;
}
}