-
Notifications
You must be signed in to change notification settings - Fork 0
/
ET8861S_I2C.ino
168 lines (142 loc) · 3.71 KB
/
ET8861S_I2C.ino
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <Arduino.h>
#include <stdio.h>
#include "ET8861.h"
#include "TinyWireS.h"
#define I2C_ADDRESS 0x14
#define SHOW_GREETING 1
/**
* No data, just the command
*/
#define COMMAND_CLEAR 0x00
/**
* Followed by 7 bytes character data
*/
#define COMMAND_SET_STRING 0x01
/**
* Followed by 1 byte position and 1 byte character data
*/
#define COMMAND_SET_CHARACTER 0x02
/**
* Followed by 1 byte symbol data
*/
#define COMMAND_SET_SYMBOLS 0x03
/**
* Followed by 1 byte position and 2 bytes segment data
* (MSB first).
* A position which received segment data is locked for
* string/character update. The lock will be removed
* if segment data 0 is received for that position.
*/
#define COMMAND_SET_SEGMENTS 0x04
static uint8_t receiveByte = 0;
char displayString[8] = " ";
uint8_t stringPositionFilter = 0xff;
static bool isDirty = false;
void receiveEvent(uint8_t bytesInBuffer) {
register uint8_t command;
if (bytesInBuffer < 1 || bytesInBuffer > 8) {
return;
}
command = TinyWireS.receive();
if (command == COMMAND_CLEAR && bytesInBuffer == 1) {
stringPositionFilter = 0;
ET8861.unsetAdditionalSymbol(0xFF);
sprintf(displayString, " ");
ET8861.setString(displayString);
}
if (command == COMMAND_SET_STRING && bytesInBuffer == 8) {
receiveCommandSetString();
isDirty = true;
}
if (command == COMMAND_SET_CHARACTER && bytesInBuffer == 3) {
receiveCommandSetCharacter();
isDirty = true;
}
if (command == COMMAND_SET_SYMBOLS && bytesInBuffer == 2) {
receiveCommandSetSymbols();
isDirty = true;
}
if (command == COMMAND_SET_SEGMENTS && bytesInBuffer == 4) {
receiveCommandSetSegments();
isDirty = true;
}
for (register uint8_t bytesToDrain = TinyWireS.available(); bytesToDrain > 0; bytesToDrain--) {
TinyWireS.receive();
}
}
void receiveCommandSetString() {
for (uint8_t displayPosition = 0; displayPosition < 7; displayPosition++) {
displayString[displayPosition] = TinyWireS.receive();
}
}
void receiveCommandSetCharacter() {
uint8_t position = TinyWireS.receive();
if (position >= 7) {
return;
}
displayString[position] = TinyWireS.receive();
}
void receiveCommandSetSymbols() {
uint8_t symbols = TinyWireS.receive();
ET8861.unsetAdditionalSymbol(0xFF);
ET8861.setAdditionalSymbol(symbols);
}
void receiveCommandSetSegments() {
uint8_t position = TinyWireS.receive();
if (position >= 7) {
return;
}
uint16_t segments = TinyWireS.receive();
segments <<= 8;
segments |= TinyWireS.receive();
ET8861.setSegments(position, segments);
if(segments == 0L) {
// allow this position again
stringPositionFilter |= (1 << position);
} else {
// lock this position
stringPositionFilter &= ~(1 << position);
}
}
void applyFilterToDisplayString() {
for(uint8_t position = 0; position < 7; position ++) {
if(! (stringPositionFilter & (1 << position))) {
displayString[position] = 0;
continue;
}
if(displayString[position] == 0) {
displayString[position] = ' ';
}
}
}
void showGreeting() {
ET8861.setAllSegmensOn();
delay(500);
for(register uint8_t position = 0; position < 7; position++) {
ET8861.setCharacter(position, ' ');
delay(100);
}
ET8861.clear();
sprintf(displayString, "I2C $%X", I2C_ADDRESS);
ET8861.setString(displayString);
ET8861.setSegments(2, 0b0000110001001000);
delay(1500);
sprintf(displayString, " ");
isDirty = true;
}
void setup() {
ET8861.enable();
#if SHOW_GREETING
showGreeting();
#endif
TinyWireS.begin(I2C_ADDRESS);
TinyWireS.onReceive(receiveEvent);
}
void loop() {
if (isDirty) {
applyFilterToDisplayString();
ET8861.setString(displayString);
isDirty = false;
}
delay(10);
}