diff -r c850674a3101 -r b3c3d54d2c7c src/main.cpp --- a/src/main.cpp Sun Nov 01 22:16:33 2020 +0100 +++ b/src/main.cpp Sun Nov 01 22:21:19 2020 +0100 @@ -23,15 +23,18 @@ Display *dsp; volatile bool splashscreen; HPSerial *hp; -Thread tdsp; -Ticker blinker; +Thread tdsp(osPriorityNormal, OS_STACK_SIZE, nullptr, "DSP"); +Ticker dsp_refresh; Timeout rst_delay; Timeout splashscreen_timer; +Timeout byescreen_timer; InterruptIn rst(HP_RST); QEI qenc(KP_ENC1, KP_ENC2, NC, 16); volatile uint8_t knob; bool shift; // true when kp is shifted, cleared by command 0x01 from Unit +bool must_reset; +bool must_shutdown; typedef enum { KEY_NONE=0, @@ -45,7 +48,6 @@ key_event_t keyevent; } keycode_t; -//volatile keycode_t cur_keycode; #define KEY_BUF_SIZE 10 CircularBuffer key_buf; @@ -94,10 +96,14 @@ {0x0A, 0x09, 0x08, 0x02, 0xFF} }; +#define KC_SHIFT 0x0C + void kp_cb(uint8_t row, uint8_t col); void kr_cb(uint8_t row, uint8_t col); Keypad *kpad; +keycode_t last_key = {0, 0, KEY_NONE}; + uint8_t curchar; //uint8_t curcmd; uint8_t nchars; @@ -139,21 +145,17 @@ void qei_cb(int dir); void end_splashscreen(void); -/* - #if defined(HAVE_PC) - FileHandle *mbed::mbed_override_console(int fd) - { - return static_cast (&pc); - } - #endif -*/ + +#if defined(HAVE_PC) +FileHandle *mbed::mbed_override_console(int fd) +{ + return static_cast (&pc); +} +#endif + void setup() { #if defined(HAVE_PC) -#if defined(TARGET_NUCLEO_F446RE) - pc.set_baud(115200); -#endif - /* #if defined(TARGET_HP34970_FP_F303RD) pc.init(); @@ -163,8 +165,7 @@ #endif printf("\n\nSETUP\n"); - printf(" System Core Clock = %.3f MHZ\r\n", - (float)SystemCoreClock/1000000); + printf(" System Core Clock = %ld MHZ\r\n", SystemCoreClock/1000000); /* #if defined(HAS_LED) @@ -199,9 +200,10 @@ qenc.attach(&qei_cb); printf(" setup Keypad\r\n"); - //cur_keycode.keyevent = KEY_NONE; shift = false; + must_reset = false; + must_shutdown = false; kpad = new Keypad(KP_NROWS, kp_rows, KP_NCOLS, kp_columns); printf(" attach Keypad callbacks\r\n"); kpad->attach(&kp_cb, &kr_cb); @@ -230,15 +232,10 @@ dsp->show_splashscreen(); printf("Starting LCD thread\r\n"); + // does not compile... + // dsp_refresh.attach(callback(dsp, &Display::copy_to_lcd), 50ms); tdsp.start(&refresh_display); - /* - dsp->clrbuff(); - show(0x00, "HH:MM:\tSS\t:mmmm", 15); // main dsp - show(0x0C, "888", 3); // channel dsp - show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags - */ - printf("Attaching timers\r\n"); splashscreen = true; splashscreen_timer.attach(callback(&end_splashscreen), 2ms); @@ -254,85 +251,86 @@ splashscreen = false; } +void reset_irq_tmr(void) +{ + must_reset = true; +} + void reset_irq(void) { - rst_delay.attach(callback(&reset), 1ms); + rst_delay.attach(callback(&reset_irq_tmr), 50ms); } void reset(void) { + // this should be called as a result of the HP_RST pin going LO + printf("Reset connection to the main unit\n"); if (DigitalIn(HP_RST).read() == 0) { if (hp == NULL) { printf("setup HP communication handler\r\n"); hp = new HPSerial(HP_TX, HP_RX); } + else { + printf("Connection already initialized\n"); + } printf("!! RST !! (gstate=%d, state=%d)\r\n", hp->gstate(), hp->state()); //printf("Value is ... %X\n", hp->search()); - hp->startup(); + dsp->power_on(); + dsp->cls(); + printf("Initiate startup sequence\n"); + if (last_key.keyevent == KEY_PRESSED) + hp->startup(kp_mapping[last_key.row][last_key.col]); + else + hp->startup(); + } + else + { + printf("HP_RST is not LOW, skipping\n"); } } void refresh_display(void) { - //uint8_t mask=1; - - while(1) { - pulse(0, true); - if (splashscreen == false) { - //Thread::wait(20); // give a bit of time for some more cmds - dsp->copy_to_lcd(); - } + while(1) { //!must_shutdown) { + dsp->copy_to_lcd(); + ThisThread::sleep_for(50ms); + } +} - /* - if (must_refresh & mask) { - for(uint8_t i=0; icopy_to_lcd(zones[i].x0/4, (zones[i].x1+3)/4, - zones[i].y0, zones[i].y1); - must_refresh &= ~mask; - break; - } - } - mask = mask << 1; - if (mask == 0) { - mask = 1; - } - */ - pulse(0, false); - - ThisThread::sleep_for(30ms); - } +void shutdown(void) +{ + must_shutdown = true; } void mainloop() { // run over and over keycode_t key = {0, 0, KEY_NONE}; - + uint8_t keycode; // actual sent value to the CPU + HPSerial::CMD cmd; + unsigned int nkeys = 0; unsigned int err[8]; for (uint8_t i=0; i<8; i++) err[i] = 0; - int p, pp; // rot encoder pulse counters - p = 0; - pp = 0; while(1) { - p = qenc.getPulses(); - if (p != pp) - { - dsp->locate(0, 0); - dsp->printf("Pulses = %d ", p); - dsp->copy_to_lcd(); - pp = p; - } + if (must_reset) { + must_reset = false; + reset(); + } + if (must_shutdown) { + //tdsp.join(); + //dsp_refresh.detach(); + must_shutdown = false; + dsp->power_off(); + } if (knob != 0) { if (hp != NULL) { - printf("Sending keycode %X\r\n", knob); + printf("KEY[%d] %s%X\n", nkeys++, knob<0x0F ? "0" : "", knob); hp->sendkey(knob); - printf(" DONE\r\n"); } else { @@ -343,19 +341,28 @@ knob = 0; } - if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE) + if (!key_buf.empty()) { key_buf.pop(key); - printf("Keycode %dx%d: %s\r\n", - key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released"); + keycode = kp_mapping[key.row][key.col]; + + if ((keycode == KC_SHIFT) && (key.keyevent == KEY_PRESSED)) + { + shift = true; + dsp->shift_on(); + } + if (hp != NULL) { - uint8_t keycode = kp_mapping[key.row][key.col]; + if ((shift) && (keycode != KC_SHIFT)) + { + keycode |= 0x20; // bit 5: key shifted + shift = false; + dsp->shift_off(); + } if (key.keyevent == KEY_RELEASED) keycode |= 0x40; // bit 6: key relased - if (shift) - keycode |= 0x20; // bit 5: key shifted - - hp->sendkey(kp_mapping[key.row][key.col]); + printf("KEY[%d] %s%X\n", nkeys++, keycode<0x0F ? "0" : "", keycode); + hp->sendkey(keycode); } else { @@ -367,13 +374,14 @@ key.keyevent==KEY_PRESSED ? "PRE" : "REL"); dsp->copy_to_lcd(); } - // cur_keycode.keyevent = KEY_NONE; } + // at each loop iteration, send buffered keycodes, if any + if (hp != NULL) + hp->send_key_when_idle(); if ((hp != NULL) && (hp->cmd_available())) { - HPSerial::CMD cmd; if (hp->pop(cmd)) { #if defined(HAS_LED) @@ -381,7 +389,7 @@ #endif for (uint8_t i=0; i<7; i++) if (hp->nerrors(i) > err[i]) { - printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n", + printf("ERR: %X/%X/%X/%X/%X/%X/%X\n", hp->nerrors(0), hp->nerrors(1), hp->nerrors(2), @@ -395,17 +403,17 @@ for (uint8_t i=0; i<7; i++) err[i] = hp->nerrors(i); - printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd); + printf("CMD[%d] %s%X", (int)cmd.id, cmd.cmd<0x10 ? "0" : "", cmd.cmd); // 0x00: main display // 0x0C: channel display if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C)) - printf(": '%s'\r\n", cmd.value); + printf(": '%s'\n", cmd.value); else { - printf(":"); + printf(": "); for (uint8_t i=0; ishift_off(); } } else if (cmd.cmd == 0x86) { - // shutdown - // TODO - } else { + // shutdown + dsp->show_byescreen(); + byescreen_timer.attach(callback(&shutdown), 2s); + } else { // display related commands dsp->show(cmd.cmd, cmd.value, cmd.size); } @@ -431,6 +441,7 @@ } } + void qei_cb(int dir) { if(dir == 1) // turn right @@ -441,22 +452,20 @@ void kp_cb(uint8_t row, uint8_t col) { - keycode_t key; - key.row = row; - key.col = col; - key.keyevent = KEY_PRESSED; + last_key.row = row; + last_key.col = col; + last_key.keyevent = KEY_PRESSED; if(!key_buf.full()) - key_buf.push(key); + key_buf.push(last_key); } void kr_cb(uint8_t row, uint8_t col) { - keycode_t key; - key.row = row; - key.col = col; - key.keyevent = KEY_RELEASED; + last_key.row = row; + last_key.col = col; + last_key.keyevent = KEY_RELEASED; if(!key_buf.full()) - key_buf.push(key); + key_buf.push(last_key); } int main()