Check this one ! mbos setup changed, WTX hor pos changed, no font style check, no checksum check

Dependencies:   4DGL MODSERIAL mbed mbos

Fork of CDU_Mbed_21 by Engravity-CDU

Committer:
WillemBraat
Date:
Mon Jul 21 14:24:15 2014 +0000
Revision:
11:d60c746c097c
Parent:
10:7e350a27f936
screen_handler.cpp
; Added external flag S_Exec in procedure . Inhibits screen updates when 1. S_Exec is declared in line 77 of FS2CDU_data_5.cpp
;
; keyboard2.cpp
; Line 40. Added Key_led=0
;
; keyboard2.cpp "
; Added switch value in CDU_SET_BGL_INTENSIT...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LvdK 8:422544d24df3 1 // L. van der Kolk, ELVEDEKA, Holland //
LvdK 8:422544d24df3 2 // File: USB_receive_5.cpp
LvdK 8:422544d24df3 3
LvdK 8:422544d24df3 4 #include "mbed.h"
LvdK 8:422544d24df3 5 #include "MODSERIAL.h"
LvdK 8:422544d24df3 6
LvdK 8:422544d24df3 7 extern MODSERIAL USB;
LvdK 8:422544d24df3 8
LvdK 8:422544d24df3 9 extern int FSdata_received_flag;
LvdK 8:422544d24df3 10
LvdK 8:422544d24df3 11 //------- debug only ---------------------
LvdK 8:422544d24df3 12 extern MODSERIAL SERIAL_DEBUG;
LvdK 8:422544d24df3 13 //----------------------------------------
LvdK 8:422544d24df3 14
LvdK 8:422544d24df3 15 void decode_string(int nummer_of_chars);
LvdK 8:422544d24df3 16 void read_datafields(int command_number);
LvdK 8:422544d24df3 17
LvdK 8:422544d24df3 18 #define max_string_length 80 // : max length of received string starting with $ and ending with CR/LF
LvdK 8:422544d24df3 19 #define min_string_length 10 // : min length of received string starting with $ and ending with CR/LF
LvdK 8:422544d24df3 20 char string_received[max_string_length + 2]; // : holds received string starting with $ and ending with CR/LF
LvdK 8:422544d24df3 21
LvdK 8:422544d24df3 22 #define message_header "$PCDU" // : common message header in all messages
LvdK 8:422544d24df3 23 #define max_commas 10 // : max. nr of possible field separating commas in a valid message string to CDU
LvdK 8:422544d24df3 24 int comma[max_commas]; // : array with positions of all found commas in string_receved[]
LvdK 8:422544d24df3 25
WillemBraat 10:7e350a27f936 26 #define max_nr_of_commands 13 // : max nr of possible FS-to-CDU commands
LvdK 8:422544d24df3 27 // Define array of pointers to possible FS-to-CDU commands with 3 characters:
LvdK 8:422544d24df3 28 const char *command[max_nr_of_commands] = {
LvdK 8:422544d24df3 29 "123", // : no valid CDU command nr. 0 , used for debugging only
LvdK 8:422544d24df3 30 "MSG", // : command nr. 1
LvdK 8:422544d24df3 31 "EXC", // : command nr. 2
LvdK 8:422544d24df3 32 "BLT", // : command nr. 3
LvdK 8:422544d24df3 33 "SBY", // : command nr. 4
LvdK 8:422544d24df3 34 "CLS", // : command nr. 5
LvdK 8:422544d24df3 35 "SBC", // : command nr. 6
LvdK 8:422544d24df3 36 "WTX", // : command nr. 7
LvdK 8:422544d24df3 37 "ETX", // : command nr. 8
LvdK 8:422544d24df3 38 "KTX", // : command nr. 9
WillemBraat 10:7e350a27f936 39 "FAI", // : command nr.10
WillemBraat 10:7e350a27f936 40 "DPY", // : command nr.11
WillemBraat 10:7e350a27f936 41 "OFS", // : command nr.12
LvdK 8:422544d24df3 42 };
LvdK 8:422544d24df3 43
LvdK 8:422544d24df3 44 void collect_FSdata() {
LvdK 8:422544d24df3 45 // Function reads characters from FS written in receive buffer.
LvdK 8:422544d24df3 46 // Wiil be called after each RX receive interrupt, so characters will allways be stored.
LvdK 8:422544d24df3 47 // When analyze_busy is false, function will start reading characters from defined buffer and
LvdK 8:422544d24df3 48 // collects strings starting with $ and ending with CR/LF.
LvdK 8:422544d24df3 49 // Strings shorter than min_string_length or longer than max_string_length will be ignored,
LvdK 8:422544d24df3 50 // others will be analyzed further.
LvdK 8:422544d24df3 51 // After analyzing, flag analyze_busy will be set to false again.
LvdK 8:422544d24df3 52 static int $_detected = false; // : no valid begin of string detected (init only on first call)
LvdK 8:422544d24df3 53 static int string_pntr = 0; // : pointer at begin of string (init only on first call)
LvdK 8:422544d24df3 54 static int nr_of_received_char = 0; // : counter of received characters (init only on first call)
LvdK 8:422544d24df3 55 char rx_char; // : character which is analyzed
LvdK 8:422544d24df3 56
LvdK 8:422544d24df3 57 if ( FSdata_received_flag == false ) { // : process received chars only if no decoding busy now
LvdK 8:422544d24df3 58 while ( !USB.rxBufferEmpty() ) // : do as long as USB receive buffer is not empty
LvdK 8:422544d24df3 59 { rx_char = USB.getc(); // : get a char from Rx buffer
LvdK 8:422544d24df3 60
LvdK 8:422544d24df3 61 // ----------- Debug only ! ---------------------------------------------
LvdK 8:422544d24df3 62 //SERIAL_DEBUG.putc(rx_char); // : unprotected immediate echo of char
LvdK 8:422544d24df3 63 // ----------------------------------------------------------------------
LvdK 8:422544d24df3 64
LvdK 8:422544d24df3 65 // Check for string starting with $ char:
LvdK 8:422544d24df3 66 if ( rx_char == '$' && $_detected == false ){
LvdK 8:422544d24df3 67 $_detected = true; // : begin of string is detected
LvdK 8:422544d24df3 68 string_pntr = 0; // : set pointer to begin of string_received[] buffer
LvdK 8:422544d24df3 69 }
LvdK 8:422544d24df3 70 string_received[string_pntr] = rx_char;
LvdK 8:422544d24df3 71 string_pntr++;
LvdK 8:422544d24df3 72 if (string_pntr >= max_string_length) {
LvdK 8:422544d24df3 73 // command string looks too long, so start all over again:
LvdK 8:422544d24df3 74 string_pntr = 0; // : set pointer back to begin of string_received[] again
LvdK 8:422544d24df3 75 nr_of_received_char = 0; // : reset number of received chars
LvdK 8:422544d24df3 76 $_detected = false;
LvdK 8:422544d24df3 77 FSdata_received_flag = false;
LvdK 8:422544d24df3 78 }
LvdK 8:422544d24df3 79 if ( rx_char == '\r' && $_detected == true ) {
LvdK 8:422544d24df3 80 if ( string_pntr > min_string_length ) { // : check minimum string length
LvdK 8:422544d24df3 81 // Received string can be interesting now because
LvdK 8:422544d24df3 82 // it starts with '$' AND it ends on New-Line AND
LvdK 8:422544d24df3 83 // it has a minimum length AND it is not too long:
LvdK 8:422544d24df3 84 string_received[string_pntr] = '\0'; // : mark end of string
LvdK 8:422544d24df3 85
LvdK 8:422544d24df3 86 FSdata_received_flag = true;
LvdK 8:422544d24df3 87
LvdK 8:422544d24df3 88 // ----------- Debug only ! -------------------------------------------
LvdK 8:422544d24df3 89 // SERIAL_DEBUG.printf("string_received from USB: %s",string_received );
LvdK 8:422544d24df3 90 // --------------------------------------------------------------------
LvdK 8:422544d24df3 91
LvdK 8:422544d24df3 92 nr_of_received_char = string_pntr;
LvdK 8:422544d24df3 93 //Call decoder to analyse this string:
LvdK 8:422544d24df3 94 decode_string(nr_of_received_char);
LvdK 8:422544d24df3 95 // Get ready for receiving new commands:
LvdK 8:422544d24df3 96 $_detected = false;
LvdK 8:422544d24df3 97 string_pntr = 0; // : set pointer back to begin of string_received[] again
LvdK 8:422544d24df3 98 nr_of_received_char = 0; // : reset number of received chars
LvdK 8:422544d24df3 99 }
LvdK 8:422544d24df3 100 else {
LvdK 8:422544d24df3 101 $_detected = false;
LvdK 8:422544d24df3 102 string_pntr = 0; // : set pointer back to begin of string_received[] again
LvdK 8:422544d24df3 103 nr_of_received_char = 0; // : reset number of received chars
LvdK 8:422544d24df3 104 FSdata_received_flag = false;
LvdK 8:422544d24df3 105 }
LvdK 8:422544d24df3 106 }
LvdK 8:422544d24df3 107 }
LvdK 8:422544d24df3 108
LvdK 8:422544d24df3 109 }
LvdK 8:422544d24df3 110 }
LvdK 8:422544d24df3 111
LvdK 8:422544d24df3 112 void decode_string(int nummer_of_chars)
LvdK 8:422544d24df3 113 { // -- This function decodes a received $.....CR/LF string written in string_received[] --
LvdK 8:422544d24df3 114 // First it checks for a valid checksum and reads the positions of commas in the string.
LvdK 8:422544d24df3 115 // If checksum is OK, it will continue to look for valid 3 char FS-to-CDU commands.
LvdK 8:422544d24df3 116 // When a valid command is found, data fields will be analyzed further using the found positions
LvdK 8:422544d24df3 117 // of commas in the string.
LvdK 8:422544d24df3 118 int i,c, equal;
LvdK 8:422544d24df3 119 char byte_read, exor_byte;
LvdK 8:422544d24df3 120 char command_string[6], received_checksum[4], calc_checksum[4];
LvdK 8:422544d24df3 121 // Get checksum and position of commas in string_received[] :
LvdK 8:422544d24df3 122 exor_byte = 0;
LvdK 8:422544d24df3 123 i = 1; // : i points to first char after $
LvdK 8:422544d24df3 124 c = 1; // : position of first comma
LvdK 8:422544d24df3 125 do {
LvdK 8:422544d24df3 126 byte_read = string_received[i];
LvdK 8:422544d24df3 127 if (byte_read == ',' && c < max_commas) {
LvdK 8:422544d24df3 128 comma[c] = i;
LvdK 8:422544d24df3 129 c++;
LvdK 8:422544d24df3 130 }
LvdK 8:422544d24df3 131 if (byte_read == '*') break;
LvdK 8:422544d24df3 132 exor_byte = exor_byte ^ byte_read;
LvdK 8:422544d24df3 133 i++;
LvdK 8:422544d24df3 134 } while ( i < nummer_of_chars );
LvdK 8:422544d24df3 135
LvdK 8:422544d24df3 136
LvdK 8:422544d24df3 137 // ----------- Debug only -------------------------------------------
LvdK 8:422544d24df3 138 //SERIAL_DEBUG.printf("commas found : %d\n",c-1 ); // : show commas
LvdK 8:422544d24df3 139 // ------------------------------------------------------------------
LvdK 8:422544d24df3 140
LvdK 8:422544d24df3 141 i++; // : i points to first checksum char after char *
LvdK 8:422544d24df3 142 strncpy(received_checksum,&string_received[i],2); // : copy 2 char checksum after *
LvdK 8:422544d24df3 143 // Get calculated checksum by transforming exor_byte in 2 hex chars (with upper case A-F) :
LvdK 8:422544d24df3 144 sprintf(calc_checksum,"%02X",exor_byte); // : + extra NULL char added by sprintf
LvdK 8:422544d24df3 145 equal = strncmp(received_checksum,calc_checksum,2);
LvdK 8:422544d24df3 146
LvdK 8:422544d24df3 147 // ******************************************************************************************
LvdK 8:422544d24df3 148 // -- Force checksum to allways OK : < -- debug only !!
LvdK 8:422544d24df3 149 equal = 0;
LvdK 8:422544d24df3 150 // ******************************************************************************************
LvdK 8:422544d24df3 151
LvdK 8:422544d24df3 152 if (equal != 0) {
LvdK 8:422544d24df3 153 // ----------- Debug only -------------------------------------------------------
LvdK 8:422544d24df3 154 //SERIAL_DEBUG.printf("checksum is NOT OK ! \n" ); // : show message for debugging
LvdK 8:422544d24df3 155 // ------------------------------------------------------------------------------
LvdK 8:422544d24df3 156 FSdata_received_flag = false;
LvdK 8:422544d24df3 157 }
LvdK 8:422544d24df3 158 else { // checksum is OK, go on:
LvdK 8:422544d24df3 159 // Check for 5 char "$PCDU" header:
LvdK 8:422544d24df3 160 equal = strncmp(string_received,message_header,strlen(message_header));
LvdK 8:422544d24df3 161 if (equal != 0) {
LvdK 8:422544d24df3 162
LvdK 8:422544d24df3 163 // ----------- Debug only ---------------------------------------------------
LvdK 8:422544d24df3 164 //SERIAL_DEBUG.printf("no $PCDU header in message !\n" ); // : show message
LvdK 8:422544d24df3 165 // --------------------------------------------------------------------------
LvdK 8:422544d24df3 166
LvdK 8:422544d24df3 167 FSdata_received_flag = false;
LvdK 8:422544d24df3 168 }
LvdK 8:422544d24df3 169 else {
LvdK 8:422544d24df3 170 // Read 3 char command after message_header:
LvdK 8:422544d24df3 171 strncpy(command_string,&string_received[strlen(message_header)],3);
LvdK 8:422544d24df3 172
LvdK 8:422544d24df3 173 // ----------- Debug only ---------------------------------------------------------------------
LvdK 8:422544d24df3 174 //SERIAL_DEBUG.printf("\ncommand found : %3s\n",command_string ); // : show command for debugging
LvdK 8:422544d24df3 175 //SERIAL_DEBUG.printf("commas found : %d\n",c-1 ); // : show commas for debugging
LvdK 8:422544d24df3 176 // --------------------------------------------------------------------------------------------
LvdK 8:422544d24df3 177
LvdK 8:422544d24df3 178 // Compare found string with known 3 char command list:
LvdK 8:422544d24df3 179 i = 0;
LvdK 8:422544d24df3 180 do {
LvdK 8:422544d24df3 181 equal = strncmp(&command_string[0],command[i],3);
LvdK 8:422544d24df3 182 if( equal == 0) break;
LvdK 8:422544d24df3 183 i++;
LvdK 8:422544d24df3 184 } while ( i < max_nr_of_commands);
LvdK 8:422544d24df3 185
LvdK 8:422544d24df3 186 // ----------- Debug only ---------------------------------------------------------------
LvdK 8:422544d24df3 187 //SERIAL_DEBUG.printf("command number is : %d\n",i ); // : show command nr for debugging
LvdK 8:422544d24df3 188 // --------------------------------------------------------------------------------------
LvdK 8:422544d24df3 189
LvdK 8:422544d24df3 190 if (equal == 0) {
LvdK 8:422544d24df3 191 // Command is known now, so now read all data fields:
LvdK 8:422544d24df3 192 read_datafields(i);
LvdK 8:422544d24df3 193 }
LvdK 8:422544d24df3 194 else FSdata_received_flag = false;
LvdK 8:422544d24df3 195 }
LvdK 8:422544d24df3 196 }
LvdK 8:422544d24df3 197
LvdK 8:422544d24df3 198 }
WillemBraat 10:7e350a27f936 199