Robert Labuz
/
mbed2_2_a
mbed2_2_a
main.cpp@0:a807bd13ba83, 2017-05-05 (annotated)
- Committer:
- Robsonik16
- Date:
- Fri May 05 19:45:09 2017 +0000
- Revision:
- 0:a807bd13ba83
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Robsonik16 | 0:a807bd13ba83 | 1 | #include "mbed.h" |
Robsonik16 | 0:a807bd13ba83 | 2 | |
Robsonik16 | 0:a807bd13ba83 | 3 | //------------------------------------ |
Robsonik16 | 0:a807bd13ba83 | 4 | // Hyperterminal configuration |
Robsonik16 | 0:a807bd13ba83 | 5 | // 9600 bauds, 8-bit data, no parity |
Robsonik16 | 0:a807bd13ba83 | 6 | //------------------------------------ |
Robsonik16 | 0:a807bd13ba83 | 7 | |
Robsonik16 | 0:a807bd13ba83 | 8 | RawSerial pc(USBTX,USBRX,9600); |
Robsonik16 | 0:a807bd13ba83 | 9 | |
Robsonik16 | 0:a807bd13ba83 | 10 | DigitalOut led_green(LED1); |
Robsonik16 | 0:a807bd13ba83 | 11 | DigitalOut led_red(LED2); |
Robsonik16 | 0:a807bd13ba83 | 12 | |
Robsonik16 | 0:a807bd13ba83 | 13 | //DigitalIn led_green_in(LED1); |
Robsonik16 | 0:a807bd13ba83 | 14 | |
Robsonik16 | 0:a807bd13ba83 | 15 | InterruptIn user_button(USER_BUTTON); |
Robsonik16 | 0:a807bd13ba83 | 16 | |
Robsonik16 | 0:a807bd13ba83 | 17 | float delay_on = 0.2; |
Robsonik16 | 0:a807bd13ba83 | 18 | float delay_off = 1.0; |
Robsonik16 | 0:a807bd13ba83 | 19 | |
Robsonik16 | 0:a807bd13ba83 | 20 | void button_pressed() |
Robsonik16 | 0:a807bd13ba83 | 21 | { |
Robsonik16 | 0:a807bd13ba83 | 22 | delay_on = 0.1; |
Robsonik16 | 0:a807bd13ba83 | 23 | delay_off = 0.1; |
Robsonik16 | 0:a807bd13ba83 | 24 | } |
Robsonik16 | 0:a807bd13ba83 | 25 | |
Robsonik16 | 0:a807bd13ba83 | 26 | void button_released() |
Robsonik16 | 0:a807bd13ba83 | 27 | { |
Robsonik16 | 0:a807bd13ba83 | 28 | delay_on = 0.2; |
Robsonik16 | 0:a807bd13ba83 | 29 | delay_off = 1.0; |
Robsonik16 | 0:a807bd13ba83 | 30 | } |
Robsonik16 | 0:a807bd13ba83 | 31 | |
Robsonik16 | 0:a807bd13ba83 | 32 | |
Robsonik16 | 0:a807bd13ba83 | 33 | int my_puts(char tab[],int size){ |
Robsonik16 | 0:a807bd13ba83 | 34 | //retutn 0 if string is valid |
Robsonik16 | 0:a807bd13ba83 | 35 | bool StringIsValid=false; |
Robsonik16 | 0:a807bd13ba83 | 36 | for( int index=0;index<size;index++){ |
Robsonik16 | 0:a807bd13ba83 | 37 | if (tab[index]==NULL){ |
Robsonik16 | 0:a807bd13ba83 | 38 | StringIsValid = true; |
Robsonik16 | 0:a807bd13ba83 | 39 | break; |
Robsonik16 | 0:a807bd13ba83 | 40 | } |
Robsonik16 | 0:a807bd13ba83 | 41 | } |
Robsonik16 | 0:a807bd13ba83 | 42 | if(StringIsValid == false) return(1); |
Robsonik16 | 0:a807bd13ba83 | 43 | |
Robsonik16 | 0:a807bd13ba83 | 44 | for( int index=0;index<size;index++){ |
Robsonik16 | 0:a807bd13ba83 | 45 | if (tab[index]==NULL){ |
Robsonik16 | 0:a807bd13ba83 | 46 | pc.putc('\r'); |
Robsonik16 | 0:a807bd13ba83 | 47 | while(!pc.writeable()); |
Robsonik16 | 0:a807bd13ba83 | 48 | pc.putc('\n'); |
Robsonik16 | 0:a807bd13ba83 | 49 | return(0); |
Robsonik16 | 0:a807bd13ba83 | 50 | } |
Robsonik16 | 0:a807bd13ba83 | 51 | while(!pc.writeable()); |
Robsonik16 | 0:a807bd13ba83 | 52 | pc.putc(tab[index]); |
Robsonik16 | 0:a807bd13ba83 | 53 | } |
Robsonik16 | 0:a807bd13ba83 | 54 | return(0); |
Robsonik16 | 0:a807bd13ba83 | 55 | } |
Robsonik16 | 0:a807bd13ba83 | 56 | |
Robsonik16 | 0:a807bd13ba83 | 57 | |
Robsonik16 | 0:a807bd13ba83 | 58 | char *my_gets(char *str){ |
Robsonik16 | 0:a807bd13ba83 | 59 | if (!pc.readable())return (NULL); |
Robsonik16 | 0:a807bd13ba83 | 60 | char c; |
Robsonik16 | 0:a807bd13ba83 | 61 | for(int index=0;;index++){ |
Robsonik16 | 0:a807bd13ba83 | 62 | c =pc.getc (); |
Robsonik16 | 0:a807bd13ba83 | 63 | if ((c=='\r')||(c=='\n')){ |
Robsonik16 | 0:a807bd13ba83 | 64 | str[index]=NULL; |
Robsonik16 | 0:a807bd13ba83 | 65 | return (str); |
Robsonik16 | 0:a807bd13ba83 | 66 | } |
Robsonik16 | 0:a807bd13ba83 | 67 | str[index]=c; |
Robsonik16 | 0:a807bd13ba83 | 68 | } |
Robsonik16 | 0:a807bd13ba83 | 69 | |
Robsonik16 | 0:a807bd13ba83 | 70 | } |
Robsonik16 | 0:a807bd13ba83 | 71 | |
Robsonik16 | 0:a807bd13ba83 | 72 | int main() |
Robsonik16 | 0:a807bd13ba83 | 73 | { |
Robsonik16 | 0:a807bd13ba83 | 74 | //user_button.rise(&button_pressed); |
Robsonik16 | 0:a807bd13ba83 | 75 | //user_button.fall(&button_released); |
Robsonik16 | 0:a807bd13ba83 | 76 | |
Robsonik16 | 0:a807bd13ba83 | 77 | //pc.format(8, Serial::None, 1); |
Robsonik16 | 0:a807bd13ba83 | 78 | |
Robsonik16 | 0:a807bd13ba83 | 79 | //pc.baud(9600); |
Robsonik16 | 0:a807bd13ba83 | 80 | |
Robsonik16 | 0:a807bd13ba83 | 81 | bool flag=false; |
Robsonik16 | 0:a807bd13ba83 | 82 | char text[15]; |
Robsonik16 | 0:a807bd13ba83 | 83 | text[0]='0'; |
Robsonik16 | 0:a807bd13ba83 | 84 | text[1]='1'; |
Robsonik16 | 0:a807bd13ba83 | 85 | text[2]='2'; |
Robsonik16 | 0:a807bd13ba83 | 86 | text[3]='3'; |
Robsonik16 | 0:a807bd13ba83 | 87 | text[4]='4'; |
Robsonik16 | 0:a807bd13ba83 | 88 | text[5]='5'; |
Robsonik16 | 0:a807bd13ba83 | 89 | text[6]='6'; |
Robsonik16 | 0:a807bd13ba83 | 90 | text[7]=0; |
Robsonik16 | 0:a807bd13ba83 | 91 | |
Robsonik16 | 0:a807bd13ba83 | 92 | while(1) { |
Robsonik16 | 0:a807bd13ba83 | 93 | |
Robsonik16 | 0:a807bd13ba83 | 94 | //pc.printf("This program runs since %d seconds.\r\n", i); |
Robsonik16 | 0:a807bd13ba83 | 95 | |
Robsonik16 | 0:a807bd13ba83 | 96 | |
Robsonik16 | 0:a807bd13ba83 | 97 | //wait(1); |
Robsonik16 | 0:a807bd13ba83 | 98 | |
Robsonik16 | 0:a807bd13ba83 | 99 | |
Robsonik16 | 0:a807bd13ba83 | 100 | |
Robsonik16 | 0:a807bd13ba83 | 101 | |
Robsonik16 | 0:a807bd13ba83 | 102 | text = my_gets(text); |
Robsonik16 | 0:a807bd13ba83 | 103 | if (text!=NULL) my_puts(text,size); |
Robsonik16 | 0:a807bd13ba83 | 104 | |
Robsonik16 | 0:a807bd13ba83 | 105 | /* |
Robsonik16 | 0:a807bd13ba83 | 106 | if (pc.readable()){ |
Robsonik16 | 0:a807bd13ba83 | 107 | char c =pc.getc (); |
Robsonik16 | 0:a807bd13ba83 | 108 | pc.putc(c); |
Robsonik16 | 0:a807bd13ba83 | 109 | if (c == 's')flag = true; |
Robsonik16 | 0:a807bd13ba83 | 110 | if (c == 'r')flag = false; |
Robsonik16 | 0:a807bd13ba83 | 111 | if (c == 't')flag=!flag; |
Robsonik16 | 0:a807bd13ba83 | 112 | led_green =flag; |
Robsonik16 | 0:a807bd13ba83 | 113 | int size = 8; |
Robsonik16 | 0:a807bd13ba83 | 114 | puts2(text,size); |
Robsonik16 | 0:a807bd13ba83 | 115 | //int puts(char *tab,int size){ |
Robsonik16 | 0:a807bd13ba83 | 116 | |
Robsonik16 | 0:a807bd13ba83 | 117 | } |
Robsonik16 | 0:a807bd13ba83 | 118 | |
Robsonik16 | 0:a807bd13ba83 | 119 | */ |
Robsonik16 | 0:a807bd13ba83 | 120 | |
Robsonik16 | 0:a807bd13ba83 | 121 | |
Robsonik16 | 0:a807bd13ba83 | 122 | /* |
Robsonik16 | 0:a807bd13ba83 | 123 | for(char a = 'a';a<='z';a++){ |
Robsonik16 | 0:a807bd13ba83 | 124 | pc.putc(a); |
Robsonik16 | 0:a807bd13ba83 | 125 | pc.putc('\r'); |
Robsonik16 | 0:a807bd13ba83 | 126 | pc.putc('\n'); |
Robsonik16 | 0:a807bd13ba83 | 127 | wait(0.1); |
Robsonik16 | 0:a807bd13ba83 | 128 | } |
Robsonik16 | 0:a807bd13ba83 | 129 | */ |
Robsonik16 | 0:a807bd13ba83 | 130 | } |
Robsonik16 | 0:a807bd13ba83 | 131 | } |
Robsonik16 | 0:a807bd13ba83 | 132 |