src/main.cpp

changeset 35
b2dca6b935bb
parent 34
94ab7ff42a1b
child 36
a6c7292742a0
equal deleted inserted replaced
34:94ab7ff42a1b 35:b2dca6b935bb
34 Timeout splashscreen_timer; 34 Timeout splashscreen_timer;
35 InterruptIn rst(HP_RST); 35 InterruptIn rst(HP_RST);
36 36
37 QEI qenc(KP_ENC1, KP_ENC2, NC, 16); 37 QEI qenc(KP_ENC1, KP_ENC2, NC, 16);
38 volatile uint8_t knob; 38 volatile uint8_t knob;
39 bool shift; // true when kp is shifted, cleared by command 0x01 from Unit
39 volatile bool splashscreen; 40 volatile bool splashscreen;
40 41
41 42
42 typedef enum { 43 typedef enum {
43 KEY_NONE=0, 44 KEY_NONE=0,
44 KEY_PRESSED, 45 KEY_PRESSED,
45 KEY_RELEASED 46 KEY_RELEASED
46 } key_event_t; 47 } key_event_t;
47 48
48 typedef struct keycode { 49 typedef struct keycode {
49 uint8_t row; 50 uint8_t row;
50 uint8_t col; 51 uint8_t col;
51 key_event_t keyevent; 52 key_event_t keyevent;
52 } keycode_t; 53 } keycode_t;
53 54
54 //volatile keycode_t cur_keycode; 55 //volatile keycode_t cur_keycode;
55 56
56 #define KEY_BUF_SIZE 10 57 #define KEY_BUF_SIZE 10
57 CircularBuffer<keycode_t, KEY_BUF_SIZE> key_buf; 58 CircularBuffer<keycode_t, KEY_BUF_SIZE> key_buf;
58 59
59 #define KP_NROWS 4 60 #define KP_NROWS 4
60 #define KP_NCOLS 5 61 #define KP_NCOLS 5
61 DigitalIn kp_rows[KP_NROWS] = { 62 DigitalIn kp_rows[KP_NROWS] = {
62 KP_R0, 63 KP_R0,
63 KP_R1, 64 KP_R1,
64 KP_R2, 65 KP_R2,
65 KP_R3 66 KP_R3
66 }; 67 };
67 68
68 DigitalOut kp_columns[KP_NCOLS] = { 69 DigitalOut kp_columns[KP_NCOLS] = {
69 KP_C0, KP_C1, 70 KP_C0, KP_C1,
70 KP_C2, KP_C3, 71 KP_C2, KP_C3,
71 KP_C4 72 KP_C4
72 }; 73 };
73 74
74 /* mapping (RxC) 75 /* mapping (RxC)
75 0x2 0x00: View 76 0x2 0x00: View
76 0x1 0x01: Mon 77 0x1 0x01: Mon
77 3x3 0x02: Sto/Rcl 78 3x3 0x02: Sto/Rcl
78 0x0 0x03: Scan 79 0x0 0x03: Scan
79 1x2 0x04: Alarm 80 1x2 0x04: Alarm
80 1x1 0x05: Mx+B 81 1x1 0x05: Mx+B
81 1x0 0x06: Measure 82 1x0 0x06: Measure
82 2x0 0x07: Interval 83 2x0 0x07: Interval
83 3x2 0x08: Card Reset 84 3x2 0x08: Card Reset
84 3x1 0x09: Close 85 3x1 0x09: Close
85 3x0 0x0A: Open 86 3x0 0x0A: Open
86 0x3 0x0B: Read 87 0x3 0x0B: Read
87 2x3 0x0C: Shift 88 2x3 0x0C: Shift
88 1x3 0x0D: Write 89 1x3 0x0D: Write
89 0x4 0x0E: Left 90 0x4 0x0E: Left
90 1x4 0x0F: Right 91 1x4 0x0F: Right
91 2x2 0x10: Advanced 92 2x2 0x10: Advanced
92 2x1 0x11: Step 93 2x1 0x11: Step
93 */ 94 */
94 95
95 uint8_t kp_mapping[KP_NROWS][KP_NCOLS] = { // [row][column] 96 uint8_t kp_mapping[KP_NROWS][KP_NCOLS] = { // [row][column]
96 {0x03, 0x01, 0x00, 0x0B, 0x0E}, 97 {0x03, 0x01, 0x00, 0x0B, 0x0E},
97 {0x06, 0x05, 0x04, 0x0D, 0x0F}, 98 {0x06, 0x05, 0x04, 0x0D, 0x0F},
98 {0x07, 0x11, 0x10, 0x0C, 0xFF}, 99 {0x07, 0x11, 0x10, 0x0C, 0xFF},
99 {0x0A, 0x09, 0x08, 0x02, 0xFF} 100 {0x0A, 0x09, 0x08, 0x02, 0xFF}
100 }; 101 };
101 102
102 103
103 void kp_cb(uint8_t row, uint8_t col); 104 void kp_cb(uint8_t row, uint8_t col);
104 void kr_cb(uint8_t row, uint8_t col); 105 void kr_cb(uint8_t row, uint8_t col);
109 uint8_t nchars; 110 uint8_t nchars;
110 char buffer[MAX_BUFF+1]; 111 char buffer[MAX_BUFF+1];
111 112
112 void timeout_h() { 113 void timeout_h() {
113 #if defined(HAS_LED) 114 #if defined(HAS_LED)
114 led = !led; 115 led = !led;
115 #endif 116 #endif
116 } 117 }
117 118
118 typedef struct _DSP 119 typedef struct _DSP
119 { 120 {
120 uint8_t cmd; 121 uint8_t cmd;
121 uint8_t color; 122 uint8_t color;
122 uint8_t bgcolor; 123 uint8_t bgcolor;
123 uint8_t x0; 124 uint8_t x0;
124 uint8_t y0; 125 uint8_t y0;
125 uint8_t fmt; // 0x01=>ascii, 0x02=>hex, 0x04=>bits, 0x08=>flags, 0x80=>ignore 126 uint8_t fmt; // 0x01=>ascii, 0x02=>hex, 0x04=>bits, 0x08=>flags, 0x80=>ignore
126 uint8_t maxsize; 127 uint8_t maxsize;
127 uint8_t width; 128 uint8_t width;
128 const unsigned char* font; 129 const unsigned char* font;
129 char buffer[MAX_BUFF+1]; 130 char buffer[MAX_BUFF+1];
130 } DSP; 131 } DSP;
131 132
132 static DSP table[] = 133 static DSP table[] =
133 { 134 {
134 { 0x00, 0xF, 0x0, 0, 0, 0x01, MAX_BUFF, 245, Mono19x27}, // main display 135 { 0x00, 0xF, 0x0, 0, 0, 0x01, MAX_BUFF, 245, Mono19x27}, // main display
135 { 0x0C, 0xF, 0x0,196, 34, 0x01, 3, 45, Mono15x22}, // channels display 136 { 0x0C, 0xF, 0x0,196, 34, 0x01, 3, 45, Mono15x22}, // channels display
136 { 0x0A, 0xF, 0x0, 0, 57, 0x08, 4, 0, Terminal6x8}, // flags + bits 137 { 0x0A, 0xF, 0x0, 0, 57, 0x08, 4, 0, Terminal6x8}, // flags + bits
137 }; 138 };
138 139
139 // 9x10 140 // 9x10
140 const unsigned char icon_alarm[] __attribute__((aligned (2))) = 141 const unsigned char icon_alarm[] __attribute__((aligned (2))) =
141 { 142 {
142 0x1c, 0x0, 143 0x1c, 0x0,
143 0x3e, 0x0, 144 0x3e, 0x0,
144 0x7f, 0x0, 145 0x7f, 0x0,
145 0x7f, 0x0, 146 0x7f, 0x0,
146 0x7f, 0x0, 147 0x7f, 0x0,
147 0x7f, 0x0, 148 0x7f, 0x0,
148 0x7f, 0x0, 149 0x7f, 0x0,
149 0x7f, 0x0, 150 0x7f, 0x0,
150 0xff, 0x80, 151 0xff, 0x80,
151 0x10, 0x0 152 0x10, 0x0
152 }; 153 };
153 154
154 const unsigned char icon_curve[] __attribute__((aligned (2))) = 155 const unsigned char icon_curve[] __attribute__((aligned (2))) =
155 { 156 {
156 0x80, 0x0, 157 0x80, 0x0,
157 0x80, 0x0, 158 0x80, 0x0,
158 0x80, 0x80, 159 0x80, 0x80,
159 0x81, 0x0, 160 0x81, 0x0,
160 0x9e, 0x0, 161 0x9e, 0x0,
161 0xa0, 0x0, 162 0xa0, 0x0,
162 0xc0, 0x0, 163 0xc0, 0x0,
163 0x80, 0x0, 164 0x80, 0x0,
164 0x80, 0x0, 165 0x80, 0x0,
165 0xff, 0x80 166 0xff, 0x80
166 }; 167 };
167 168
168 typedef struct _FLAG 169 typedef struct _FLAG
169 { 170 {
170 uint8_t flag; 171 uint8_t flag;
171 uint8_t x; 172 uint8_t x;
172 uint8_t y; 173 uint8_t y;
173 bool reverse; 174 bool reverse;
174 const char* msg; 175 const char* msg;
175 const unsigned char* icon; 176 const unsigned char* icon;
176 } FLAG; 177 } FLAG;
177 178
178 typedef struct _FRAME 179 typedef struct _FRAME
179 { 180 {
180 uint16_t flag; 181 uint16_t flag;
181 uint8_t x0; 182 uint8_t x0;
182 uint8_t y0; 183 uint8_t y0;
183 uint8_t x1; 184 uint8_t x1;
184 uint8_t y1; 185 uint8_t y1;
185 } FRAME; 186 } FRAME;
186 187
187 static const FLAG flags[] = 188 static const FLAG flags[] =
188 { 189 {
189 // flag, zone, x0, y0, reverse, msg, icon 190 // flag, zone, x0, y0, reverse, msg, icon
190 // right-side icons area 191 // right-side icons area
191 { 0x00, 246, 0, false, NULL, icon_alarm}, // F1.0 192 { 0x00, 246, 0, false, NULL, icon_alarm}, // F1.0
192 { 0x01, 246, 11, false, NULL, icon_curve}, // F1.1 193 { 0x01, 246, 11, false, NULL, icon_curve}, // F1.1
193 194
194 // F1.2 == Channel frame 195 // F1.2 == Channel frame
195 { 0x03, 197, 27, false, "Channel"}, // F1.3 196 { 0x03, 197, 27, false, "Channel"}, // F1.3
196 // F1.7 == Alarm frame 197 // F1.7 == Alarm frame
225 { 0x10, 154+12, 17+20, false, "2"}, // F2.0 226 { 0x10, 154+12, 17+20, false, "2"}, // F2.0
226 { 0x12, 154+18, 17+20, false, "3"}, // F2.2 227 { 0x12, 154+18, 17+20, false, "3"}, // F2.2
227 { 0x11, 154+24, 17+20, false, "4"}, // F2.1 228 { 0x11, 154+24, 17+20, false, "4"}, // F2.1
228 { 0x04, 154+30, 17+20, false, "L"}, // F1.4 229 { 0x04, 154+30, 17+20, false, "L"}, // F1.4
229 230
230 }; 231 };
231 232
232 static const FRAME zones[] = 233 static const FRAME zones[] =
233 { // flag, x0, y0, x1, y1 234 { // flag, x0, y0, x1, y1
234 { 0x001, 0, 0, 245, 27}, // main display area 235 { 0x001, 0, 0, 245, 27}, // main display area
235 { 0x002, 246, 0, 255, 27}, // right notif area 236 { 0x002, 246, 0, 255, 27}, // right notif area
240 { 0x040, 71, 28, 103, 54}, // flags col3 241 { 0x040, 71, 28, 103, 54}, // flags col3
241 { 0x080, 104, 28, 128, 54}, // flags col4 242 { 0x080, 104, 28, 128, 54}, // flags col4
242 { 0x100, 129, 28, 159, 54}, // flags col5 243 { 0x100, 129, 28, 159, 54}, // flags col5
243 244
244 // { 0x8000, 0, 55, 255, 63}, // flags bits display area 245 // { 0x8000, 0, 55, 255, 63}, // flags bits display area
245 }; 246 };
246 247
247 static const FRAME frames[] = 248 static const FRAME frames[] =
248 { 249 {
249 { 0x02, 194, 30, 243, 53}, // F1.2 - channel frame 250 { 0x02, 194, 30, 243, 53}, // F1.2 - channel frame
250 { 0x07, 151, 30, 192, 46}, // F1.7 - alarm frame 251 { 0x07, 151, 30, 192, 46}, // F1.7 - alarm frame
251 }; 252 };
252 253
253 254
254 #ifdef DEBUG 255 #ifdef DEBUG
255 256
256 DigitalOut dbgpin(DBGPIN); 257 DigitalOut dbgpin(DBGPIN);
257 inline void pulse(uint8_t count=1, bool stayup=false) 258 inline void pulse(uint8_t count=1, bool stayup=false)
258 { 259 {
259 dbgpin = 0; 260 dbgpin = 0;
260 wait_us(2); 261 wait_us(2);
261 while (count--) 262 while (count--)
262 { 263 {
263 dbgpin = 1; 264 dbgpin = 1;
264 wait_us(2); 265 wait_us(2);
265 dbgpin = 0; 266 dbgpin = 0;
266 wait_us(2); 267 wait_us(2);
267 } 268 }
268 if (stayup) 269 if (stayup)
269 dbgpin = 1; 270 dbgpin = 1;
270 } 271 }
271 #else 272 #else
272 inline void pulse(uint8_t count=1, bool stayup=false) 273 inline void pulse(uint8_t count=1, bool stayup=false)
273 {} 274 {}
274 #endif 275 #endif
282 void qei_cb(int dir); 283 void qei_cb(int dir);
283 void end_splashscreen(void); 284 void end_splashscreen(void);
284 void show(uint8_t, const char*, uint8_t); 285 void show(uint8_t, const char*, uint8_t);
285 286
286 /* 287 /*
287 #if defined(HAVE_PC) 288 #if defined(HAVE_PC)
288 FileHandle *mbed::mbed_override_console(int fd) 289 FileHandle *mbed::mbed_override_console(int fd)
289 { 290 {
290 return static_cast<FileHandle*> (&pc); 291 return static_cast<FileHandle*> (&pc);
291 } 292 }
292 #endif 293 #endif
293 */ 294 */
294 295
295 void setup() { 296 void setup() {
296 #if defined(HAVE_PC) 297 #if defined(HAVE_PC)
297 #if defined(TARGET_NUCLEO_F446RE) 298 #if defined(TARGET_NUCLEO_F446RE)
298 pc.set_baud(115200); 299 pc.set_baud(115200);
299 #endif 300 #endif
300 301
301 /* 302 /*
302 #if defined(TARGET_HP34970_FP_F303RD) 303 #if defined(TARGET_HP34970_FP_F303RD)
303 pc.init(); 304 pc.init();
304 pc.connect(); 305 pc.connect();
305 #endif 306 #endif
306 */ 307 */
307 #endif 308 #endif
308 309
309 printf("\n\nSETUP\n"); 310 printf("\n\nSETUP\n");
310 printf(" System Core Clock = %.3f MHZ\r\n", 311 printf(" System Core Clock = %.3f MHZ\r\n",
311 (float)SystemCoreClock/1000000); 312 (float)SystemCoreClock/1000000);
312 313
313 /* 314 /*
314 #if defined(HAS_LED) 315 #if defined(HAS_LED)
315 printf("Attaching Led 1: %d\n", LED1); 316 printf("Attaching Led 1: %d\n", LED1);
316 blinker.attach(callback(timeout_h), 0.5f); 317 blinker.attach(callback(timeout_h), 0.5f);
317 #endif 318 #endif
318 */ 319 */
319 320
320 hp = NULL; 321 hp = NULL;
321 #if defined(TARGET_NUCLEO_F446RE) 322 #if defined(TARGET_NUCLEO_F446RE)
322 printf("Serial communication pins\r\n"); 323 printf("Serial communication pins\r\n");
323 printf(" USBRX=%d\r\n", USBRX); 324 printf(" USBRX=%d\r\n", USBRX);
324 printf(" USBTX=%d\r\n", USBTX); 325 printf(" USBTX=%d\r\n", USBTX);
325 #endif 326 #endif
326 #if defined(TARGET_HP34970_FP_F303RD) 327 #if defined(TARGET_HP34970_FP_F303RD)
327 printf("Serial communication via USB\r\n"); 328 printf("Serial communication via USB\r\n");
328 #endif 329 #endif
329 330
330 printf("Setup HP communication pins\r\n"); 331 printf("Setup HP communication pins\r\n");
331 printf(" HP_RX=%d\r\n", HP_RX); 332 printf(" HP_RX=%d\r\n", HP_RX);
332 DigitalIn(HP_RX).mode(PullDown); 333 DigitalIn(HP_RX).mode(PullDown);
333 printf(" HP_TX=%d\r\n", HP_TX); 334 printf(" HP_TX=%d\r\n", HP_TX);
334 DigitalOut(HP_TX).write(1); 335 DigitalOut(HP_TX).write(1);
335 printf(" HP_RST=%d\r\n", HP_RST); 336 printf(" HP_RST=%d\r\n", HP_RST);
336 DigitalIn(HP_RST).mode(PullDown); 337 DigitalIn(HP_RST).mode(PullDown);
337 338
338 printf(" setup QEI pins\r\n"); 339 printf(" setup QEI pins\r\n");
339 printf(" ENC1=%d\r\n", KP_ENC1); 340 printf(" ENC1=%d\r\n", KP_ENC1);
340 DigitalIn(KP_ENC1).mode(PullDown); 341 DigitalIn(KP_ENC1).mode(PullDown);
341 printf(" ENC2=%d\r\n", KP_ENC2); 342 printf(" ENC2=%d\r\n", KP_ENC2);
342 DigitalIn(KP_ENC2).mode(PullDown); 343 DigitalIn(KP_ENC2).mode(PullDown);
343 qenc.attach(&qei_cb); 344 qenc.attach(&qei_cb);
344 345
345 printf(" setup Keypad\r\n"); 346 printf(" setup Keypad\r\n");
346 //cur_keycode.keyevent = KEY_NONE; 347 //cur_keycode.keyevent = KEY_NONE;
347 348
348 kpad = new Keypad(KP_NROWS, kp_rows, KP_NCOLS, kp_columns); 349 shift = false;
349 printf(" attach Keypad callbacks\r\n"); 350 kpad = new Keypad(KP_NROWS, kp_rows, KP_NCOLS, kp_columns);
350 kpad->attach(&kp_cb, &kr_cb); 351 printf(" attach Keypad callbacks\r\n");
351 printf(" start Keypad\r\n"); 352 kpad->attach(&kp_cb, &kr_cb);
352 kpad->start(); 353 printf(" start Keypad\r\n");
353 354 kpad->start();
354 printf("Setup OLED display\r\n"); 355
355 // init the LCD 356 printf("Setup OLED display\r\n");
356 printf(" DSP_MOSI=%d\r\n", DSP_MOSI); 357 // init the LCD
357 printf(" DSP_MISO=%d\r\n", DSP_MISO); 358 printf(" DSP_MOSI=%d\r\n", DSP_MOSI);
358 printf(" DSP_SCLK=%d\r\n", DSP_SCLK); 359 printf(" DSP_MISO=%d\r\n", DSP_MISO);
359 printf(" DSP_CS=%d\r\n", DSP_CS); 360 printf(" DSP_SCLK=%d\r\n", DSP_SCLK);
360 printf(" DSP_RST=%d\r\n", DSP_RST); 361 printf(" DSP_CS=%d\r\n", DSP_CS);
361 printf(" DSP_DC=%d\r\n", DSP_DC); 362 printf(" DSP_RST=%d\r\n", DSP_RST);
362 dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS, 363 printf(" DSP_DC=%d\r\n", DSP_DC);
363 DSP_RST, DSP_DC, "SSD1322"); 364 dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS,
364 365 DSP_RST, DSP_DC, "SSD1322");
365 printf(" configure DSP\r\n"); 366
366 dsp->background(Black); // set background to black 367 printf(" configure DSP\r\n");
367 dsp->foreground(0xF); 368 dsp->background(Black); // set background to black
368 dsp->cls(); 369 dsp->foreground(0xF);
369 370 dsp->cls();
370 //curcmd = 0xFF; 371
371 curchar = 0; 372 //curcmd = 0xFF;
372 nchars = 0; 373 curchar = 0;
373 374 nchars = 0;
374 for (uint8_t i=0; i<sizeof(table)/sizeof(table[0]); ++i) 375
375 memset(table[i].buffer, 0, MAX_BUFF+1); 376 for (uint8_t i=0; i<sizeof(table)/sizeof(table[0]); ++i)
376 377 memset(table[i].buffer, 0, MAX_BUFF+1);
377 printf(" display splash screen\r\n"); 378
378 dsp->locate(30, 10); 379 printf(" display splash screen\r\n");
379 dsp->set_font((unsigned char*)Mono19x27); 380 dsp->locate(30, 10);
380 dsp->printf("HP34970A"); 381 dsp->set_font((unsigned char*)Mono19x27);
381 dsp->set_font((unsigned char*)Terminal6x8); 382 dsp->printf("HP34970A");
382 dsp->locate(90, 40); 383 dsp->set_font((unsigned char*)Terminal6x8);
383 dsp->printf("David Douard"); 384 dsp->locate(90, 40);
384 dsp->locate(0, 52); 385 dsp->printf("David Douard");
385 dsp->printf("Clock = %d ", SystemCoreClock); 386 dsp->locate(0, 52);
386 387 dsp->printf("Clock = %d ", SystemCoreClock);
387 RCC_OscInitTypeDef cfg; 388
388 HAL_RCC_GetOscConfig(&cfg); 389 RCC_OscInitTypeDef cfg;
389 if (cfg.HSEState == RCC_HSE_BYPASS) 390 HAL_RCC_GetOscConfig(&cfg);
390 dsp->printf("HSE:EXT "); 391 if (cfg.HSEState == RCC_HSE_BYPASS)
391 else if (cfg.HSEState == RCC_HSE_ON) 392 dsp->printf("HSE:EXT ");
392 dsp->printf("HSE:XTAL "); 393 else if (cfg.HSEState == RCC_HSE_ON)
393 else 394 dsp->printf("HSE:XTAL ");
394 dsp->printf("HSE:OFF "); 395 else
395 396 dsp->printf("HSE:OFF ");
396 if (cfg.HSIState == RCC_HSI_ON) 397
397 dsp->printf("HSI:ON "); 398 if (cfg.HSIState == RCC_HSI_ON)
398 else 399 dsp->printf("HSI:ON ");
399 dsp->printf("HSI:OFF "); 400 else
400 401 dsp->printf("HSI:OFF ");
401 dsp->copy_to_lcd(); 402
402 403 dsp->copy_to_lcd();
403 printf("Starting LCD thread\r\n"); 404
404 tdsp.start(&copy_to_lcd); 405 printf("Starting LCD thread\r\n");
405 406 tdsp.start(&copy_to_lcd);
406 printf("Starting Event thread\r\n"); 407
407 tloop.start(&loop); 408 printf("Starting Event thread\r\n");
408 409 tloop.start(&loop);
409 /* 410
410 dsp->clrbuff(); 411 /*
411 show(0x00, "HH:MM:\tSS\t:mmmm", 15); // main dsp 412 dsp->clrbuff();
412 show(0x0C, "888", 3); // channel dsp 413 show(0x00, "HH:MM:\tSS\t:mmmm", 15); // main dsp
413 show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags 414 show(0x0C, "888", 3); // channel dsp
414 */ 415 show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags
415 416 */
416 printf("Attaching timers\r\n"); 417
417 splashscreen = true; 418 printf("Attaching timers\r\n");
418 splashscreen_timer.attach(callback(&end_splashscreen), 2); 419 splashscreen = true;
419 rst.fall(&reset_irq); 420 splashscreen_timer.attach(callback(&end_splashscreen), 2);
420 421 rst.fall(&reset_irq);
421 printf("SETUP DONE\r\n"); 422
423 printf("SETUP DONE\r\n");
422 } 424 }
423 425
424 void end_splashscreen(void) 426 void end_splashscreen(void)
425 { 427 {
426 // print is forbidden here because we are in an ISR context here 428 // print is forbidden here because we are in an ISR context here
427 //printf("End of splash screen CB\r\n"); 429 //printf("End of splash screen CB\r\n");
428 splashscreen = false; 430 splashscreen = false;
429 } 431 }
430 432
431 void reset_irq(void) 433 void reset_irq(void)
432 { 434 {
433 rst_delay.attach(callback(&reset), 0.1); 435 rst_delay.attach(callback(&reset), 0.1);
434 } 436 }
435 437
436 void reset(void) 438 void reset(void)
437 { 439 {
438 if (DigitalIn(HP_RST).read() == 0) { 440 if (DigitalIn(HP_RST).read() == 0) {
439 if (hp == NULL) { 441 if (hp == NULL) {
440 printf("setup HP communication handler\r\n"); 442 printf("setup HP communication handler\r\n");
441 hp = new HPSerial(HP_TX, HP_RX); 443 hp = new HPSerial(HP_TX, HP_RX);
442 } 444 }
443 445
444 printf("!! RST !! (gstate=%d, state=%d)\r\n", 446 printf("!! RST !! (gstate=%d, state=%d)\r\n",
445 hp->gstate(), hp->state()); 447 hp->gstate(), hp->state());
446 //printf("Value is ... %X\n", hp->search()); 448 //printf("Value is ... %X\n", hp->search());
447 hp->startup(); 449 hp->startup();
448 } 450 }
449 } 451 }
450 452
451 void copy_to_lcd(void) { 453 void copy_to_lcd(void) {
452 //uint8_t mask=1; 454 //uint8_t mask=1;
453 455
454 while(1) { 456 while(1) {
455 pulse(0, true); 457 pulse(0, true);
456 if ((splashscreen == false) && (must_refresh)) { 458 if ((splashscreen == false) && (must_refresh)) {
457 must_refresh = 0; 459 must_refresh = 0;
458 //Thread::wait(20); // give a bit of time for some more cmds 460 //Thread::wait(20); // give a bit of time for some more cmds
459 dsp->copy_to_lcd(); 461 dsp->copy_to_lcd();
460 } 462 }
461 463
462 /* 464 /*
463 if (must_refresh & mask) { 465 if (must_refresh & mask) {
464 for(uint8_t i=0; i<sizeof(zones)/sizeof(zones[0]); i++) 466 for(uint8_t i=0; i<sizeof(zones)/sizeof(zones[0]); i++)
465 if (zones[i].flag == mask) { 467 if (zones[i].flag == mask) {
466 dsp->copy_to_lcd(zones[i].x0/4, (zones[i].x1+3)/4, 468 dsp->copy_to_lcd(zones[i].x0/4, (zones[i].x1+3)/4,
467 zones[i].y0, zones[i].y1); 469 zones[i].y0, zones[i].y1);
468 must_refresh &= ~mask; 470 must_refresh &= ~mask;
469 break; 471 break;
470 } 472 }
471 } 473 }
472 mask = mask << 1; 474 mask = mask << 1;
473 if (mask == 0) { 475 if (mask == 0) {
474 mask = 1; 476 mask = 1;
475 } 477 }
476 */ 478 */
477 pulse(0, false); 479 pulse(0, false);
478 480
479 ThisThread::sleep_for(30); 481 ThisThread::sleep_for(30);
480 } 482 }
481 } 483 }
482 484
483 void show(uint8_t cmd, const char *intxt, uint8_t nchar=0) 485 void show(uint8_t cmd, const char *intxt, uint8_t nchar=0)
484 { 486 {
485 uint8_t i; 487 uint8_t i;
486 // uint8_t len; 488 // uint8_t len;
487 uint16_t bgcolor, fgcolor; 489 uint16_t bgcolor, fgcolor;
488 char *oldv; 490 char *oldv;
489 // char *txt; 491 // char *txt;
490 char *txtp; 492 char *txtp;
491 static char txt[256]; 493 static char txt[256];
492 494
493 495
494 if (cmd == 0xFF) // cls 496 if (cmd == 0xFF) // cls
495 { 497 {
496 dsp->clrbuff(); 498 dsp->clrbuff();
497 } 499 }
498 else 500 else
499 { 501 {
500 //txt = (char *)malloc(strlen(intxt)+1); 502 //txt = (char *)malloc(strlen(intxt)+1);
501 strcpy(txt, intxt); 503 strcpy(txt, intxt);
502 txtp = txt; 504 txtp = txt;
503 505
504 pulse(1, true); 506 pulse(1, true);
505 507
506 // len = MAX_BUFF; 508 // len = MAX_BUFF;
507 509
508 for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) { 510 for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) {
509 if (table[i].cmd == cmd) { 511 if (table[i].cmd == cmd) {
510 bgcolor = table[i].bgcolor; 512 bgcolor = table[i].bgcolor;
511 fgcolor = table[i].color; 513 fgcolor = table[i].color;
512 dsp->background(bgcolor); 514 dsp->background(bgcolor);
513 dsp->foreground(fgcolor); 515 dsp->foreground(fgcolor);
514 dsp->set_font((unsigned char*) table[i].font); 516 dsp->set_font((unsigned char*) table[i].font);
515 oldv = table[i].buffer; 517 oldv = table[i].buffer;
516 518
517 dsp->locate(table[i].x0, table[i].y0); 519 dsp->locate(table[i].x0, table[i].y0);
518 520
519 if (table[i].fmt & 0x01) // ASCII text 521 if (table[i].fmt & 0x01) // ASCII text
520 { 522 {
521 if (strncmp(oldv, txt, table[i].maxsize) != 0) 523 if (strncmp(oldv, txt, table[i].maxsize) != 0)
522 { 524 {
523 if (table[i].width > 0) 525 if (table[i].width > 0)
524 dsp->fillrect(table[i].x0, 526 dsp->fillrect(table[i].x0,
525 table[i].y0, 527 table[i].y0,
526 table[i].x0 + table[i].width, 528 table[i].x0 + table[i].width,
527 table[i].y0 + table[i].font[2], 529 table[i].y0 + table[i].font[2],
528 bgcolor); 530 bgcolor);
529 for (uint8_t k=0; ;k++) 531 for (uint8_t k=0; ;k++)
530 { 532 {
531 if (txtp[k] == 0x00) 533 if (txtp[k] == 0x00)
532 { 534 {
533 dsp->printf(txtp); 535 dsp->printf(txtp);
534 break; 536 break;
535 } 537 }
536 if (txtp[k] == 0x09) 538 if (txtp[k] == 0x09)
537 { // \t is a special char for 'unselected' display value 539 { // \t is a special char for 'unselected' display value
538 txtp[k] = 0x00; 540 txtp[k] = 0x00;
539 dsp->printf(txtp); 541 dsp->printf(txtp);
540 542
541 if (fgcolor == table[i].color) 543 if (fgcolor == table[i].color)
542 fgcolor /= 2; 544 fgcolor /= 2;
543 else 545 else
544 fgcolor = table[i].color; 546 fgcolor = table[i].color;
545 dsp->foreground(fgcolor); 547 dsp->foreground(fgcolor);
546 txtp = &(txtp[k+1]); 548 txtp = &(txtp[k+1]);
547 k = 0; 549 k = 0;
548 } 550 }
549 } 551 }
550 if (cmd == 0x00) // main area 552 if (cmd == 0x00) // main area
551 must_refresh |= 0x01; 553 must_refresh |= 0x01;
552 if (cmd == 0x0C) // channels area 554 if (cmd == 0x0C) // channels area
553 must_refresh |= 0x04; 555 must_refresh |= 0x04;
554 } 556 }
555 } 557 }
556 /* 558 /*
557 if (table[i].fmt & 0x02 ) { 559 if (table[i].fmt & 0x02 ) {
558 // hex 560 // hex
559 for (uint8_t j=0;; j++) { 561 for (uint8_t j=0;; j++) {
560 if (txt[j] == 0x00) 562 if (txt[j] == 0x00)
561 break; 563 break;
562 dsp->printf("%02X ", txt[j]); 564 dsp->printf("%02X ", txt[j]);
563 } 565 }
564 for (uint8_t j=3*strlen(txt); j<table[i].maxsize; j++) 566 for (uint8_t j=3*strlen(txt); j<table[i].maxsize; j++)
565 dsp->printf(" "); 567 dsp->printf(" ");
566 } 568 }
567 */ 569 */
568 if (table[i].fmt & 0x08 ) // flag indicators 570 if (table[i].fmt & 0x08 ) // flag indicators
569 { 571 {
570 // flags 572 // flags
571 for (uint8_t j=0; j<max(nchar, table[i].maxsize) ; j++) 573 for (uint8_t j=0; j<max(nchar, table[i].maxsize) ; j++)
572 { 574 {
573 for (uint8_t k=0; k<8; k++) 575 for (uint8_t k=0; k<8; k++)
574 { 576 {
575 if (1) 577 if (1)
576 { //(txt[j] & (1 << k) ) != (oldv[j] & (1 << k))) { 578 { //(txt[j] & (1 << k) ) != (oldv[j] & (1 << k))) {
577 for (uint8_t l=0; 579 for (uint8_t l=0;
578 l<(sizeof(flags)/sizeof(flags[0])); ++l) 580 l<(sizeof(flags)/sizeof(flags[0])); ++l)
579 { 581 {
580 if (flags[l].flag == ((j<<4) + k)) { 582 if (flags[l].flag == ((j<<4) + k)) {
581 if (txtp[j] & (1 << k)) 583 if (txtp[j] & (1 << k))
582 { 584 {
583 dsp->foreground(flags[l].reverse ? bgcolor : fgcolor); 585 dsp->foreground(flags[l].reverse ? bgcolor : fgcolor);
584 dsp->background(flags[l].reverse ? fgcolor : bgcolor); 586 dsp->background(flags[l].reverse ? fgcolor : bgcolor);
585 } 587 }
586 else 588 else
587 { 589 {
588 dsp->foreground(bgcolor); 590 dsp->foreground(bgcolor);
589 dsp->background(bgcolor); 591 dsp->background(bgcolor);
590 } 592 }
591 if (flags[l].msg != NULL) 593 if (flags[l].msg != NULL)
592 { // a string 594 { // a string
593 dsp->locate(flags[l].x, flags[l].y); 595 dsp->locate(flags[l].x, flags[l].y);
594 dsp->printf(flags[l].msg);} 596 dsp->printf(flags[l].msg);}
595 else 597 else
596 { // an icon 598 { // an icon
597 Bitmap_s pic = {9, 10, 2, (char*) flags[l].icon}; 599 Bitmap_s pic = {9, 10, 2, (char*) flags[l].icon};
598 dsp->Bitmap_BW(pic, flags[l].x, flags[l].y); 600 dsp->Bitmap_BW(pic, flags[l].x, flags[l].y);
599 } 601 }
600 must_refresh = 1; //|= zones[m].flag; 602 must_refresh = 1; //|= zones[m].flag;
601 break; 603 break;
602 } 604 }
603 } 605 }
604 } 606 }
605 } 607 }
606 } 608 }
607 609
608 // draw frames (Alarm and Channel) 610 // draw frames (Alarm and Channel)
609 for (uint8_t l=0; 611 for (uint8_t l=0;
610 l<(sizeof(frames)/sizeof(frames[0])); ++l) 612 l<(sizeof(frames)/sizeof(frames[0])); ++l)
611 { 613 {
612 uint16_t color; 614 uint16_t color;
613 if (frames[l].flag & txt[0]) // frame flags are on the 1st byte only 615 if (frames[l].flag & txt[0]) // frame flags are on the 1st byte only
614 color = fgcolor/6; 616 color = fgcolor/6;
615 else 617 else
616 color = bgcolor; 618 color = bgcolor;
617 dsp->hline(frames[l].x0+1, frames[l].x0+3, frames[l].y0, color); 619 dsp->hline(frames[l].x0+1, frames[l].x0+3, frames[l].y0, color);
618 dsp->hline(frames[l].x1-3, frames[l].x1-1, frames[l].y0, color); 620 dsp->hline(frames[l].x1-3, frames[l].x1-1, frames[l].y0, color);
619 dsp->hline(frames[l].x0+1, frames[l].x1-1, frames[l].y1, color); 621 dsp->hline(frames[l].x0+1, frames[l].x1-1, frames[l].y1, color);
620 622
621 dsp->vline(frames[l].x0, frames[l].y0+1, frames[l].y1-1, color); 623 dsp->vline(frames[l].x0, frames[l].y0+1, frames[l].y1-1, color);
622 dsp->vline(frames[l].x1, frames[l].y0+1, frames[l].y1-1, color); 624 dsp->vline(frames[l].x1, frames[l].y0+1, frames[l].y1-1, color);
623 } 625 }
624 } 626 }
625 627
626 for(uint8_t j=0; j<table[i].maxsize; j++) 628 for(uint8_t j=0; j<table[i].maxsize; j++)
627 oldv[j] = txt[j]; 629 oldv[j] = txt[j];
628 break; 630 break;
629 } 631 }
630 } 632 }
631 //free(txt); 633 //free(txt);
632 //dsp->copy_to_lcd(); 634 //dsp->copy_to_lcd();
633 pulse(1, false); 635 pulse(1, false);
634 } 636 }
635 } 637 }
636 638
637 void test_dsp() 639 void test_dsp()
638 { 640 {
639 const FRAME *z; 641 const FRAME *z;
640 printf("TEST DSP\r\n"); 642 printf("TEST DSP\r\n");
641 dsp->cls(); 643 dsp->cls();
642 printf("TEST DSP #2\r\n"); 644 printf("TEST DSP #2\r\n");
643 show(0x00, "8g8g8g8g8g8g8", 13); // main dsp 645 show(0x00, "8g8g8g8g8g8g8", 13); // main dsp
644 show(0x0C, "888", 3); // channel dsp 646 show(0x0C, "888", 3); // channel dsp
645 show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags 647 show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags
646 dsp->copy_to_lcd(); 648 dsp->copy_to_lcd();
647 ThisThread::sleep_for(3); 649 ThisThread::sleep_for(3);
648 dsp->cls(); 650 dsp->cls();
649 printf("TEST DSP #3\r\n"); 651 printf("TEST DSP #3\r\n");
650 652
651 for (uint8_t i=0; i<(sizeof(zones)/sizeof(zones[0])); i++) 653 for (uint8_t i=0; i<(sizeof(zones)/sizeof(zones[0])); i++)
652 { 654 {
653 z = &zones[i]; 655 z = &zones[i];
654 dsp->fillrect(z->x0, z->y0, z->x1, z->y1, 4+i); 656 dsp->fillrect(z->x0, z->y0, z->x1, z->y1, 4+i);
655 dsp->locate(z->x0+1, z->y0+1); 657 dsp->locate(z->x0+1, z->y0+1);
656 dsp->printf("%d", i); 658 dsp->printf("%d", i);
657 } 659 }
658 660
659 /* 661 /*
660 for (uint8_t i=0; i<(sizeof(zones)/sizeof(zones[0])); i++) 662 for (uint8_t i=0; i<(sizeof(zones)/sizeof(zones[0])); i++)
661 { 663 {
662 z = &zones[i]; 664 z = &zones[i];
663 printf("Zone %d [%x]: %d, %d, %d, %d\n", i, z->flag, 665 printf("Zone %d [%x]: %d, %d, %d, %d\n", i, z->flag,
664 z->x0, z->y0, z->x1, z->y1); 666 z->x0, z->y0, z->x1, z->y1);
665 must_refresh = z->flag; 667 must_refresh = z->flag;
666 wait(1); 668 wait(1);
667 } 669 }
668 printf("Done\n"); 670 printf("Done\n");
669 wait(2); 671 wait(2);
670 printf("Copy ALL\n"); 672 printf("Copy ALL\n");
671 dsp->copy_to_lcd(); 673 dsp->copy_to_lcd();
672 */ 674 */
673 ThisThread::sleep_for(2); 675 ThisThread::sleep_for(2);
674 dsp->cls(); 676 dsp->cls();
675 printf("TEST DSP DONE\r\n"); 677 printf("TEST DSP DONE\r\n");
676 } 678 }
677 679
678 680
679 void loop() 681 void loop()
680 { // run over and over 682 { // run over and over
681 keycode_t key; 683 keycode_t key;
682 684
683 unsigned int err[8]; 685 unsigned int err[8];
684 for (uint8_t i=0; i<8; i++) 686 for (uint8_t i=0; i<8; i++)
685 err[i] = 0; 687 err[i] = 0;
686 int p, pp; // rot encoder pulse counters 688 int p, pp; // rot encoder pulse counters
687 p = 0; 689 p = 0;
688 pp = 0; 690 pp = 0;
689 691
690 while(1) { 692 while(1) {
691 p = qenc.getPulses(); 693 p = qenc.getPulses();
692 if (p != pp) 694 if (p != pp)
693 { 695 {
694 dsp->locate(0, 0); 696 dsp->locate(0, 0);
695 dsp->printf("Pulses = %d ", p); 697 dsp->printf("Pulses = %d ", p);
696 dsp->copy_to_lcd(); 698 dsp->copy_to_lcd();
697 pp = p; 699 pp = p;
698 } 700 }
699 701
700 if (knob != 0) 702 if (knob != 0)
701 { 703 {
702 if (hp != NULL) 704 if (hp != NULL)
703 { 705 {
704 printf("Sending keycode %X\r\n", knob); 706 printf("Sending keycode %X\r\n", knob);
705 hp->sendkey(knob); 707 hp->sendkey(knob);
706 printf(" DONE\r\n"); 708 printf(" DONE\r\n");
707 } 709 }
708 else 710 else
709 { 711 {
710 dsp->locate(70, 0); 712 dsp->locate(70, 0);
711 dsp->printf("Knob = %X ", knob); 713 dsp->printf("Knob = %X ", knob);
712 dsp->copy_to_lcd(); 714 dsp->copy_to_lcd();
713 } 715 }
714 knob = 0; 716 knob = 0;
715 } 717 }
716 718
717 if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE) 719 if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE)
720 {
721 key_buf.pop(key);
722 printf("Keycode %dx%d: %s\r\n",
723 key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released");
724 if (hp != NULL) {
725 uint8_t keycode = kp_mapping[key.row][key.col];
726 if (key.keyevent == KEY_RELEASED)
727 keycode |= 0x40; // bit 6: key relased
728 if (shift)
729 keycode |= 0x20; // bit 5: key shifted
730
731 hp->sendkey(kp_mapping[key.row][key.col]);
732 }
733 else
734 {
735 dsp->locate(140, 0);
736 dsp->printf("KC: %dx%d[0x%s%X] %s",
737 key.row, key.col,
738 kp_mapping[key.row][key.col] <= 0x0F ? "0" : "",
739 kp_mapping[key.row][key.col],
740 key.keyevent==KEY_PRESSED ? "PRE" : "REL");
741 dsp->copy_to_lcd();
742 }
743 // cur_keycode.keyevent = KEY_NONE;
744 }
745
746
747 if ((hp != NULL) && (hp->cmd_available()))
748 {
749 HPSerial::CMD cmd;
750 if (hp->pop(cmd))
751 {
752 #if defined(HAS_LED)
753 led = 1;
754 #endif
755 for (uint8_t i=0; i<7; i++)
756 if (hp->nerrors(i) > err[i]) {
757 printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n",
758 hp->nerrors(0),
759 hp->nerrors(1),
760 hp->nerrors(2),
761 hp->nerrors(3),
762 hp->nerrors(4),
763 hp->nerrors(5),
764 hp->nerrors(6)
765 );
766 break;
767 }
768 for (uint8_t i=0; i<7; i++)
769 err[i] = hp->nerrors(i);
770
771 printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd);
772
773 if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C))
774 printf(": '%s'\r\n", cmd.value);
775 else {
776 printf(":");
777 for (uint8_t i=0; i<cmd.size; i++)
778 printf("%02x ", cmd.value[i]);
779 printf("\r\n");
780 }
781 show(cmd.cmd, cmd.value, cmd.size);
782 #if defined(HAS_LED)
783 led = 0;
784 #endif
785 }
786 }
787 //else
788 ThisThread::sleep_for(1);
789 }
790 }
791
792 void qei_cb(int dir)
793 {
794 if(dir == 1) // turn right
795 knob = 0x80;
796 else // turn left
797 knob = 0x81; // 83?
798 }
799
800 void kp_cb(uint8_t row, uint8_t col)
801 {
802 keycode_t key;
803 key.row = row;
804 key.col = col;
805 key.keyevent = KEY_PRESSED;
806 if(!key_buf.full())
807 key_buf.push(key);
808 }
809
810 void kr_cb(uint8_t row, uint8_t col)
811 {
812 keycode_t key;
813 key.row = row;
814 key.col = col;
815 key.keyevent = KEY_RELEASED;
816 if(!key_buf.full())
817 key_buf.push(key);
818 }
819
820 int main()
821 {
822 setup();
823 printf("Main loop (noop)\r\n");
824 while(1)
718 { 825 {
719 key_buf.pop(key); 826 timeout_h();
720 printf("Keycode %dx%d: %s\r\n", 827 ThisThread::sleep_for(1);
721 key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released"); 828 }
722 if (hp != NULL) { 829 }
723 hp->sendkey(kp_mapping[key.row][key.col]);
724 }
725 else
726 {
727 dsp->locate(140, 0);
728 dsp->printf("KC: %dx%d[%X] %s",
729 key.row, key.col, kp_mapping[key.row][key.col],
730 key.keyevent==KEY_PRESSED?"PRE":"REL");
731 dsp->copy_to_lcd();
732 }
733 // cur_keycode.keyevent = KEY_NONE;
734 }
735
736
737 if ((hp != NULL) && (hp->cmd_available()))
738 {
739 HPSerial::CMD cmd;
740 if (hp->pop(cmd))
741 {
742 #if defined(HAS_LED)
743 led = 1;
744 #endif
745 for (uint8_t i=0; i<7; i++)
746 if (hp->nerrors(i) > err[i]) {
747 printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n",
748 hp->nerrors(0),
749 hp->nerrors(1),
750 hp->nerrors(2),
751 hp->nerrors(3),
752 hp->nerrors(4),
753 hp->nerrors(5),
754 hp->nerrors(6)
755 );
756 break;
757 }
758 for (uint8_t i=0; i<7; i++)
759 err[i] = hp->nerrors(i);
760
761 printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd);
762
763 if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C))
764 printf(": '%s'\r\n", cmd.value);
765 else {
766 printf(":");
767 for (uint8_t i=0; i<cmd.size; i++)
768 printf("%02x ", cmd.value[i]);
769 printf("\r\n");
770 }
771 show(cmd.cmd, cmd.value, cmd.size);
772 #if defined(HAS_LED)
773 led = 0;
774 #endif
775 }
776 }
777 //else
778 ThisThread::sleep_for(1);
779 }
780 }
781
782 void qei_cb(int dir)
783 {
784 if(dir == 1) // turn right
785 knob = 0x80;
786 else // turn left
787 knob = 0x81; // 83?
788 }
789
790 void kp_cb(uint8_t row, uint8_t col)
791 {
792 keycode_t key;
793 key.row = row;
794 key.col = col;
795 key.keyevent = KEY_PRESSED;
796 if(!key_buf.full())
797 key_buf.push(key);
798 }
799
800 void kr_cb(uint8_t row, uint8_t col)
801 {
802 keycode_t key;
803 key.row = row;
804 key.col = col;
805 key.keyevent = KEY_RELEASED;
806 if(!key_buf.full())
807 key_buf.push(key);
808 }
809
810 int main()
811 {
812 setup();
813 printf("Main loop (noop)\r\n");
814 while(1)
815 {
816 timeout_h();
817 ThisThread::sleep_for(1);
818 }
819 }

mercurial