Serial Communication
Dependencies: mbed
Fork of Nucleo_serial_menu by
main.cpp@1:df3b700ba2d9, 2015-10-20 (annotated)
- Committer:
- aulvaa
- Date:
- Tue Oct 20 08:20:13 2015 +0000
- Revision:
- 1:df3b700ba2d9
- Parent:
- 0:f4e546303b4e
- Child:
- 2:87127bdc7e93
gtesrhyea
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soulx | 0:f4e546303b4e | 1 | #include "mbed.h" |
soulx | 0:f4e546303b4e | 2 | |
soulx | 0:f4e546303b4e | 3 | //------------------------------------ |
soulx | 0:f4e546303b4e | 4 | // Hyperterminal configuration |
soulx | 0:f4e546303b4e | 5 | // 9600 bauds, 8-bit data, no parity |
soulx | 0:f4e546303b4e | 6 | //------------------------------------ |
soulx | 0:f4e546303b4e | 7 | |
soulx | 0:f4e546303b4e | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
aulvaa | 1:df3b700ba2d9 | 9 | DigitalOut led[] = {D2,D3,D4,D5,D6,D7,D8,D9}; |
soulx | 0:f4e546303b4e | 10 | DigitalOut myled(LED1); |
aulvaa | 1:df3b700ba2d9 | 11 | DigitalIn sw(D0); |
aulvaa | 1:df3b700ba2d9 | 12 | double speed=0.1; |
aulvaa | 1:df3b700ba2d9 | 13 | int pat = 1,tog=0; |
soulx | 0:f4e546303b4e | 14 | |
soulx | 0:f4e546303b4e | 15 | int main() |
soulx | 0:f4e546303b4e | 16 | { |
soulx | 0:f4e546303b4e | 17 | |
soulx | 0:f4e546303b4e | 18 | uint8_t state_menu=0; |
soulx | 0:f4e546303b4e | 19 | uint8_t state_show=0; |
soulx | 0:f4e546303b4e | 20 | uint8_t state_exit =0; |
soulx | 0:f4e546303b4e | 21 | uint8_t data; |
soulx | 0:f4e546303b4e | 22 | |
soulx | 0:f4e546303b4e | 23 | myled = 0; |
soulx | 0:f4e546303b4e | 24 | pc.printf("Hello World !\n"); |
soulx | 0:f4e546303b4e | 25 | while(1) { |
soulx | 0:f4e546303b4e | 26 | if(state_show == 0) { |
soulx | 0:f4e546303b4e | 27 | pc.printf("Menu\n"); |
soulx | 0:f4e546303b4e | 28 | pc.printf("1.LED Test\n"); |
soulx | 0:f4e546303b4e | 29 | pc.printf("2.Print Test\n"); |
soulx | 0:f4e546303b4e | 30 | state_show =1; |
soulx | 0:f4e546303b4e | 31 | } |
soulx | 0:f4e546303b4e | 32 | if(pc.readable()) { |
soulx | 0:f4e546303b4e | 33 | data = pc.getc(); |
soulx | 0:f4e546303b4e | 34 | pc.printf("\n"); |
soulx | 0:f4e546303b4e | 35 | state_show =0; |
soulx | 0:f4e546303b4e | 36 | state_exit =0; |
soulx | 0:f4e546303b4e | 37 | |
soulx | 0:f4e546303b4e | 38 | switch(data) { |
soulx | 0:f4e546303b4e | 39 | case '1': |
soulx | 0:f4e546303b4e | 40 | do { |
soulx | 0:f4e546303b4e | 41 | if(state_menu == 0) { |
soulx | 0:f4e546303b4e | 42 | pc.printf("LED Test\n"); |
aulvaa | 1:df3b700ba2d9 | 43 | pc.printf("a.LED Pattern1\n"); |
aulvaa | 1:df3b700ba2d9 | 44 | pc.printf("d.LED Pattern2\n"); |
aulvaa | 1:df3b700ba2d9 | 45 | |
aulvaa | 1:df3b700ba2d9 | 46 | pc.printf("s.Exit\n"); |
soulx | 0:f4e546303b4e | 47 | state_menu = 1; |
soulx | 0:f4e546303b4e | 48 | } |
soulx | 0:f4e546303b4e | 49 | if(pc.readable()) { |
soulx | 0:f4e546303b4e | 50 | data = pc.getc(); |
soulx | 0:f4e546303b4e | 51 | pc.printf("\n"); |
soulx | 0:f4e546303b4e | 52 | state_menu=0; |
soulx | 0:f4e546303b4e | 53 | |
soulx | 0:f4e546303b4e | 54 | switch(data) { |
soulx | 0:f4e546303b4e | 55 | |
soulx | 0:f4e546303b4e | 56 | case 'a': |
aulvaa | 1:df3b700ba2d9 | 57 | if(pat==1){ |
aulvaa | 1:df3b700ba2d9 | 58 | for(int x=0;x<=7;x++){ |
aulvaa | 1:df3b700ba2d9 | 59 | led[x]=1; |
aulvaa | 1:df3b700ba2d9 | 60 | wait(speed); |
aulvaa | 1:df3b700ba2d9 | 61 | led[x]=0; |
aulvaa | 1:df3b700ba2d9 | 62 | |
aulvaa | 1:df3b700ba2d9 | 63 | } |
aulvaa | 1:df3b700ba2d9 | 64 | } |
aulvaa | 1:df3b700ba2d9 | 65 | break; |
aulvaa | 1:df3b700ba2d9 | 66 | |
aulvaa | 1:df3b700ba2d9 | 67 | case 'd': |
aulvaa | 1:df3b700ba2d9 | 68 | if(pat==2){ |
aulvaa | 1:df3b700ba2d9 | 69 | for(int x=0;x<=7;x++){ |
aulvaa | 1:df3b700ba2d9 | 70 | led[x]=1; |
aulvaa | 1:df3b700ba2d9 | 71 | wait(speed); |
aulvaa | 1:df3b700ba2d9 | 72 | |
aulvaa | 1:df3b700ba2d9 | 73 | } |
aulvaa | 1:df3b700ba2d9 | 74 | for(int x=0;x<=7;x++){ |
aulvaa | 1:df3b700ba2d9 | 75 | led[x]=0; |
aulvaa | 1:df3b700ba2d9 | 76 | wait(speed); |
aulvaa | 1:df3b700ba2d9 | 77 | |
aulvaa | 1:df3b700ba2d9 | 78 | } |
aulvaa | 1:df3b700ba2d9 | 79 | |
aulvaa | 1:df3b700ba2d9 | 80 | } |
soulx | 0:f4e546303b4e | 81 | break; |
soulx | 0:f4e546303b4e | 82 | |
soulx | 0:f4e546303b4e | 83 | case 's': |
soulx | 0:f4e546303b4e | 84 | state_exit =1; |
soulx | 0:f4e546303b4e | 85 | |
soulx | 0:f4e546303b4e | 86 | break; |
soulx | 0:f4e546303b4e | 87 | |
soulx | 0:f4e546303b4e | 88 | default: |
aulvaa | 1:df3b700ba2d9 | 89 | pc.printf("please select a or s\n"); |
aulvaa | 1:df3b700ba2d9 | 90 | pc.printf("\n"); |
soulx | 0:f4e546303b4e | 91 | break; |
soulx | 0:f4e546303b4e | 92 | |
soulx | 0:f4e546303b4e | 93 | } |
soulx | 0:f4e546303b4e | 94 | } |
soulx | 0:f4e546303b4e | 95 | |
soulx | 0:f4e546303b4e | 96 | |
soulx | 0:f4e546303b4e | 97 | |
soulx | 0:f4e546303b4e | 98 | } while(state_exit ==0); |
soulx | 0:f4e546303b4e | 99 | pc.printf("\n\n"); |
soulx | 0:f4e546303b4e | 100 | break; |
soulx | 0:f4e546303b4e | 101 | |
soulx | 0:f4e546303b4e | 102 | case '2': |
soulx | 0:f4e546303b4e | 103 | |
soulx | 0:f4e546303b4e | 104 | do { |
soulx | 0:f4e546303b4e | 105 | if(state_menu == 0) { |
soulx | 0:f4e546303b4e | 106 | pc.printf("LED Test\n"); |
soulx | 0:f4e546303b4e | 107 | pc.printf("a.Menu 2 TEST\n"); |
soulx | 0:f4e546303b4e | 108 | pc.printf("s.Run\n"); |
soulx | 0:f4e546303b4e | 109 | pc.printf("x.Exit\n"); |
soulx | 0:f4e546303b4e | 110 | state_menu = 1; |
soulx | 0:f4e546303b4e | 111 | } |
aulvaa | 1:df3b700ba2d9 | 112 | if(sw.read()) { |
soulx | 0:f4e546303b4e | 113 | data = pc.getc(); |
soulx | 0:f4e546303b4e | 114 | pc.printf("\n"); |
soulx | 0:f4e546303b4e | 115 | state_menu=0; |
aulvaa | 1:df3b700ba2d9 | 116 | myled = 1; |
aulvaa | 1:df3b700ba2d9 | 117 | pc.putc(1); |
aulvaa | 1:df3b700ba2d9 | 118 | } |
aulvaa | 1:df3b700ba2d9 | 119 | |
aulvaa | 1:df3b700ba2d9 | 120 | else |
aulvaa | 1:df3b700ba2d9 | 121 | { myled = 0; |
aulvaa | 1:df3b700ba2d9 | 122 | pc.putc(0); |
aulvaa | 1:df3b700ba2d9 | 123 | } |
aulvaa | 1:df3b700ba2d9 | 124 | |
aulvaa | 1:df3b700ba2d9 | 125 | |
soulx | 0:f4e546303b4e | 126 | |
soulx | 0:f4e546303b4e | 127 | default: |
soulx | 0:f4e546303b4e | 128 | pc.printf("plz select a or s\n"); |
soulx | 0:f4e546303b4e | 129 | pc.printf("\n\n"); |
soulx | 0:f4e546303b4e | 130 | break; |
soulx | 0:f4e546303b4e | 131 | |
aulvaa | 1:df3b700ba2d9 | 132 | |
soulx | 0:f4e546303b4e | 133 | } |
soulx | 0:f4e546303b4e | 134 | |
soulx | 0:f4e546303b4e | 135 | |
soulx | 0:f4e546303b4e | 136 | |
aulvaa | 1:df3b700ba2d9 | 137 | while(state_exit ==0); |
soulx | 0:f4e546303b4e | 138 | pc.printf("\n\n"); |
soulx | 0:f4e546303b4e | 139 | break; |
soulx | 0:f4e546303b4e | 140 | |
soulx | 0:f4e546303b4e | 141 | |
soulx | 0:f4e546303b4e | 142 | case 0x00: |
soulx | 0:f4e546303b4e | 143 | |
soulx | 0:f4e546303b4e | 144 | break; |
soulx | 0:f4e546303b4e | 145 | |
aulvaa | 1:df3b700ba2d9 | 146 | |
soulx | 0:f4e546303b4e | 147 | } |
soulx | 0:f4e546303b4e | 148 | } |
soulx | 0:f4e546303b4e | 149 | |
soulx | 0:f4e546303b4e | 150 | |
soulx | 0:f4e546303b4e | 151 | |
soulx | 0:f4e546303b4e | 152 | |
soulx | 0:f4e546303b4e | 153 | |
soulx | 0:f4e546303b4e | 154 | |
soulx | 0:f4e546303b4e | 155 | } |
soulx | 0:f4e546303b4e | 156 | } |