voor willem test
Dependencies: 4DGL MODSERIAL mbed mbos
CDU2FS_message_4.cpp@7:6576a287e563, 2014-07-17 (annotated)
- Committer:
- LvdK
- Date:
- Thu Jul 17 10:09:14 2014 +0000
- Revision:
- 7:6576a287e563
CDU V21
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LvdK | 7:6576a287e563 | 1 | // L. van der Kolk, ELVEDEKA, Holland |
LvdK | 7:6576a287e563 | 2 | // File: CDU2FS_message_4.cpp |
LvdK | 7:6576a287e563 | 3 | // |
LvdK | 7:6576a287e563 | 4 | // -- Message handling from CDU to FS -- |
LvdK | 7:6576a287e563 | 5 | |
LvdK | 7:6576a287e563 | 6 | #include "mbed.h" |
LvdK | 7:6576a287e563 | 7 | #include "MODSERIAL.h" |
LvdK | 7:6576a287e563 | 8 | #include "keys.h" |
LvdK | 7:6576a287e563 | 9 | #include "debug_lvdk.h" // : debug mode control |
LvdK | 7:6576a287e563 | 10 | |
LvdK | 7:6576a287e563 | 11 | extern MODSERIAL USB; |
LvdK | 7:6576a287e563 | 12 | extern MODSERIAL SERIAL_DEBUG; |
LvdK | 7:6576a287e563 | 13 | extern int CDU_FS_interface; |
LvdK | 7:6576a287e563 | 14 | |
LvdK | 7:6576a287e563 | 15 | int key_hit_ID = 0; // : number of key that was hit, 0 = no hit of any key. ( global flag ) |
LvdK | 7:6576a287e563 | 16 | |
LvdK | 7:6576a287e563 | 17 | char key_message[25] = "$PCDUKEY,"; // : setup begin of KEY message to FS |
LvdK | 7:6576a287e563 | 18 | char alive_message[25] = "$PCDUOKE,"; // : setup begin of ALIVE message to FS |
LvdK | 7:6576a287e563 | 19 | |
LvdK | 7:6576a287e563 | 20 | |
LvdK | 7:6576a287e563 | 21 | void send_message_to_FS(char *message_string) { |
LvdK | 7:6576a287e563 | 22 | // Common fnction to send a created message string (KEY or OKE) to the FS. |
LvdK | 7:6576a287e563 | 23 | // Parameter is pointer to char string that has to be sent. |
LvdK | 7:6576a287e563 | 24 | // Interface can be USB port or Ethernet port. |
LvdK | 7:6576a287e563 | 25 | int i = 0; |
LvdK | 7:6576a287e563 | 26 | if ( CDU_FS_interface == 0 ) { // : messages will be sent out by USB port |
LvdK | 7:6576a287e563 | 27 | while ( message_string[i] != '\0' ) { |
LvdK | 7:6576a287e563 | 28 | USB.putc(message_string[i]); |
LvdK | 7:6576a287e563 | 29 | i++; |
LvdK | 7:6576a287e563 | 30 | } |
LvdK | 7:6576a287e563 | 31 | } |
LvdK | 7:6576a287e563 | 32 | if ( CDU_FS_interface == 1 ) { |
LvdK | 7:6576a287e563 | 33 | // Messages will be sent out by Ehternet interface |
LvdK | 7:6576a287e563 | 34 | // - Not implemented - |
LvdK | 7:6576a287e563 | 35 | } |
LvdK | 7:6576a287e563 | 36 | } |
LvdK | 7:6576a287e563 | 37 | |
LvdK | 7:6576a287e563 | 38 | |
LvdK | 7:6576a287e563 | 39 | void Send_ALIVE_message(int seconds){ |
LvdK | 7:6576a287e563 | 40 | int i; |
LvdK | 7:6576a287e563 | 41 | char byte_read; |
LvdK | 7:6576a287e563 | 42 | char exor_byte = 0; |
LvdK | 7:6576a287e563 | 43 | //Create alive message: |
LvdK | 7:6576a287e563 | 44 | i = 9; // : i points to first place after "$PCDUOKE," |
LvdK | 7:6576a287e563 | 45 | // Add seconds in 2 dec digits and a '*' char : |
LvdK | 7:6576a287e563 | 46 | sprintf(&alive_message[i],"%02d*",seconds); |
LvdK | 7:6576a287e563 | 47 | // Calculate checksum now : |
LvdK | 7:6576a287e563 | 48 | i = 1; // : i points to first place after '$' |
LvdK | 7:6576a287e563 | 49 | do { byte_read = alive_message[i]; |
LvdK | 7:6576a287e563 | 50 | if (byte_read == '*') break; // : exclude '*' from exor calculation |
LvdK | 7:6576a287e563 | 51 | exor_byte = exor_byte ^ byte_read; |
LvdK | 7:6576a287e563 | 52 | i++; |
LvdK | 7:6576a287e563 | 53 | } while ( i < 20 ); |
LvdK | 7:6576a287e563 | 54 | i++; // : i now points to first digit of checksum after '*' |
LvdK | 7:6576a287e563 | 55 | // Add exor_byte in 2 hex chars (with upper case A-F) and a CR + LF: |
LvdK | 7:6576a287e563 | 56 | sprintf(&alive_message[i],"%02X\r\n",exor_byte); // : + extra NULL char added by sprintf |
LvdK | 7:6576a287e563 | 57 | send_message_to_FS(alive_message); // : send message to defined CDU-FS interface |
LvdK | 7:6576a287e563 | 58 | |
LvdK | 7:6576a287e563 | 59 | } |
LvdK | 7:6576a287e563 | 60 | |
LvdK | 7:6576a287e563 | 61 | void Send_KEY_message(int key_nr) { |
LvdK | 7:6576a287e563 | 62 | // Function creates a valid KEY message out of key_nr parameter. |
LvdK | 7:6576a287e563 | 63 | // Based on key_nr, a key char string is looked up and added to the message. |
LvdK | 7:6576a287e563 | 64 | // After adding a checksum, the total KEY message will be sent. |
LvdK | 7:6576a287e563 | 65 | int i; |
LvdK | 7:6576a287e563 | 66 | char byte_read; |
LvdK | 7:6576a287e563 | 67 | char exor_byte = 0; |
LvdK | 7:6576a287e563 | 68 | // Create key message, starting with "$PCDUKEY," message header |
LvdK | 7:6576a287e563 | 69 | i = 9; // : i points to first position after "$PCDUKEY," message header |
LvdK | 7:6576a287e563 | 70 | // Add key string to message string including '*' |
LvdK | 7:6576a287e563 | 71 | |
LvdK | 7:6576a287e563 | 72 | if ( key_nr != 0 && key_nr < max_keys_CDUpanel ) { |
LvdK | 7:6576a287e563 | 73 | strcpy(&key_message[i],key_value[key_nr]); |
LvdK | 7:6576a287e563 | 74 | // Calculate checksum now : |
LvdK | 7:6576a287e563 | 75 | i = 1; // : i points to first place after '$' in message |
LvdK | 7:6576a287e563 | 76 | do { byte_read = key_message[i]; |
LvdK | 7:6576a287e563 | 77 | if (byte_read == '*') break; // : exclude '*' from calculation |
LvdK | 7:6576a287e563 | 78 | exor_byte = exor_byte ^ byte_read; |
LvdK | 7:6576a287e563 | 79 | i++; |
LvdK | 7:6576a287e563 | 80 | } while ( i < 20 ); |
LvdK | 7:6576a287e563 | 81 | i++; // : i now points to first digit of checksum after '*' |
LvdK | 7:6576a287e563 | 82 | // Add exor_byte in 2 hex digits and a CR + LF: |
LvdK | 7:6576a287e563 | 83 | sprintf(&key_message[i],"%02X\r\n",exor_byte); // : extra NULL char added by sprintf |
LvdK | 7:6576a287e563 | 84 | send_message_to_FS(key_message); // : send message to defined CDU-FS interface |
LvdK | 7:6576a287e563 | 85 | } |
LvdK | 7:6576a287e563 | 86 | } |
LvdK | 7:6576a287e563 | 87 | |
LvdK | 7:6576a287e563 | 88 |