-
Notifications
You must be signed in to change notification settings - Fork 13
/
VW2002FISWriter.cpp
315 lines (260 loc) · 8.07 KB
/
VW2002FISWriter.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "VW2002FISWriter.h"
#include <MemoryFree.h>
#include <Arduino.h>
// #define ENABLE_IRQ 1
// #define DEBUG_MEM 1
// Constructor
VW2002FISWriter::VW2002FISWriter(uint8_t clkPin, uint8_t dataPin, uint8_t enaPin)
{
_FIS_WRITE_CLK = clkPin;
_FIS_WRITE_DATA = dataPin;
_FIS_WRITE_ENA = enaPin;
}
// Destructor
VW2002FISWriter::~VW2002FISWriter()
{
}
//Initialize instrument-cluster
void VW2002FISWriter::FIS_init() {
// set port signals
pinMode(_FIS_WRITE_ENA, OUTPUT);
digitalWrite(_FIS_WRITE_ENA, LOW);
FIS_WRITE_stopENA();
pinMode(_FIS_WRITE_CLK, OUTPUT);
digitalWrite(_FIS_WRITE_CLK, HIGH);
pinMode(_FIS_WRITE_DATA, OUTPUT);
digitalWrite(_FIS_WRITE_DATA, HIGH);
// init port
FIS_WRITE_send_3LB_singleByteCommand(0xF0);
delay(200);
FIS_WRITE_send_3LB_singleByteCommand(0xC3);
delay(200);
FIS_WRITE_send_3LB_singleByteCommand(0xF0);
delay(200);
FIS_WRITE_send_3LB_singleByteCommand(0xC3);
delay(200);
}; // FIS_init
static char tx_array[20];
void VW2002FISWriter::sendMsg(String line1, String line2, bool center) {
// char msg1[] = {0x81, 18, 240, ' ', 'N', 'R', 'K', ' ', 'P', '1', ' ', 'F', 'M', '1', '.', '1', ' ', ' ', 28, 75};
// fill lines to 8 chars
line1.toUpperCase();
line2.toUpperCase();
while (line1.length() < 8) line1 += " ";
while (line2.length() < 8) line2 += " ";
// build tx_array
tx_array[0] = 0x81; // command to set text-display in FIS
tx_array[1] = 18; // Length of this message (command and this length not counted
tx_array[2] = 0xF0; // unsure what this is
line1.toCharArray(&tx_array[3], 9);
if (tx_array[10] == 32 && !center) tx_array[10] = 28; // set last char to 28 if not center
line2.toCharArray(&tx_array[11], 9);
if (tx_array[18] == 32 && !center) tx_array[18] = 28; // set last char to 28 if not center
tx_array[19] = (char)checksum((uint8_t*)tx_array);
sendRawMsg(tx_array);
}
void VW2002FISWriter::write_text_full(int x, int y, String line) {
line.toUpperCase();
tx_array[0] = 0x56; // command to set text-display in FIS
tx_array[1] = line.length() + 4; // Length of this message (command and this length not counted
tx_array[2] = 0x26; // unsure what this is (was 26)
tx_array[3] = x;
tx_array[4] = y;
line.toCharArray(&tx_array[5], line.length() + 1);
tx_array[line.length() + 5] = (char)checksum((uint8_t*)tx_array);
sendRawMsg(tx_array);
}
void VW2002FISWriter::GraphicOut(uint8_t x, uint8_t y, uint16_t size, uint8_t data[], uint8_t mode, uint8_t offset) {
uint8_t myArray[size + 5];
tx_array[0] = 0x55;
tx_array[1] = size + 4;
tx_array[2] = 0x81;
tx_array[3] = x;
tx_array[4] = y;
for (uint16_t a = 0; a < size; a++) {
tx_array[a + 5] = data[offset + a];
}
Serial.println(size);
tx_array[offset+size+5] = (char)checksum((uint8_t*)tx_array);
sendRawMsg(tx_array);
}
//fisWriter.GraphicFromArray(0,70,64,16,QBSW,1);
void VW2002FISWriter::GraphicFromArray(uint8_t x, uint8_t y, uint8_t sizex, uint8_t sizey, uint8_t data[], uint8_t mode) {
// 22x32bytes = 704
uint8_t packet_size = (sizex + 7) / 8; // how much byte per packet
//Serial.println(packet_size);
for (uint8_t line = 0; line < sizey; line++) {
//Serial.println(line);
uint8_t _data[packet_size];
for (uint8_t i = 0; i < packet_size; i++) {
_data[i] = data[(line * packet_size) + i];
}
GraphicOut(x, line + y, packet_size, _data, mode, 0);
delay(5);
}
}
void VW2002FISWriter::init_graphic() {
tx_array[0] = 0x53; // command to set text-display in FIS
tx_array[1] = 6; // Length of this message (command and this length not counted
tx_array[2] = 0x82; // unsure what this is
//0x80,0x81 - screen initialization without cleaning
//0x82 - screen initialization with clearing, positive screen
//0x83 - screen initialization with clearing, negative screen
tx_array[3] = 0x00;
tx_array[4] = 0x00;
tx_array[5] = 0x40;
tx_array[6] = 0x58;
tx_array[7] = (char)checksum((uint8_t*)tx_array);
sendRawMsg(tx_array);
}
void VW2002FISWriter::remove_graphic() {
tx_array[0] = 0x53; // command to set text-display in FIS
tx_array[1] = 6; // Length of this message (command and this length not counted
tx_array[2] = 0x80; // unsure what this is
tx_array[3] = 0x00;
tx_array[4] = 0x00;
tx_array[5] = 0x01;
tx_array[6] = 0x01;
tx_array[7] = (char)checksum((uint8_t*)tx_array);
sendRawMsg(tx_array);
}
void VW2002FISWriter::sendMsg(char msg[]) {
// build tx_array
tx_array[0] = 0x81; // command to set text-display in FIS
tx_array[1] = 18; // Length of this message (command and this length not counted
tx_array[2] = 0xF0; // unsure what this is
for (int i = 0; i < 16; i++) { // TODO: use memcpy
tx_array[3 + i] = msg[i];
}
tx_array[19] = (char)checksum((uint8_t*)tx_array);
sendRawMsg(tx_array);
}
//Prepare and send Text-Message
void VW2002FISWriter::sendRawMsg(char in_msg[]) {
FIS_WRITE_send_3LB_msg(in_msg);
}
// Send Text-Message out on 3LB port to instrument cluster
void VW2002FISWriter::FIS_WRITE_send_3LB_msg(char in_msg[]) {
uint16_t timeout_us;
#ifdef ENABLE_IRQ
cli();
#endif
sendEnablePulse();
// Send FIS-command
FIS_WRITE_3LB_sendByte(in_msg[FIS_MSG_COMMAND]);
setDataHigh();
byte msg_length = in_msg[FIS_MSG_LENGTH];
byte msg_end = msg_length + 1;
// Step 2 - wait for response from cluster to set ENA-High
timeout_us = 1500;
while (!digitalRead(_FIS_WRITE_ENA) && timeout_us > 0) {
delayMicroseconds(20);
timeout_us -= 20;
}
uint8_t crc = 0;
for (int i = 1; i <= msg_end; i++)
{
// Step 9.2 - ENA-Low detected
if (i > 1) {
setDataLow();
}
delayMicroseconds(40);
FIS_WRITE_3LB_sendByte(in_msg[i]);
////Serial.println(in_msg[i]); ///////////////////////////////////////////////
setDataLow();
// Step 10.2 - wait for response from cluster to set ENA-High
timeout_us = 1500;
while (!digitalRead(_FIS_WRITE_ENA) && timeout_us > 0) {
delayMicroseconds(1);
timeout_us -= 1;
}
}
// Step 9.5 - ENA-Low detected
setDataLow();
#ifdef ENABLE_IRQ
sei();
#endif
}
// Send Keep-Alive message
void VW2002FISWriter::sendKeepAliveMsg() {
delay(100);
FIS_WRITE_send_3LB_singleByteCommand(0xC3);
delay(100);
}
void VW2002FISWriter::displayOff() {
sendRawMsg((char*)off);
}
void VW2002FISWriter::displayBlank() {
sendRawMsg((char*)blank);
}
///Send Single-Command out on 3LB port to instrument cluster
void VW2002FISWriter::FIS_WRITE_send_3LB_singleByteCommand(uint8_t txByte) {
#ifdef ENABLE_IRQ
cli();
#endif
sendEnablePulse();
// Send FIS-command
FIS_WRITE_3LB_sendByte(txByte);
delayMicroseconds(30);
setDataLow();
#ifdef ENABLE_IRQ
sei();
#endif
}
//Send 3LB Enable pulse
void VW2002FISWriter::sendEnablePulse() {
FIS_WRITE_startENA();
delayMicroseconds(41);
FIS_WRITE_stopENA();
delayMicroseconds(37);
} // sendEnablePulse
void VW2002FISWriter::printFreeMem() {
//Serial.print(F("FIS:freeMemory()="));
////Serial.println(freeMemory());
}
// Send byte out on 3LB port to instrument cluster
void VW2002FISWriter::FIS_WRITE_3LB_sendByte(int in_byte) {
int tx_byte = 0xff - in_byte;
for (int i = 7; i >= 0; i--) {
setClockLow();
switch ((tx_byte & (1 << i)) > 0 ) {
case 1: setDataHigh();
break;
case 0: setDataLow();
break;
}
delayMicroseconds(FIS_WRITE_PULSEW / 1);
setClockHigh();
delayMicroseconds(FIS_WRITE_PULSEW * 2);
}
}
void VW2002FISWriter::FIS_WRITE_startENA() {
pinMode(_FIS_WRITE_ENA, OUTPUT);
digitalWrite(_FIS_WRITE_ENA, HIGH);
}
void VW2002FISWriter::FIS_WRITE_stopENA() {
digitalWrite(_FIS_WRITE_ENA, LOW);
pinMode(_FIS_WRITE_ENA, INPUT);
}
void VW2002FISWriter::setClockHigh() {
digitalWrite(_FIS_WRITE_CLK, HIGH);
}
void VW2002FISWriter::setClockLow() {
digitalWrite(_FIS_WRITE_CLK, LOW);
}
void VW2002FISWriter::setDataHigh() {
digitalWrite(_FIS_WRITE_DATA, HIGH);
}
void VW2002FISWriter::setDataLow() {
digitalWrite(_FIS_WRITE_DATA, LOW);
}
uint8_t VW2002FISWriter::checksum( volatile uint8_t in_msg[]) {
uint8_t crc = in_msg[0];
////Serial.println(in_msg[1]);
for (int i = 1; i < in_msg[1] + 1; i++)
{
crc ^= in_msg[i];
}
crc -= 1;
return crc;
}