ECE 4180 Mini-Project (Lab 4)

Dependencies:   4DGL-uLCD-SE MODSERIAL USBHost mbed

Committer:
mohit1234
Date:
Mon Mar 09 21:27:24 2015 +0000
Revision:
0:08ca1c7b9623
ECE4180

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mohit1234 0:08ca1c7b9623 1 #include "mbed.h"
mohit1234 0:08ca1c7b9623 2 #include "USBHostKeyboard.h"
mohit1234 0:08ca1c7b9623 3 #include <string>
mohit1234 0:08ca1c7b9623 4 #include "error.h"
mohit1234 0:08ca1c7b9623 5
mohit1234 0:08ca1c7b9623 6 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
mohit1234 0:08ca1c7b9623 7 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 1024
mohit1234 0:08ca1c7b9623 8
mohit1234 0:08ca1c7b9623 9 #include "MODSERIAL.h"
mohit1234 0:08ca1c7b9623 10 #include "uLCD_4DGL.h"
mohit1234 0:08ca1c7b9623 11
mohit1234 0:08ca1c7b9623 12 DigitalOut myled(LED1);
mohit1234 0:08ca1c7b9623 13 MODSERIAL xbee(p9, p10);
mohit1234 0:08ca1c7b9623 14 DigitalOut reset(p20);
mohit1234 0:08ca1c7b9623 15 DigitalOut type(p19);
mohit1234 0:08ca1c7b9623 16 MODSERIAL pc(USBTX, USBRX);
mohit1234 0:08ca1c7b9623 17
mohit1234 0:08ca1c7b9623 18 DigitalOut led1(LED1);
mohit1234 0:08ca1c7b9623 19 DigitalOut led2(LED2);
mohit1234 0:08ca1c7b9623 20 DigitalOut led3(LED3);
mohit1234 0:08ca1c7b9623 21
mohit1234 0:08ca1c7b9623 22 uLCD_4DGL lcd(p13,p14,p15);
mohit1234 0:08ca1c7b9623 23
mohit1234 0:08ca1c7b9623 24 string s = "";
mohit1234 0:08ca1c7b9623 25
mohit1234 0:08ca1c7b9623 26 void clearLastChar() {
mohit1234 0:08ca1c7b9623 27 lcd.color(BLACK);
mohit1234 0:08ca1c7b9623 28 lcd.locate(lcd.current_col-1, lcd.current_row);
mohit1234 0:08ca1c7b9623 29 lcd.putc(' ');
mohit1234 0:08ca1c7b9623 30 lcd.locate(lcd.current_col-1, lcd.current_row);
mohit1234 0:08ca1c7b9623 31 }
mohit1234 0:08ca1c7b9623 32
mohit1234 0:08ca1c7b9623 33 void onKey(uint8_t key)
mohit1234 0:08ca1c7b9623 34 {
mohit1234 0:08ca1c7b9623 35 if (key == '\n') {
mohit1234 0:08ca1c7b9623 36 led3 = 0;
mohit1234 0:08ca1c7b9623 37 xbee.printf("%s\r", s);
mohit1234 0:08ca1c7b9623 38 s = "";
mohit1234 0:08ca1c7b9623 39 lcd.cls();
mohit1234 0:08ca1c7b9623 40 } else if (key == 8) {
mohit1234 0:08ca1c7b9623 41 if (lcd.current_col > 0) {
mohit1234 0:08ca1c7b9623 42 clearLastChar();
mohit1234 0:08ca1c7b9623 43 s = s.substr(0,s.length()-1);
mohit1234 0:08ca1c7b9623 44 }
mohit1234 0:08ca1c7b9623 45 } else {
mohit1234 0:08ca1c7b9623 46 s += key;
mohit1234 0:08ca1c7b9623 47 led3 = 1;
mohit1234 0:08ca1c7b9623 48 lcd.color(WHITE);
mohit1234 0:08ca1c7b9623 49 lcd.putc(key);
mohit1234 0:08ca1c7b9623 50 }
mohit1234 0:08ca1c7b9623 51 }
mohit1234 0:08ca1c7b9623 52
mohit1234 0:08ca1c7b9623 53 void keyboard_task(void const *)
mohit1234 0:08ca1c7b9623 54 {
mohit1234 0:08ca1c7b9623 55
mohit1234 0:08ca1c7b9623 56 USBHostKeyboard keyboard;
mohit1234 0:08ca1c7b9623 57
mohit1234 0:08ca1c7b9623 58 while(1) {
mohit1234 0:08ca1c7b9623 59 // try to connect a USB keyboard
mohit1234 0:08ca1c7b9623 60 while(!keyboard.connect())
mohit1234 0:08ca1c7b9623 61 Thread::wait(500);
mohit1234 0:08ca1c7b9623 62
mohit1234 0:08ca1c7b9623 63 // when connected, attach handler called on keyboard event
mohit1234 0:08ca1c7b9623 64 keyboard.attach(onKey);
mohit1234 0:08ca1c7b9623 65
mohit1234 0:08ca1c7b9623 66 // wait until the keyboard is disconnected
mohit1234 0:08ca1c7b9623 67 while(keyboard.connected())
mohit1234 0:08ca1c7b9623 68 Thread::wait(500);
mohit1234 0:08ca1c7b9623 69 }
mohit1234 0:08ca1c7b9623 70 }
mohit1234 0:08ca1c7b9623 71
mohit1234 0:08ca1c7b9623 72 int num = 0;
mohit1234 0:08ca1c7b9623 73
mohit1234 0:08ca1c7b9623 74 int main()
mohit1234 0:08ca1c7b9623 75 {
mohit1234 0:08ca1c7b9623 76 lcd.baudrate(3000000);
mohit1234 0:08ca1c7b9623 77 reset = 0;
mohit1234 0:08ca1c7b9623 78 wait_ms(1);
mohit1234 0:08ca1c7b9623 79 reset = 1;
mohit1234 0:08ca1c7b9623 80 wait_ms(1);
mohit1234 0:08ca1c7b9623 81
mohit1234 0:08ca1c7b9623 82 if (!type) {
mohit1234 0:08ca1c7b9623 83 while(1) {
mohit1234 0:08ca1c7b9623 84 led2 = !led2;
mohit1234 0:08ca1c7b9623 85
mohit1234 0:08ca1c7b9623 86 if (xbee.readable()) {
mohit1234 0:08ca1c7b9623 87 ++num;
mohit1234 0:08ca1c7b9623 88 if (num >= 2) {
mohit1234 0:08ca1c7b9623 89 lcd.cls();
mohit1234 0:08ca1c7b9623 90 num = 0;
mohit1234 0:08ca1c7b9623 91 }
mohit1234 0:08ca1c7b9623 92 lcd.printf("Got new Message:\n");
mohit1234 0:08ca1c7b9623 93 while (xbee.readable()) {
mohit1234 0:08ca1c7b9623 94 //pc.putc(xbee.getc());
mohit1234 0:08ca1c7b9623 95 char buffer[512];
mohit1234 0:08ca1c7b9623 96 xbee.scanf("%s", buffer);
mohit1234 0:08ca1c7b9623 97 lcd.printf("%s ", buffer);
mohit1234 0:08ca1c7b9623 98 wait_ms(1);
mohit1234 0:08ca1c7b9623 99 }
mohit1234 0:08ca1c7b9623 100 lcd.printf("\n\n");
mohit1234 0:08ca1c7b9623 101 led3 = !led3;
mohit1234 0:08ca1c7b9623 102 }
mohit1234 0:08ca1c7b9623 103 wait_ms(20);
mohit1234 0:08ca1c7b9623 104 }
mohit1234 0:08ca1c7b9623 105 } else {
mohit1234 0:08ca1c7b9623 106 Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
mohit1234 0:08ca1c7b9623 107 while(1);
mohit1234 0:08ca1c7b9623 108 }
mohit1234 0:08ca1c7b9623 109
mohit1234 0:08ca1c7b9623 110 }