![](/media/cache/profiles/cff02a74da64d145a4aed3a577a106ab.jpg.50x50_q85.jpg)
test
main.cpp@4:75077e05de3e, 2014-06-06 (annotated)
- Committer:
- fblanc
- Date:
- Fri Jun 06 13:10:57 2014 +0000
- Revision:
- 4:75077e05de3e
- Parent:
- 3:7acf6d1a6b1e
- Child:
- 5:832b59a73dec
USBserial and strncmp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 0:92dd9611bf80 | 1 | #include "mbed.h" |
fblanc | 4:75077e05de3e | 2 | #include "USBSerial.h" |
fblanc | 4:75077e05de3e | 3 | //#define BAUD 460800 |
fblanc | 4:75077e05de3e | 4 | //#define BAUD 9600 |
fblanc | 0:92dd9611bf80 | 5 | |
fblanc | 4:75077e05de3e | 6 | #define __LPC1768 |
fblanc | 4:75077e05de3e | 7 | #if defined( __LPC1768) |
fblanc | 4:75077e05de3e | 8 | //Serial pc(USBTX, USBRX); |
fblanc | 4:75077e05de3e | 9 | USBSerial pc; |
fblanc | 0:92dd9611bf80 | 10 | DigitalOut led2(LED2); |
fblanc | 0:92dd9611bf80 | 11 | DigitalOut led3(LED3); |
fblanc | 0:92dd9611bf80 | 12 | DigitalOut led4(LED4); |
fblanc | 3:7acf6d1a6b1e | 13 | AnalogIn adc_1(p16); |
fblanc | 3:7acf6d1a6b1e | 14 | AnalogIn adc_2(p17); |
fblanc | 4:75077e05de3e | 15 | #endif |
fblanc | 4:75077e05de3e | 16 | #if defined( __KL25) |
fblanc | 4:75077e05de3e | 17 | USBSerial pc; |
fblanc | 4:75077e05de3e | 18 | DigitalOut led2(LED_GREEN); |
fblanc | 4:75077e05de3e | 19 | DigitalOut led3(LED_RED); |
fblanc | 4:75077e05de3e | 20 | DigitalOut led4(LED_BLUE); |
fblanc | 4:75077e05de3e | 21 | AnalogIn adc_1(PTC1); |
fblanc | 4:75077e05de3e | 22 | AnalogIn adc_2(PTC2); |
fblanc | 4:75077e05de3e | 23 | #endif |
fblanc | 4:75077e05de3e | 24 | |
fblanc | 4:75077e05de3e | 25 | uint16_t T_hma_u16[2][2048]; |
fblanc | 0:92dd9611bf80 | 26 | uint16_t index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 27 | |
fblanc | 0:92dd9611bf80 | 28 | uint16_t index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 29 | bool t_write=0; |
fblanc | 0:92dd9611bf80 | 30 | char str[256]; |
fblanc | 0:92dd9611bf80 | 31 | uint8_t start_str=0; |
fblanc | 0:92dd9611bf80 | 32 | bool flag_write_USB=0; |
fblanc | 0:92dd9611bf80 | 33 | Ticker flipperADC; |
fblanc | 0:92dd9611bf80 | 34 | |
fblanc | 0:92dd9611bf80 | 35 | void flipADC() |
fblanc | 0:92dd9611bf80 | 36 | { |
fblanc | 4:75077e05de3e | 37 | |
fblanc | 3:7acf6d1a6b1e | 38 | T_hma_u16[t_write][index_hma_write++]=adc_1.read_u16(); //read ADC |
fblanc | 3:7acf6d1a6b1e | 39 | T_hma_u16[t_write][index_hma_write++]=adc_2.read_u16(); //read ADC |
fblanc | 0:92dd9611bf80 | 40 | //T_hma_u16[t_write][index_hma_write++]=0x00FF; |
fblanc | 0:92dd9611bf80 | 41 | |
fblanc | 3:7acf6d1a6b1e | 42 | if(index_hma_write>2000) { |
fblanc | 0:92dd9611bf80 | 43 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 44 | t_write=!t_write; //toggel buffer |
fblanc | 3:7acf6d1a6b1e | 45 | index_hma_read=index_hma_write-2; |
fblanc | 0:92dd9611bf80 | 46 | index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 47 | } |
fblanc | 4:75077e05de3e | 48 | |
fblanc | 0:92dd9611bf80 | 49 | } |
fblanc | 0:92dd9611bf80 | 50 | |
fblanc | 0:92dd9611bf80 | 51 | Ticker flipperUSB; |
fblanc | 0:92dd9611bf80 | 52 | void flipUSB() |
fblanc | 0:92dd9611bf80 | 53 | { |
fblanc | 0:92dd9611bf80 | 54 | |
fblanc | 0:92dd9611bf80 | 55 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 56 | |
fblanc | 0:92dd9611bf80 | 57 | } |
fblanc | 0:92dd9611bf80 | 58 | |
fblanc | 0:92dd9611bf80 | 59 | void pc_rx(void) |
fblanc | 0:92dd9611bf80 | 60 | { |
fblanc | 0:92dd9611bf80 | 61 | |
fblanc | 4:75077e05de3e | 62 | led4=1; |
fblanc | 0:92dd9611bf80 | 63 | for(; pc.readable()==true; ++start_str) { |
fblanc | 0:92dd9611bf80 | 64 | str[start_str]=pc.getc(); |
fblanc | 0:92dd9611bf80 | 65 | } |
fblanc | 4:75077e05de3e | 66 | //pc.printf("recv:%s",str); |
fblanc | 4:75077e05de3e | 67 | if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) { |
fblanc | 4:75077e05de3e | 68 | //pc.printf("HMA\r"); |
fblanc | 4:75077e05de3e | 69 | led3=1; |
fblanc | 4:75077e05de3e | 70 | pc.printf("HMA serial\r"); |
fblanc | 0:92dd9611bf80 | 71 | start_str=0; |
fblanc | 0:92dd9611bf80 | 72 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 73 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 74 | |
fblanc | 0:92dd9611bf80 | 75 | } |
fblanc | 4:75077e05de3e | 76 | |
fblanc | 4:75077e05de3e | 77 | } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) { |
fblanc | 0:92dd9611bf80 | 78 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 79 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 80 | |
fblanc | 0:92dd9611bf80 | 81 | } |
fblanc | 0:92dd9611bf80 | 82 | flipperADC.attach_us(&flipADC, 1000); //500us |
fblanc | 4:75077e05de3e | 83 | |
fblanc | 0:92dd9611bf80 | 84 | |
fblanc | 4:75077e05de3e | 85 | } else if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) { |
fblanc | 0:92dd9611bf80 | 86 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 87 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 88 | |
fblanc | 0:92dd9611bf80 | 89 | } |
fblanc | 0:92dd9611bf80 | 90 | flipperADC.detach(); |
fblanc | 0:92dd9611bf80 | 91 | t_write=0; |
fblanc | 0:92dd9611bf80 | 92 | index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 93 | index_hma_write=0; |
fblanc | 4:75077e05de3e | 94 | |
fblanc | 0:92dd9611bf80 | 95 | } |
fblanc | 0:92dd9611bf80 | 96 | for(int i=0; i<start_str; ++i) { |
fblanc | 0:92dd9611bf80 | 97 | if(str[i]=='\r') { |
fblanc | 0:92dd9611bf80 | 98 | for(int j=0; j<256; ++j) { |
fblanc | 0:92dd9611bf80 | 99 | str[j]='\0'; |
fblanc | 0:92dd9611bf80 | 100 | |
fblanc | 0:92dd9611bf80 | 101 | } |
fblanc | 0:92dd9611bf80 | 102 | start_str=0; |
fblanc | 0:92dd9611bf80 | 103 | |
fblanc | 0:92dd9611bf80 | 104 | } |
fblanc | 0:92dd9611bf80 | 105 | |
fblanc | 0:92dd9611bf80 | 106 | } |
fblanc | 0:92dd9611bf80 | 107 | |
fblanc | 0:92dd9611bf80 | 108 | |
fblanc | 0:92dd9611bf80 | 109 | } |
fblanc | 0:92dd9611bf80 | 110 | |
fblanc | 0:92dd9611bf80 | 111 | int main() |
fblanc | 0:92dd9611bf80 | 112 | { |
fblanc | 4:75077e05de3e | 113 | led2=0; |
fblanc | 4:75077e05de3e | 114 | led3=0; |
fblanc | 4:75077e05de3e | 115 | led4=0; |
fblanc | 4:75077e05de3e | 116 | // pc.baud(BAUD); |
fblanc | 4:75077e05de3e | 117 | pc.printf("HMA serial\r"); |
fblanc | 0:92dd9611bf80 | 118 | for(int i=0; i<256; ++i) { |
fblanc | 0:92dd9611bf80 | 119 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 120 | |
fblanc | 0:92dd9611bf80 | 121 | } |
fblanc | 0:92dd9611bf80 | 122 | |
fblanc | 0:92dd9611bf80 | 123 | |
fblanc | 0:92dd9611bf80 | 124 | while(1) { |
fblanc | 4:75077e05de3e | 125 | |
fblanc | 0:92dd9611bf80 | 126 | if (pc.readable()) { |
fblanc | 0:92dd9611bf80 | 127 | pc_rx(); |
fblanc | 0:92dd9611bf80 | 128 | } |
fblanc | 0:92dd9611bf80 | 129 | if(flag_write_USB==1) { |
fblanc | 0:92dd9611bf80 | 130 | flag_write_USB=0; |
fblanc | 4:75077e05de3e | 131 | |
fblanc | 0:92dd9611bf80 | 132 | |
fblanc | 2:88f866289381 | 133 | pc.printf("%04d%c\r",index_hma_read,'\0'); //'\0'fin de chaine ASCII '\r' fin de transmission |
fblanc | 0:92dd9611bf80 | 134 | |
fblanc | 0:92dd9611bf80 | 135 | char* ptr=(char*)&T_hma_u16[!t_write][0]; |
fblanc | 0:92dd9611bf80 | 136 | |
fblanc | 0:92dd9611bf80 | 137 | for(uint16_t i=0; i<index_hma_read; ++i) { |
fblanc | 0:92dd9611bf80 | 138 | pc.putc(*ptr++); |
fblanc | 0:92dd9611bf80 | 139 | pc.putc(*ptr++); |
fblanc | 0:92dd9611bf80 | 140 | } |
fblanc | 0:92dd9611bf80 | 141 | |
fblanc | 0:92dd9611bf80 | 142 | } |
fblanc | 0:92dd9611bf80 | 143 | |
fblanc | 0:92dd9611bf80 | 144 | |
fblanc | 0:92dd9611bf80 | 145 | |
fblanc | 0:92dd9611bf80 | 146 | } |
fblanc | 0:92dd9611bf80 | 147 | } |