![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ftf connects queue lab
main.cpp@0:684e5d5adc13, 2016-11-06 (annotated)
- Committer:
- uLipe
- Date:
- Sun Nov 06 16:54:35 2016 +0000
- Revision:
- 0:684e5d5adc13
first working version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uLipe | 0:684e5d5adc13 | 1 | /** |
uLipe | 0:684e5d5adc13 | 2 | * @brief NXP FTF LAB3 - Mbed OS Queue message to control tasks |
uLipe | 0:684e5d5adc13 | 3 | */ |
uLipe | 0:684e5d5adc13 | 4 | |
uLipe | 0:684e5d5adc13 | 5 | |
uLipe | 0:684e5d5adc13 | 6 | #include "mbed.h" |
uLipe | 0:684e5d5adc13 | 7 | #include "ST7567.h" |
uLipe | 0:684e5d5adc13 | 8 | #include "rtos.h" |
uLipe | 0:684e5d5adc13 | 9 | |
uLipe | 0:684e5d5adc13 | 10 | /* LCD screen dimensions */ |
uLipe | 0:684e5d5adc13 | 11 | #define LCD_HEIGHT 64 |
uLipe | 0:684e5d5adc13 | 12 | #define LCD_WIDTH 128 |
uLipe | 0:684e5d5adc13 | 13 | |
uLipe | 0:684e5d5adc13 | 14 | /* LCD font dimensions */ |
uLipe | 0:684e5d5adc13 | 15 | #define FONT_HEIGHT 10 |
uLipe | 0:684e5d5adc13 | 16 | #define FONT_WIDTH 5 |
uLipe | 0:684e5d5adc13 | 17 | |
uLipe | 0:684e5d5adc13 | 18 | |
uLipe | 0:684e5d5adc13 | 19 | /** led task command descriptor */ |
uLipe | 0:684e5d5adc13 | 20 | typedef struct { |
uLipe | 0:684e5d5adc13 | 21 | int led_nbr; |
uLipe | 0:684e5d5adc13 | 22 | bool on_off; |
uLipe | 0:684e5d5adc13 | 23 | bool make_color; |
uLipe | 0:684e5d5adc13 | 24 | uint8_t color; |
uLipe | 0:684e5d5adc13 | 25 | |
uLipe | 0:684e5d5adc13 | 26 | }led_cmd_desc_t; |
uLipe | 0:684e5d5adc13 | 27 | |
uLipe | 0:684e5d5adc13 | 28 | |
uLipe | 0:684e5d5adc13 | 29 | /** glcd command descriptor */ |
uLipe | 0:684e5d5adc13 | 30 | typedef struct { |
uLipe | 0:684e5d5adc13 | 31 | char message[64]; |
uLipe | 0:684e5d5adc13 | 32 | int x; |
uLipe | 0:684e5d5adc13 | 33 | int y; |
uLipe | 0:684e5d5adc13 | 34 | }glcd_cmd_desc_t; |
uLipe | 0:684e5d5adc13 | 35 | |
uLipe | 0:684e5d5adc13 | 36 | |
uLipe | 0:684e5d5adc13 | 37 | |
uLipe | 0:684e5d5adc13 | 38 | /** Instance a on board GLCD object */ |
uLipe | 0:684e5d5adc13 | 39 | ST7567 glcd(D11, D13, D12, D9, D10); |
uLipe | 0:684e5d5adc13 | 40 | |
uLipe | 0:684e5d5adc13 | 41 | /* allocate statically stacks for the three threads */ |
uLipe | 0:684e5d5adc13 | 42 | unsigned char led_stk[1024]; |
uLipe | 0:684e5d5adc13 | 43 | unsigned char disp_stk[1024]; |
uLipe | 0:684e5d5adc13 | 44 | unsigned char shell_stk[8192]; |
uLipe | 0:684e5d5adc13 | 45 | |
uLipe | 0:684e5d5adc13 | 46 | /* creates three tread objects with different priorities */ |
uLipe | 0:684e5d5adc13 | 47 | Thread led_thread(osPriorityRealtime, 1024, &led_stk[0]); |
uLipe | 0:684e5d5adc13 | 48 | Thread disp_thread(osPriorityRealtime, 1024, &disp_stk[0]); |
uLipe | 0:684e5d5adc13 | 49 | Thread shell_thread(osPriorityNormal, 8192, &shell_stk[0]); |
uLipe | 0:684e5d5adc13 | 50 | |
uLipe | 0:684e5d5adc13 | 51 | /* create two queues, one for led commands other to lcd commands */ |
uLipe | 0:684e5d5adc13 | 52 | Mail<led_cmd_desc_t, 16> led_q; |
uLipe | 0:684e5d5adc13 | 53 | Mail<glcd_cmd_desc_t, 16> disp_q; |
uLipe | 0:684e5d5adc13 | 54 | |
uLipe | 0:684e5d5adc13 | 55 | |
uLipe | 0:684e5d5adc13 | 56 | /** Instance a UART class to communicate with pc */ |
uLipe | 0:684e5d5adc13 | 57 | Serial pc_serial(USBTX,USBRX); |
uLipe | 0:684e5d5adc13 | 58 | |
uLipe | 0:684e5d5adc13 | 59 | |
uLipe | 0:684e5d5adc13 | 60 | /** |
uLipe | 0:684e5d5adc13 | 61 | * @brief application demo task |
uLipe | 0:684e5d5adc13 | 62 | */ |
uLipe | 0:684e5d5adc13 | 63 | void led_task(void) { |
uLipe | 0:684e5d5adc13 | 64 | osEvent evt; |
uLipe | 0:684e5d5adc13 | 65 | DigitalOut leds[3] = {LED1, LED2, LED3}; |
uLipe | 0:684e5d5adc13 | 66 | led_cmd_desc_t *cmd; |
uLipe | 0:684e5d5adc13 | 67 | |
uLipe | 0:684e5d5adc13 | 68 | |
uLipe | 0:684e5d5adc13 | 69 | leds[0] = leds[1] = leds[2] = 1; |
uLipe | 0:684e5d5adc13 | 70 | |
uLipe | 0:684e5d5adc13 | 71 | for(;;) { |
uLipe | 0:684e5d5adc13 | 72 | |
uLipe | 0:684e5d5adc13 | 73 | /* wait for a new command */ |
uLipe | 0:684e5d5adc13 | 74 | evt = led_q.get(osWaitForever); |
uLipe | 0:684e5d5adc13 | 75 | if(evt.status != osEventMail) continue; |
uLipe | 0:684e5d5adc13 | 76 | cmd = (led_cmd_desc_t *)evt.value.p; |
uLipe | 0:684e5d5adc13 | 77 | if(cmd == NULL) continue; |
uLipe | 0:684e5d5adc13 | 78 | |
uLipe | 0:684e5d5adc13 | 79 | |
uLipe | 0:684e5d5adc13 | 80 | |
uLipe | 0:684e5d5adc13 | 81 | if(cmd->make_color != true) { |
uLipe | 0:684e5d5adc13 | 82 | |
uLipe | 0:684e5d5adc13 | 83 | /* is a led on_off command */ |
uLipe | 0:684e5d5adc13 | 84 | if(cmd->on_off == true) { |
uLipe | 0:684e5d5adc13 | 85 | |
uLipe | 0:684e5d5adc13 | 86 | if(cmd->led_nbr == 0) { |
uLipe | 0:684e5d5adc13 | 87 | /* handle all leds */ |
uLipe | 0:684e5d5adc13 | 88 | leds[0] = leds[1] = leds[2] = 0; |
uLipe | 0:684e5d5adc13 | 89 | } else { |
uLipe | 0:684e5d5adc13 | 90 | /* handle specific led */ |
uLipe | 0:684e5d5adc13 | 91 | leds[cmd->led_nbr - 1] = 0; |
uLipe | 0:684e5d5adc13 | 92 | } |
uLipe | 0:684e5d5adc13 | 93 | } else { |
uLipe | 0:684e5d5adc13 | 94 | if(cmd->led_nbr == 0) { |
uLipe | 0:684e5d5adc13 | 95 | /* handle all leds */ |
uLipe | 0:684e5d5adc13 | 96 | leds[0] = leds[1] = leds[2] = 1; |
uLipe | 0:684e5d5adc13 | 97 | } else { |
uLipe | 0:684e5d5adc13 | 98 | /* handle specific led */ |
uLipe | 0:684e5d5adc13 | 99 | leds[cmd->led_nbr - 1] = 1; |
uLipe | 0:684e5d5adc13 | 100 | } |
uLipe | 0:684e5d5adc13 | 101 | } |
uLipe | 0:684e5d5adc13 | 102 | |
uLipe | 0:684e5d5adc13 | 103 | /* free memory for next message */ |
uLipe | 0:684e5d5adc13 | 104 | led_q.free(cmd); |
uLipe | 0:684e5d5adc13 | 105 | |
uLipe | 0:684e5d5adc13 | 106 | } else { |
uLipe | 0:684e5d5adc13 | 107 | /* is a make color command */ |
uLipe | 0:684e5d5adc13 | 108 | int r, b, g; |
uLipe | 0:684e5d5adc13 | 109 | |
uLipe | 0:684e5d5adc13 | 110 | |
uLipe | 0:684e5d5adc13 | 111 | /* extract color bitfields */ |
uLipe | 0:684e5d5adc13 | 112 | r = (cmd->color & 0x04 )>> 2; |
uLipe | 0:684e5d5adc13 | 113 | b = cmd->color & 0x01; |
uLipe | 0:684e5d5adc13 | 114 | g = (cmd->color & 0x02) >> 1; |
uLipe | 0:684e5d5adc13 | 115 | |
uLipe | 0:684e5d5adc13 | 116 | leds[0] = ~(r) & 0x1; |
uLipe | 0:684e5d5adc13 | 117 | leds[1] = ~(b) & 0x1; |
uLipe | 0:684e5d5adc13 | 118 | leds[2] = ~(g) & 0x1; |
uLipe | 0:684e5d5adc13 | 119 | |
uLipe | 0:684e5d5adc13 | 120 | /* free memory for next message */ |
uLipe | 0:684e5d5adc13 | 121 | led_q.free(cmd); |
uLipe | 0:684e5d5adc13 | 122 | |
uLipe | 0:684e5d5adc13 | 123 | } |
uLipe | 0:684e5d5adc13 | 124 | |
uLipe | 0:684e5d5adc13 | 125 | } |
uLipe | 0:684e5d5adc13 | 126 | } |
uLipe | 0:684e5d5adc13 | 127 | |
uLipe | 0:684e5d5adc13 | 128 | /** |
uLipe | 0:684e5d5adc13 | 129 | * @brief application demo task |
uLipe | 0:684e5d5adc13 | 130 | */ |
uLipe | 0:684e5d5adc13 | 131 | void disp_task(void) { |
uLipe | 0:684e5d5adc13 | 132 | osEvent evt; |
uLipe | 0:684e5d5adc13 | 133 | glcd_cmd_desc_t *gl; |
uLipe | 0:684e5d5adc13 | 134 | |
uLipe | 0:684e5d5adc13 | 135 | for(;;) { |
uLipe | 0:684e5d5adc13 | 136 | /* wait for a new command */ |
uLipe | 0:684e5d5adc13 | 137 | evt = disp_q.get(osWaitForever); |
uLipe | 0:684e5d5adc13 | 138 | /* check if not corrupted */ |
uLipe | 0:684e5d5adc13 | 139 | if(evt.status != osEventMail) continue; |
uLipe | 0:684e5d5adc13 | 140 | gl = (glcd_cmd_desc_t *)evt.value.p; |
uLipe | 0:684e5d5adc13 | 141 | if(gl == NULL) continue; |
uLipe | 0:684e5d5adc13 | 142 | |
uLipe | 0:684e5d5adc13 | 143 | /* check consistency */ |
uLipe | 0:684e5d5adc13 | 144 | //pc_serial.printf("## %3d \n\r", gl->x); |
uLipe | 0:684e5d5adc13 | 145 | //pc_serial.printf("## %2d \n\r", gl->y); |
uLipe | 0:684e5d5adc13 | 146 | //pc_serial.printf("## %s \n\r", gl->message); |
uLipe | 0:684e5d5adc13 | 147 | |
uLipe | 0:684e5d5adc13 | 148 | |
uLipe | 0:684e5d5adc13 | 149 | |
uLipe | 0:684e5d5adc13 | 150 | glcd.cls(); |
uLipe | 0:684e5d5adc13 | 151 | /* set the glcd cursor */ |
uLipe | 0:684e5d5adc13 | 152 | glcd.locate(gl->x * FONT_WIDTH, gl->y * FONT_HEIGHT); |
uLipe | 0:684e5d5adc13 | 153 | /* and prints the message */ |
uLipe | 0:684e5d5adc13 | 154 | glcd.printf("%s", gl->message); |
uLipe | 0:684e5d5adc13 | 155 | |
uLipe | 0:684e5d5adc13 | 156 | disp_q.free(gl); |
uLipe | 0:684e5d5adc13 | 157 | } |
uLipe | 0:684e5d5adc13 | 158 | } |
uLipe | 0:684e5d5adc13 | 159 | |
uLipe | 0:684e5d5adc13 | 160 | |
uLipe | 0:684e5d5adc13 | 161 | |
uLipe | 0:684e5d5adc13 | 162 | |
uLipe | 0:684e5d5adc13 | 163 | /** |
uLipe | 0:684e5d5adc13 | 164 | * @brief thread_command interpreter |
uLipe | 0:684e5d5adc13 | 165 | */ |
uLipe | 0:684e5d5adc13 | 166 | void shell_led_command(int argc, char **argv){ |
uLipe | 0:684e5d5adc13 | 167 | led_cmd_desc_t *led_cmd = led_q.alloc(); |
uLipe | 0:684e5d5adc13 | 168 | |
uLipe | 0:684e5d5adc13 | 169 | if(led_cmd == NULL) { |
uLipe | 0:684e5d5adc13 | 170 | pc_serial.printf("leds: FATAL! Not enough memory! \n\r"); |
uLipe | 0:684e5d5adc13 | 171 | return; |
uLipe | 0:684e5d5adc13 | 172 | } |
uLipe | 0:684e5d5adc13 | 173 | if(argc < 1) { |
uLipe | 0:684e5d5adc13 | 174 | pc_serial.printf("## leds: too few arguments! \n\r"); |
uLipe | 0:684e5d5adc13 | 175 | led_q.free(led_cmd); |
uLipe | 0:684e5d5adc13 | 176 | return; |
uLipe | 0:684e5d5adc13 | 177 | } |
uLipe | 0:684e5d5adc13 | 178 | |
uLipe | 0:684e5d5adc13 | 179 | /* if has only one argument, the command can be a color */ |
uLipe | 0:684e5d5adc13 | 180 | if(argc == 1) { |
uLipe | 0:684e5d5adc13 | 181 | /* extract and limit the color value */ |
uLipe | 0:684e5d5adc13 | 182 | sscanf(argv[0],"%d", &led_cmd->color); |
uLipe | 0:684e5d5adc13 | 183 | led_cmd->color &= 0x07; |
uLipe | 0:684e5d5adc13 | 184 | led_cmd->on_off = false; |
uLipe | 0:684e5d5adc13 | 185 | led_cmd->make_color = true; |
uLipe | 0:684e5d5adc13 | 186 | } else if(( argc > 1) && (argc <= 2)) { |
uLipe | 0:684e5d5adc13 | 187 | /* selects the led */ |
uLipe | 0:684e5d5adc13 | 188 | if(strcmp("LED1", argv[0]) == 0) { |
uLipe | 0:684e5d5adc13 | 189 | led_cmd->led_nbr = 1; |
uLipe | 0:684e5d5adc13 | 190 | }else if(strcmp("LED2",argv[0]) == 0) { |
uLipe | 0:684e5d5adc13 | 191 | led_cmd->led_nbr = 2; |
uLipe | 0:684e5d5adc13 | 192 | }else if(strcmp("LED3",argv[0]) == 0) { |
uLipe | 0:684e5d5adc13 | 193 | led_cmd->led_nbr = 3; |
uLipe | 0:684e5d5adc13 | 194 | }else if(strcmp("all",argv[0]) == 0) { |
uLipe | 0:684e5d5adc13 | 195 | led_cmd->led_nbr = 0; |
uLipe | 0:684e5d5adc13 | 196 | }else { |
uLipe | 0:684e5d5adc13 | 197 | pc_serial.printf("## leds: invalid led! \n\r"); |
uLipe | 0:684e5d5adc13 | 198 | led_q.free(led_cmd); |
uLipe | 0:684e5d5adc13 | 199 | return; |
uLipe | 0:684e5d5adc13 | 200 | } |
uLipe | 0:684e5d5adc13 | 201 | |
uLipe | 0:684e5d5adc13 | 202 | |
uLipe | 0:684e5d5adc13 | 203 | /* take the action */ |
uLipe | 0:684e5d5adc13 | 204 | if(strcmp("on", argv[1])== 0) { |
uLipe | 0:684e5d5adc13 | 205 | led_cmd->make_color = false; |
uLipe | 0:684e5d5adc13 | 206 | led_cmd->on_off = true; |
uLipe | 0:684e5d5adc13 | 207 | }else if (strcmp("off", argv[1])==0){ |
uLipe | 0:684e5d5adc13 | 208 | led_cmd->make_color = false; |
uLipe | 0:684e5d5adc13 | 209 | led_cmd->on_off = false; |
uLipe | 0:684e5d5adc13 | 210 | } else { |
uLipe | 0:684e5d5adc13 | 211 | pc_serial.printf("## leds: invalid option! \n\r"); |
uLipe | 0:684e5d5adc13 | 212 | led_q.free(led_cmd); |
uLipe | 0:684e5d5adc13 | 213 | return; |
uLipe | 0:684e5d5adc13 | 214 | } |
uLipe | 0:684e5d5adc13 | 215 | } else { |
uLipe | 0:684e5d5adc13 | 216 | pc_serial.printf("## leds: too many arguments! \n\r"); |
uLipe | 0:684e5d5adc13 | 217 | led_q.free(led_cmd); |
uLipe | 0:684e5d5adc13 | 218 | return; |
uLipe | 0:684e5d5adc13 | 219 | } |
uLipe | 0:684e5d5adc13 | 220 | |
uLipe | 0:684e5d5adc13 | 221 | /* send the command descriptor to be executed in thread */ |
uLipe | 0:684e5d5adc13 | 222 | led_q.put(led_cmd); |
uLipe | 0:684e5d5adc13 | 223 | pc_serial.printf("## leds: running! \n\r"); |
uLipe | 0:684e5d5adc13 | 224 | |
uLipe | 0:684e5d5adc13 | 225 | |
uLipe | 0:684e5d5adc13 | 226 | } |
uLipe | 0:684e5d5adc13 | 227 | |
uLipe | 0:684e5d5adc13 | 228 | /** |
uLipe | 0:684e5d5adc13 | 229 | * @brief parses the glcd commands |
uLipe | 0:684e5d5adc13 | 230 | */ |
uLipe | 0:684e5d5adc13 | 231 | static void shell_disp_command(int argc, char **argv){ |
uLipe | 0:684e5d5adc13 | 232 | glcd_cmd_desc_t *gl = disp_q.alloc(); |
uLipe | 0:684e5d5adc13 | 233 | |
uLipe | 0:684e5d5adc13 | 234 | memset(&gl->message,0, 64); |
uLipe | 0:684e5d5adc13 | 235 | |
uLipe | 0:684e5d5adc13 | 236 | int position_x = 0; |
uLipe | 0:684e5d5adc13 | 237 | int position_y = 0; |
uLipe | 0:684e5d5adc13 | 238 | |
uLipe | 0:684e5d5adc13 | 239 | if(argc < 3) { |
uLipe | 0:684e5d5adc13 | 240 | pc_serial.printf("display: Too few arguments, exiting \n\r"); |
uLipe | 0:684e5d5adc13 | 241 | disp_q.free(gl); |
uLipe | 0:684e5d5adc13 | 242 | } else { |
uLipe | 0:684e5d5adc13 | 243 | sscanf(argv[0], "%3d", &gl->x); |
uLipe | 0:684e5d5adc13 | 244 | sscanf(argv[1], "%2d", &gl->y); |
uLipe | 0:684e5d5adc13 | 245 | |
uLipe | 0:684e5d5adc13 | 246 | /* check position range */ |
uLipe | 0:684e5d5adc13 | 247 | if(position_x > 127 || position_x < 0) { |
uLipe | 0:684e5d5adc13 | 248 | pc_serial.printf("## display: Invalid arguments, exiting"); |
uLipe | 0:684e5d5adc13 | 249 | disp_q.free(gl); |
uLipe | 0:684e5d5adc13 | 250 | return; |
uLipe | 0:684e5d5adc13 | 251 | } |
uLipe | 0:684e5d5adc13 | 252 | |
uLipe | 0:684e5d5adc13 | 253 | if(position_y > 63 || position_y < 0) { |
uLipe | 0:684e5d5adc13 | 254 | pc_serial.printf("## display: Invalid arguments, exiting"); |
uLipe | 0:684e5d5adc13 | 255 | disp_q.free(gl); |
uLipe | 0:684e5d5adc13 | 256 | return; |
uLipe | 0:684e5d5adc13 | 257 | } |
uLipe | 0:684e5d5adc13 | 258 | |
uLipe | 0:684e5d5adc13 | 259 | /* rebuild the string */ |
uLipe | 0:684e5d5adc13 | 260 | for(int i = 0; i < (argc - 2); i++) { |
uLipe | 0:684e5d5adc13 | 261 | strcat(&gl->message[0], argv[2 + i]); |
uLipe | 0:684e5d5adc13 | 262 | strcat(&gl->message[0],(const char *)" "); |
uLipe | 0:684e5d5adc13 | 263 | } |
uLipe | 0:684e5d5adc13 | 264 | |
uLipe | 0:684e5d5adc13 | 265 | |
uLipe | 0:684e5d5adc13 | 266 | /* check consistency */ |
uLipe | 0:684e5d5adc13 | 267 | //pc_serial.printf("## %3d \n\r", gl->x); |
uLipe | 0:684e5d5adc13 | 268 | //pc_serial.printf("## %2d \n\r", gl->y); |
uLipe | 0:684e5d5adc13 | 269 | //pc_serial.printf("## %s \n\r", gl->message); |
uLipe | 0:684e5d5adc13 | 270 | |
uLipe | 0:684e5d5adc13 | 271 | /* sends the text command to lcd task */ |
uLipe | 0:684e5d5adc13 | 272 | disp_q.put(gl); |
uLipe | 0:684e5d5adc13 | 273 | } |
uLipe | 0:684e5d5adc13 | 274 | } |
uLipe | 0:684e5d5adc13 | 275 | |
uLipe | 0:684e5d5adc13 | 276 | /** |
uLipe | 0:684e5d5adc13 | 277 | * @brief show help menu |
uLipe | 0:684e5d5adc13 | 278 | */ |
uLipe | 0:684e5d5adc13 | 279 | static void print_usage(void) { |
uLipe | 0:684e5d5adc13 | 280 | pc_serial.printf("## use with the syntax below: \n\r"); |
uLipe | 0:684e5d5adc13 | 281 | pc_serial.printf("## command <arg1> <arg2> ... <arg16> \n\r"); |
uLipe | 0:684e5d5adc13 | 282 | pc_serial.printf("## Available commands: \n\r"); |
uLipe | 0:684e5d5adc13 | 283 | pc_serial.printf ("## leds [LED1/2/3] [color1 -- 8] [on/off] : controls on board leds in multithread mode \n\r"); |
uLipe | 0:684e5d5adc13 | 284 | pc_serial.printf ("## leds LED1 on : for example turns on led 1\n\r"); |
uLipe | 0:684e5d5adc13 | 285 | pc_serial.printf ("## leds 5 : for example show purple color \n\r"); |
uLipe | 0:684e5d5adc13 | 286 | pc_serial.printf ("##\n\r"); |
uLipe | 0:684e5d5adc13 | 287 | pc_serial.printf ("## disp <x> <y> <message> : prints a message on lcd \n\r"); |
uLipe | 0:684e5d5adc13 | 288 | pc_serial.printf ("## disp 0 3 Hello world! : prints hello world at 0 colunm and 3 row \n\r"); |
uLipe | 0:684e5d5adc13 | 289 | } |
uLipe | 0:684e5d5adc13 | 290 | |
uLipe | 0:684e5d5adc13 | 291 | |
uLipe | 0:684e5d5adc13 | 292 | /** |
uLipe | 0:684e5d5adc13 | 293 | * @brief parse the command received via comport |
uLipe | 0:684e5d5adc13 | 294 | */ |
uLipe | 0:684e5d5adc13 | 295 | static void shell_parser (char *cmd, int size) { |
uLipe | 0:684e5d5adc13 | 296 | int cmd_ptr = 0; |
uLipe | 0:684e5d5adc13 | 297 | int arg_ptr = 0; |
uLipe | 0:684e5d5adc13 | 298 | int cmd_size = 0; |
uLipe | 0:684e5d5adc13 | 299 | char command_buffer[256]; |
uLipe | 0:684e5d5adc13 | 300 | |
uLipe | 0:684e5d5adc13 | 301 | int argc = 0; |
uLipe | 0:684e5d5adc13 | 302 | char *argv[16]; |
uLipe | 0:684e5d5adc13 | 303 | |
uLipe | 0:684e5d5adc13 | 304 | /* copy to the root command */ |
uLipe | 0:684e5d5adc13 | 305 | memset(&command_buffer, 0, sizeof(command_buffer)); |
uLipe | 0:684e5d5adc13 | 306 | |
uLipe | 0:684e5d5adc13 | 307 | /* find the root command terminator (space) */ |
uLipe | 0:684e5d5adc13 | 308 | while(cmd_ptr < size) { |
uLipe | 0:684e5d5adc13 | 309 | if(cmd[cmd_ptr] == ' ') break; |
uLipe | 0:684e5d5adc13 | 310 | cmd_ptr++; |
uLipe | 0:684e5d5adc13 | 311 | } |
uLipe | 0:684e5d5adc13 | 312 | cmd_size = size - cmd_ptr; |
uLipe | 0:684e5d5adc13 | 313 | |
uLipe | 0:684e5d5adc13 | 314 | |
uLipe | 0:684e5d5adc13 | 315 | /* extract command arguments */ |
uLipe | 0:684e5d5adc13 | 316 | strncpy(&command_buffer[0], &cmd[cmd_ptr + 1], (size - cmd_ptr)); |
uLipe | 0:684e5d5adc13 | 317 | |
uLipe | 0:684e5d5adc13 | 318 | /* terminates the root command */ |
uLipe | 0:684e5d5adc13 | 319 | cmd[cmd_ptr] = 0; |
uLipe | 0:684e5d5adc13 | 320 | arg_ptr = 0; |
uLipe | 0:684e5d5adc13 | 321 | |
uLipe | 0:684e5d5adc13 | 322 | //pc_serial.printf("## command: %s \n\r", cmd); |
uLipe | 0:684e5d5adc13 | 323 | //pc_serial.printf("## arguments: %s \n\r", command_buffer); |
uLipe | 0:684e5d5adc13 | 324 | |
uLipe | 0:684e5d5adc13 | 325 | |
uLipe | 0:684e5d5adc13 | 326 | /* extract the further arguments */ |
uLipe | 0:684e5d5adc13 | 327 | while(arg_ptr < (cmd_size)) { |
uLipe | 0:684e5d5adc13 | 328 | |
uLipe | 0:684e5d5adc13 | 329 | argc++; |
uLipe | 0:684e5d5adc13 | 330 | *(argv + (argc- 1)) = &command_buffer[arg_ptr]; |
uLipe | 0:684e5d5adc13 | 331 | |
uLipe | 0:684e5d5adc13 | 332 | /* find terminator */ |
uLipe | 0:684e5d5adc13 | 333 | while(command_buffer[arg_ptr] != ' ') { |
uLipe | 0:684e5d5adc13 | 334 | arg_ptr++; |
uLipe | 0:684e5d5adc13 | 335 | } |
uLipe | 0:684e5d5adc13 | 336 | |
uLipe | 0:684e5d5adc13 | 337 | /* adds to argument list */ |
uLipe | 0:684e5d5adc13 | 338 | command_buffer[arg_ptr] = 0; |
uLipe | 0:684e5d5adc13 | 339 | arg_ptr++; |
uLipe | 0:684e5d5adc13 | 340 | // pc_serial.printf("## argument no: %d : %s \n\r", argc, argv[argc-1]); |
uLipe | 0:684e5d5adc13 | 341 | } |
uLipe | 0:684e5d5adc13 | 342 | |
uLipe | 0:684e5d5adc13 | 343 | |
uLipe | 0:684e5d5adc13 | 344 | |
uLipe | 0:684e5d5adc13 | 345 | /* finds and execute the command table */ |
uLipe | 0:684e5d5adc13 | 346 | if(strcmp("leds", cmd) == 0) { |
uLipe | 0:684e5d5adc13 | 347 | shell_led_command(argc, argv); |
uLipe | 0:684e5d5adc13 | 348 | } else if(strcmp("disp", cmd) == 0){ |
uLipe | 0:684e5d5adc13 | 349 | shell_disp_command(argc, argv); |
uLipe | 0:684e5d5adc13 | 350 | } else { |
uLipe | 0:684e5d5adc13 | 351 | print_usage(); |
uLipe | 0:684e5d5adc13 | 352 | } |
uLipe | 0:684e5d5adc13 | 353 | |
uLipe | 0:684e5d5adc13 | 354 | } |
uLipe | 0:684e5d5adc13 | 355 | |
uLipe | 0:684e5d5adc13 | 356 | |
uLipe | 0:684e5d5adc13 | 357 | |
uLipe | 0:684e5d5adc13 | 358 | |
uLipe | 0:684e5d5adc13 | 359 | /** |
uLipe | 0:684e5d5adc13 | 360 | * @brief shell commands processing thread |
uLipe | 0:684e5d5adc13 | 361 | */ |
uLipe | 0:684e5d5adc13 | 362 | static void shell_task(void) |
uLipe | 0:684e5d5adc13 | 363 | { |
uLipe | 0:684e5d5adc13 | 364 | char serial_buffer[1024] = {0}; |
uLipe | 0:684e5d5adc13 | 365 | int read_ptr = 0; |
uLipe | 0:684e5d5adc13 | 366 | const char msg[] = {"Welcome to NXP FTF !\0"}; |
uLipe | 0:684e5d5adc13 | 367 | |
uLipe | 0:684e5d5adc13 | 368 | /* setup the serial as 115200 bps */ |
uLipe | 0:684e5d5adc13 | 369 | pc_serial.baud(115200); |
uLipe | 0:684e5d5adc13 | 370 | |
uLipe | 0:684e5d5adc13 | 371 | /* setup our on-board glcd */ |
uLipe | 0:684e5d5adc13 | 372 | glcd.set_contrast(0x35); |
uLipe | 0:684e5d5adc13 | 373 | glcd.cls(); |
uLipe | 0:684e5d5adc13 | 374 | |
uLipe | 0:684e5d5adc13 | 375 | /* Center the LCD cursor based on message size*/ |
uLipe | 0:684e5d5adc13 | 376 | glcd.locate(LCD_WIDTH - (sizeof(msg) * FONT_WIDTH), |
uLipe | 0:684e5d5adc13 | 377 | (LCD_HEIGHT - FONT_HEIGHT) / 2); |
uLipe | 0:684e5d5adc13 | 378 | |
uLipe | 0:684e5d5adc13 | 379 | |
uLipe | 0:684e5d5adc13 | 380 | /* prints a welcome message */ |
uLipe | 0:684e5d5adc13 | 381 | glcd.printf(msg); |
uLipe | 0:684e5d5adc13 | 382 | |
uLipe | 0:684e5d5adc13 | 383 | glcd.cls(); |
uLipe | 0:684e5d5adc13 | 384 | Thread::wait(1000); |
uLipe | 0:684e5d5adc13 | 385 | |
uLipe | 0:684e5d5adc13 | 386 | |
uLipe | 0:684e5d5adc13 | 387 | pc_serial.printf("******************************************************************\n\r"); |
uLipe | 0:684e5d5adc13 | 388 | pc_serial.printf("*** Welcome to NXP FTF Simple Shell application ****\n\r"); |
uLipe | 0:684e5d5adc13 | 389 | pc_serial.printf("*** Type some commands or just Enter key to see the available ****\n\r"); |
uLipe | 0:684e5d5adc13 | 390 | pc_serial.printf("******************************************************************\n\r"); |
uLipe | 0:684e5d5adc13 | 391 | pc_serial.printf(">>"); |
uLipe | 0:684e5d5adc13 | 392 | |
uLipe | 0:684e5d5adc13 | 393 | for(;;Thread::wait(50)) { |
uLipe | 0:684e5d5adc13 | 394 | /* check if we have character available */ |
uLipe | 0:684e5d5adc13 | 395 | if(pc_serial.readable()) { |
uLipe | 0:684e5d5adc13 | 396 | bool new_cmd = false; |
uLipe | 0:684e5d5adc13 | 397 | |
uLipe | 0:684e5d5adc13 | 398 | /* get the incoming character */ |
uLipe | 0:684e5d5adc13 | 399 | char c = pc_serial.getc(); |
uLipe | 0:684e5d5adc13 | 400 | |
uLipe | 0:684e5d5adc13 | 401 | if( (c == '\n') || (c == '\r')) { |
uLipe | 0:684e5d5adc13 | 402 | /* handle enter key */ |
uLipe | 0:684e5d5adc13 | 403 | new_cmd = true; |
uLipe | 0:684e5d5adc13 | 404 | pc_serial.printf("\n\r"); |
uLipe | 0:684e5d5adc13 | 405 | |
uLipe | 0:684e5d5adc13 | 406 | }else if( (c == 0x7F) || (c == 0x08)){ |
uLipe | 0:684e5d5adc13 | 407 | /* handle backspace and del keys */ |
uLipe | 0:684e5d5adc13 | 408 | pc_serial.printf("\033[1D"); |
uLipe | 0:684e5d5adc13 | 409 | pc_serial.putc(' '); |
uLipe | 0:684e5d5adc13 | 410 | pc_serial.printf("\033[1D"); |
uLipe | 0:684e5d5adc13 | 411 | |
uLipe | 0:684e5d5adc13 | 412 | read_ptr--; |
uLipe | 0:684e5d5adc13 | 413 | if(read_ptr < -1) read_ptr = 1023; |
uLipe | 0:684e5d5adc13 | 414 | serial_buffer[read_ptr] = ' '; |
uLipe | 0:684e5d5adc13 | 415 | |
uLipe | 0:684e5d5adc13 | 416 | |
uLipe | 0:684e5d5adc13 | 417 | } else { |
uLipe | 0:684e5d5adc13 | 418 | /* loopback the pressed key */ |
uLipe | 0:684e5d5adc13 | 419 | pc_serial.putc(c); |
uLipe | 0:684e5d5adc13 | 420 | |
uLipe | 0:684e5d5adc13 | 421 | /* store the incoming character on command circular buffer */ |
uLipe | 0:684e5d5adc13 | 422 | serial_buffer[read_ptr] = c; |
uLipe | 0:684e5d5adc13 | 423 | read_ptr = (read_ptr + 1) % 1024; |
uLipe | 0:684e5d5adc13 | 424 | } |
uLipe | 0:684e5d5adc13 | 425 | |
uLipe | 0:684e5d5adc13 | 426 | |
uLipe | 0:684e5d5adc13 | 427 | |
uLipe | 0:684e5d5adc13 | 428 | if(new_cmd != false) { |
uLipe | 0:684e5d5adc13 | 429 | /* command arrived, has other characters? */ |
uLipe | 0:684e5d5adc13 | 430 | if(read_ptr != 0) { |
uLipe | 0:684e5d5adc13 | 431 | shell_parser(&serial_buffer[0], read_ptr); |
uLipe | 0:684e5d5adc13 | 432 | } else { |
uLipe | 0:684e5d5adc13 | 433 | print_usage(); |
uLipe | 0:684e5d5adc13 | 434 | } |
uLipe | 0:684e5d5adc13 | 435 | /* reset the buffer command */ |
uLipe | 0:684e5d5adc13 | 436 | memset(&serial_buffer, 0, sizeof(serial_buffer)); |
uLipe | 0:684e5d5adc13 | 437 | read_ptr = 0; |
uLipe | 0:684e5d5adc13 | 438 | pc_serial.printf(">>"); |
uLipe | 0:684e5d5adc13 | 439 | } |
uLipe | 0:684e5d5adc13 | 440 | |
uLipe | 0:684e5d5adc13 | 441 | } |
uLipe | 0:684e5d5adc13 | 442 | } |
uLipe | 0:684e5d5adc13 | 443 | } |
uLipe | 0:684e5d5adc13 | 444 | |
uLipe | 0:684e5d5adc13 | 445 | |
uLipe | 0:684e5d5adc13 | 446 | /** |
uLipe | 0:684e5d5adc13 | 447 | * @brief main application loop |
uLipe | 0:684e5d5adc13 | 448 | */ |
uLipe | 0:684e5d5adc13 | 449 | int main(void) |
uLipe | 0:684e5d5adc13 | 450 | { |
uLipe | 0:684e5d5adc13 | 451 | |
uLipe | 0:684e5d5adc13 | 452 | glcd.cls(); |
uLipe | 0:684e5d5adc13 | 453 | |
uLipe | 0:684e5d5adc13 | 454 | /* starts the shell task and applications task*/ |
uLipe | 0:684e5d5adc13 | 455 | shell_thread.start(shell_task); |
uLipe | 0:684e5d5adc13 | 456 | led_thread.start(led_task); |
uLipe | 0:684e5d5adc13 | 457 | disp_thread.start(disp_task); |
uLipe | 0:684e5d5adc13 | 458 | return 0; |
uLipe | 0:684e5d5adc13 | 459 | } |