Teerapong Apiraungpituk
/
Nucleo_serial_menu_p
=3=
Fork of Nucleo_serial_menu by
main.cpp@1:59178970c02b, 2015-11-16 (annotated)
- Committer:
- 57340500039
- Date:
- Mon Nov 16 13:23:06 2015 +0000
- Revision:
- 1:59178970c02b
- Parent:
- 0:f4e546303b4e
meow
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soulx | 0:f4e546303b4e | 1 | #include "mbed.h" |
57340500039 | 1:59178970c02b | 2 | #include <string.h> |
soulx | 0:f4e546303b4e | 3 | |
57340500039 | 1:59178970c02b | 4 | DigitalOut led1(D11); |
57340500039 | 1:59178970c02b | 5 | DigitalOut led2(D3); |
57340500039 | 1:59178970c02b | 6 | DigitalOut led3(D4); |
57340500039 | 1:59178970c02b | 7 | DigitalOut led4(D5); |
57340500039 | 1:59178970c02b | 8 | DigitalOut led5(D6); |
57340500039 | 1:59178970c02b | 9 | DigitalOut led6(D7); |
57340500039 | 1:59178970c02b | 10 | DigitalOut led7(D9); |
57340500039 | 1:59178970c02b | 11 | DigitalOut led8(D10); |
57340500039 | 1:59178970c02b | 12 | DigitalOut led[8] = {led1,led2,led3,led4,led5,led6,led7,led8}; |
57340500039 | 1:59178970c02b | 13 | AnalogIn analog_value(A0); |
57340500039 | 1:59178970c02b | 14 | |
57340500039 | 1:59178970c02b | 15 | //------------------------------------ |
57340500039 | 1:59178970c02b | 16 | |
57340500039 | 1:59178970c02b | 17 | //------------------------------------ |
57340500039 | 1:59178970c02b | 18 | |
57340500039 | 1:59178970c02b | 19 | Serial pc(D8, D2); //D8 = Tx, D2 = Rx |
57340500039 | 1:59178970c02b | 20 | int funcA(){ |
57340500039 | 1:59178970c02b | 21 | uint8_t a=0; |
57340500039 | 1:59178970c02b | 22 | uint8_t b=0; |
57340500039 | 1:59178970c02b | 23 | char input[2]={0,0}; |
57340500039 | 1:59178970c02b | 24 | |
57340500039 | 1:59178970c02b | 25 | input[0] = pc.getc(); |
57340500039 | 1:59178970c02b | 26 | input[1] = pc.getc(); |
57340500039 | 1:59178970c02b | 27 | a = input[0]-'0'; |
57340500039 | 1:59178970c02b | 28 | b = input[1]-'0'; |
57340500039 | 1:59178970c02b | 29 | if(a>=9||a<0) a = 0; |
57340500039 | 1:59178970c02b | 30 | if(b>=9||b<0) b = 0; |
57340500039 | 1:59178970c02b | 31 | |
57340500039 | 1:59178970c02b | 32 | switch(a){ |
57340500039 | 1:59178970c02b | 33 | case 0: led5 = 0; led6 = 0; led7 = 0; led8 = 0; break; |
57340500039 | 1:59178970c02b | 34 | case 1: led5 = 1; led6 = 0; led7 = 0; led8 = 0; break; |
57340500039 | 1:59178970c02b | 35 | case 2: led5 = 0; led6 = 1; led7 = 0; led8 = 0; break; |
57340500039 | 1:59178970c02b | 36 | case 3: led5 = 1; led6 = 1; led7 = 0; led8 = 0; break; |
57340500039 | 1:59178970c02b | 37 | case 4: led5 = 0; led6 = 0; led7 = 1; led8 = 0; break; |
57340500039 | 1:59178970c02b | 38 | case 5: led5 = 1; led6 = 0; led7 = 1; led8 = 0; break; |
57340500039 | 1:59178970c02b | 39 | case 6: led5 = 0; led6 = 1; led7 = 1; led8 = 0; break; |
57340500039 | 1:59178970c02b | 40 | case 7: led5 = 1; led6 = 1; led7 = 1; led8 = 0; break; |
57340500039 | 1:59178970c02b | 41 | case 8: led5 = 0; led6 = 0; led7 = 0; led8 = 1; break; |
57340500039 | 1:59178970c02b | 42 | case 9: led5 = 1; led6 = 0; led7 = 0; led8 = 1; break; |
soulx | 0:f4e546303b4e | 43 | } |
57340500039 | 1:59178970c02b | 44 | switch(b){ |
57340500039 | 1:59178970c02b | 45 | case 0: led1 = 0; led2 = 0; led3 = 0; led4 = 0; break; |
57340500039 | 1:59178970c02b | 46 | case 1: led1 = 1; led2 = 0; led3 = 0; led4 = 0; break; |
57340500039 | 1:59178970c02b | 47 | case 2: led1 = 0; led2 = 1; led3 = 0; led4 = 0; break; |
57340500039 | 1:59178970c02b | 48 | case 3: led1 = 1; led2 = 1; led3 = 0; led4 = 0; break; |
57340500039 | 1:59178970c02b | 49 | case 4: led1 = 0; led2 = 0; led3 = 1; led4 = 0; break; |
57340500039 | 1:59178970c02b | 50 | case 5: led1 = 1; led2 = 0; led3 = 1; led4 = 0; break; |
57340500039 | 1:59178970c02b | 51 | case 6: led1 = 0; led2 = 1; led3 = 1; led4 = 0; break; |
57340500039 | 1:59178970c02b | 52 | case 7: led1 = 1; led2 = 1; led3 = 1; led4 = 0; break; |
57340500039 | 1:59178970c02b | 53 | case 8: led1 = 0; led2 = 0; led3 = 0; led4 = 1; break; |
57340500039 | 1:59178970c02b | 54 | case 9: led1 = 1; led2 = 0; led3 = 0; led4 = 1; break; |
57340500039 | 1:59178970c02b | 55 | } |
57340500039 | 1:59178970c02b | 56 | return 0; |
57340500039 | 1:59178970c02b | 57 | } |
57340500039 | 1:59178970c02b | 58 | int funcB(){ |
57340500039 | 1:59178970c02b | 59 | float meas; |
57340500039 | 1:59178970c02b | 60 | meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
57340500039 | 1:59178970c02b | 61 | meas = meas * 3.3; // Change the value to be in the 0 to 33 range |
57340500039 | 1:59178970c02b | 62 | pc.printf("%f\n",meas); |
57340500039 | 1:59178970c02b | 63 | return 0; |
57340500039 | 1:59178970c02b | 64 | } |
57340500039 | 1:59178970c02b | 65 | int main(){ |
57340500039 | 1:59178970c02b | 66 | char input = 'x'; |
57340500039 | 1:59178970c02b | 67 | |
57340500039 | 1:59178970c02b | 68 | while(1){ |
57340500039 | 1:59178970c02b | 69 | pc.printf("mode a : input 2 digits number.\r\n"); |
57340500039 | 1:59178970c02b | 70 | pc.printf("mode b : read analog from VR.\r\n\r\n"); |
57340500039 | 1:59178970c02b | 71 | pc.printf("input : "); |
57340500039 | 1:59178970c02b | 72 | input = pc.getc(); |
57340500039 | 1:59178970c02b | 73 | switch(input){ |
57340500039 | 1:59178970c02b | 74 | case 'a': pc.printf("mode a:\r\n");funcA();break; |
57340500039 | 1:59178970c02b | 75 | case 'b': pc.printf("mode b:\r\n");funcB();break; |
57340500039 | 1:59178970c02b | 76 | } |
57340500039 | 1:59178970c02b | 77 | |
57340500039 | 1:59178970c02b | 78 | pc.printf("\r\n**************************\r\n\r\n"); |
soulx | 0:f4e546303b4e | 79 | } |
57340500039 | 1:59178970c02b | 80 | |
57340500039 | 1:59178970c02b | 81 | } |