Interface layer for the mbed boards ready for the JAVA library
Dependencies: C12832 LM75B MMA7660 mbed FXOS8700Q
Fork of frdm_serial by
main.cpp@0:6891aea05ef2, 2015-11-02 (annotated)
- Committer:
- co657_mjrb6
- Date:
- Mon Nov 02 17:29:26 2015 +0000
- Revision:
- 0:6891aea05ef2
- Child:
- 1:38f32fc3db2c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co657_mjrb6 | 0:6891aea05ef2 | 1 | #include "C12832.h" |
co657_mjrb6 | 0:6891aea05ef2 | 2 | #include "LM75B.h" |
co657_mjrb6 | 0:6891aea05ef2 | 3 | #include <string> |
co657_mjrb6 | 0:6891aea05ef2 | 4 | #include "mbed.h" |
co657_mjrb6 | 0:6891aea05ef2 | 5 | |
co657_mjrb6 | 0:6891aea05ef2 | 6 | DigitalOut led_red (LED_RED); |
co657_mjrb6 | 0:6891aea05ef2 | 7 | DigitalOut led_green (LED_GREEN); |
co657_mjrb6 | 0:6891aea05ef2 | 8 | DigitalOut led_blue (LED_BLUE); |
co657_mjrb6 | 0:6891aea05ef2 | 9 | |
co657_mjrb6 | 0:6891aea05ef2 | 10 | DigitalOut xr_led (D5); |
co657_mjrb6 | 0:6891aea05ef2 | 11 | DigitalOut xg_led (D9); |
co657_mjrb6 | 0:6891aea05ef2 | 12 | DigitalOut xb_led (PTC12); |
co657_mjrb6 | 0:6891aea05ef2 | 13 | |
co657_mjrb6 | 0:6891aea05ef2 | 14 | Ticker tempSender; |
co657_mjrb6 | 0:6891aea05ef2 | 15 | |
co657_mjrb6 | 0:6891aea05ef2 | 16 | int sw2_down, sw2_up, sw3_down, sw3_up, temp_flag; |
co657_mjrb6 | 0:6891aea05ef2 | 17 | |
co657_mjrb6 | 0:6891aea05ef2 | 18 | C12832 lcd(D11, D13, D12, D7, D10); |
co657_mjrb6 | 0:6891aea05ef2 | 19 | LM75B lm_temp (D14, D15); |
co657_mjrb6 | 0:6891aea05ef2 | 20 | InterruptIn sw2_int (PTC6); /* interrupts for the two on-board switches */ |
co657_mjrb6 | 0:6891aea05ef2 | 21 | InterruptIn sw3_int (PTA4); |
co657_mjrb6 | 0:6891aea05ef2 | 22 | |
co657_mjrb6 | 0:6891aea05ef2 | 23 | Serial pc(USBTX, USBRX); |
co657_mjrb6 | 0:6891aea05ef2 | 24 | |
co657_mjrb6 | 0:6891aea05ef2 | 25 | void parse(string buf) { |
co657_mjrb6 | 0:6891aea05ef2 | 26 | if (buf.find("ldg")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 27 | led_green = 1-strtod(buf.substr(3).c_str(), NULL); |
co657_mjrb6 | 0:6891aea05ef2 | 28 | } |
co657_mjrb6 | 0:6891aea05ef2 | 29 | else if (buf.find("ldb")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 30 | led_blue = 1-strtod(buf.substr(3).c_str(), NULL); |
co657_mjrb6 | 0:6891aea05ef2 | 31 | } |
co657_mjrb6 | 0:6891aea05ef2 | 32 | else if (buf.find("ldr")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 33 | led_red = 1-strtod(buf.substr(3).c_str(), NULL); |
co657_mjrb6 | 0:6891aea05ef2 | 34 | } |
co657_mjrb6 | 0:6891aea05ef2 | 35 | if (buf.find("xdg")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 36 | xg_led = 1-strtod(buf.substr(3).c_str(), NULL); |
co657_mjrb6 | 0:6891aea05ef2 | 37 | } |
co657_mjrb6 | 0:6891aea05ef2 | 38 | else if (buf.find("xdb")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 39 | xb_led = 1-strtod(buf.substr(3).c_str(), NULL); |
co657_mjrb6 | 0:6891aea05ef2 | 40 | } |
co657_mjrb6 | 0:6891aea05ef2 | 41 | else if (buf.find("xdr")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 42 | xr_led = 1-strtod(buf.substr(3).c_str(), NULL); |
co657_mjrb6 | 0:6891aea05ef2 | 43 | } |
co657_mjrb6 | 0:6891aea05ef2 | 44 | else if (buf.find("lcdclr")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 45 | lcd.cls(); |
co657_mjrb6 | 0:6891aea05ef2 | 46 | } |
co657_mjrb6 | 0:6891aea05ef2 | 47 | else if (buf.find("lcdloc")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 48 | string coord = buf.substr(6); |
co657_mjrb6 | 0:6891aea05ef2 | 49 | string x = buf.substr(0,buf.find(",")); |
co657_mjrb6 | 0:6891aea05ef2 | 50 | string y = buf.substr(buf.find(",")); |
co657_mjrb6 | 0:6891aea05ef2 | 51 | lcd.locate(atoi(x.c_str()),atoi(y.c_str())); |
co657_mjrb6 | 0:6891aea05ef2 | 52 | } |
co657_mjrb6 | 0:6891aea05ef2 | 53 | else if(buf.find("lcdprn")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 54 | lcd.printf(buf.substr(6).c_str()); |
co657_mjrb6 | 0:6891aea05ef2 | 55 | } |
co657_mjrb6 | 0:6891aea05ef2 | 56 | else if(buf.find("temp")==0) { |
co657_mjrb6 | 0:6891aea05ef2 | 57 | pc.printf("%f", lm_temp.read()); |
co657_mjrb6 | 0:6891aea05ef2 | 58 | pc.printf(";"); |
co657_mjrb6 | 0:6891aea05ef2 | 59 | } |
co657_mjrb6 | 0:6891aea05ef2 | 60 | } |
co657_mjrb6 | 0:6891aea05ef2 | 61 | |
co657_mjrb6 | 0:6891aea05ef2 | 62 | void sendTemp(void) |
co657_mjrb6 | 0:6891aea05ef2 | 63 | { |
co657_mjrb6 | 0:6891aea05ef2 | 64 | temp_flag = 1; |
co657_mjrb6 | 0:6891aea05ef2 | 65 | } |
co657_mjrb6 | 0:6891aea05ef2 | 66 | |
co657_mjrb6 | 0:6891aea05ef2 | 67 | void sw2Down (void) |
co657_mjrb6 | 0:6891aea05ef2 | 68 | { |
co657_mjrb6 | 0:6891aea05ef2 | 69 | sw2_down = 1; |
co657_mjrb6 | 0:6891aea05ef2 | 70 | } |
co657_mjrb6 | 0:6891aea05ef2 | 71 | |
co657_mjrb6 | 0:6891aea05ef2 | 72 | |
co657_mjrb6 | 0:6891aea05ef2 | 73 | void sw2Up (void) |
co657_mjrb6 | 0:6891aea05ef2 | 74 | { |
co657_mjrb6 | 0:6891aea05ef2 | 75 | sw2_up = 1; |
co657_mjrb6 | 0:6891aea05ef2 | 76 | } |
co657_mjrb6 | 0:6891aea05ef2 | 77 | |
co657_mjrb6 | 0:6891aea05ef2 | 78 | void sw3Down (void) |
co657_mjrb6 | 0:6891aea05ef2 | 79 | { |
co657_mjrb6 | 0:6891aea05ef2 | 80 | sw3_down = 1; |
co657_mjrb6 | 0:6891aea05ef2 | 81 | } |
co657_mjrb6 | 0:6891aea05ef2 | 82 | |
co657_mjrb6 | 0:6891aea05ef2 | 83 | |
co657_mjrb6 | 0:6891aea05ef2 | 84 | void sw3Up (void) |
co657_mjrb6 | 0:6891aea05ef2 | 85 | { |
co657_mjrb6 | 0:6891aea05ef2 | 86 | sw3_up = 1; |
co657_mjrb6 | 0:6891aea05ef2 | 87 | } |
co657_mjrb6 | 0:6891aea05ef2 | 88 | |
co657_mjrb6 | 0:6891aea05ef2 | 89 | int main() { |
co657_mjrb6 | 0:6891aea05ef2 | 90 | xb_led = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 91 | xr_led = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 92 | xg_led = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 93 | led_red=0; |
co657_mjrb6 | 0:6891aea05ef2 | 94 | led_green=0; |
co657_mjrb6 | 0:6891aea05ef2 | 95 | led_blue=0; |
co657_mjrb6 | 0:6891aea05ef2 | 96 | |
co657_mjrb6 | 0:6891aea05ef2 | 97 | sw2_int.mode (PullUp); |
co657_mjrb6 | 0:6891aea05ef2 | 98 | |
co657_mjrb6 | 0:6891aea05ef2 | 99 | sw2_int.fall(&sw2Down); |
co657_mjrb6 | 0:6891aea05ef2 | 100 | sw2_int.rise(&sw2Up); |
co657_mjrb6 | 0:6891aea05ef2 | 101 | |
co657_mjrb6 | 0:6891aea05ef2 | 102 | sw3_int.mode (PullUp); |
co657_mjrb6 | 0:6891aea05ef2 | 103 | |
co657_mjrb6 | 0:6891aea05ef2 | 104 | sw3_int.fall(&sw3Down); |
co657_mjrb6 | 0:6891aea05ef2 | 105 | sw3_int.rise(&sw3Up); |
co657_mjrb6 | 0:6891aea05ef2 | 106 | |
co657_mjrb6 | 0:6891aea05ef2 | 107 | tempSender.attach(&sendTemp, 2.0); |
co657_mjrb6 | 0:6891aea05ef2 | 108 | |
co657_mjrb6 | 0:6891aea05ef2 | 109 | pc.baud(115200); |
co657_mjrb6 | 0:6891aea05ef2 | 110 | pc.printf("%f", lm_temp.read()); |
co657_mjrb6 | 0:6891aea05ef2 | 111 | pc.printf(";"); |
co657_mjrb6 | 0:6891aea05ef2 | 112 | string buf; |
co657_mjrb6 | 0:6891aea05ef2 | 113 | while (true) { |
co657_mjrb6 | 0:6891aea05ef2 | 114 | if(sw2_down) { |
co657_mjrb6 | 0:6891aea05ef2 | 115 | pc.printf("2d;"); |
co657_mjrb6 | 0:6891aea05ef2 | 116 | sw2_down = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 117 | } |
co657_mjrb6 | 0:6891aea05ef2 | 118 | if(sw2_up) { |
co657_mjrb6 | 0:6891aea05ef2 | 119 | pc.printf("2u;"); |
co657_mjrb6 | 0:6891aea05ef2 | 120 | sw2_up = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 121 | } |
co657_mjrb6 | 0:6891aea05ef2 | 122 | if(sw3_down) { |
co657_mjrb6 | 0:6891aea05ef2 | 123 | pc.printf("3d;"); |
co657_mjrb6 | 0:6891aea05ef2 | 124 | sw3_down = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 125 | } |
co657_mjrb6 | 0:6891aea05ef2 | 126 | if(sw3_up) { |
co657_mjrb6 | 0:6891aea05ef2 | 127 | pc.printf("3u;"); |
co657_mjrb6 | 0:6891aea05ef2 | 128 | sw3_up = 0; |
co657_mjrb6 | 0:6891aea05ef2 | 129 | } |
co657_mjrb6 | 0:6891aea05ef2 | 130 | if(pc.readable()) { |
co657_mjrb6 | 0:6891aea05ef2 | 131 | char x = pc.getc(); |
co657_mjrb6 | 0:6891aea05ef2 | 132 | if(x==';') { |
co657_mjrb6 | 0:6891aea05ef2 | 133 | parse(buf); |
co657_mjrb6 | 0:6891aea05ef2 | 134 | buf = ""; |
co657_mjrb6 | 0:6891aea05ef2 | 135 | } |
co657_mjrb6 | 0:6891aea05ef2 | 136 | else { |
co657_mjrb6 | 0:6891aea05ef2 | 137 | buf += x; |
co657_mjrb6 | 0:6891aea05ef2 | 138 | } |
co657_mjrb6 | 0:6891aea05ef2 | 139 | } |
co657_mjrb6 | 0:6891aea05ef2 | 140 | if(temp_flag) { |
co657_mjrb6 | 0:6891aea05ef2 | 141 | temp_flag=0; |
co657_mjrb6 | 0:6891aea05ef2 | 142 | pc.printf("%f", lm_temp.read()); |
co657_mjrb6 | 0:6891aea05ef2 | 143 | pc.printf(";"); |
co657_mjrb6 | 0:6891aea05ef2 | 144 | } |
co657_mjrb6 | 0:6891aea05ef2 | 145 | } |
co657_mjrb6 | 0:6891aea05ef2 | 146 | } |
co657_mjrb6 | 0:6891aea05ef2 | 147 |