Publish test
Dependencies: BSP_DISCO_F746NG F746_GUI F746_SAI_IO LCD_DISCO_F746NG SD_DISCO_F746NG TS_DISCO_F746NG USBHost_DISCO-F746NG_joerg mbed-rtos mbed
Fork of rtos_basic by
main.cpp@10:4bcd31e78d4e, 2016-12-09 (annotated)
- Committer:
- schmalzl
- Date:
- Fri Dec 09 09:55:04 2016 +0000
- Revision:
- 10:4bcd31e78d4e
- Parent:
- 8:98bd91792454
Publish test Joerg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
emilmont | 1:491820ee784d | 2 | #include "rtos.h" |
schmalzl | 8:98bd91792454 | 3 | #include "USBHostMouseKeyboard.h" |
schmalzl | 8:98bd91792454 | 4 | #include "USBHostMSD.h" |
schmalzl | 8:98bd91792454 | 5 | #include "LCD_DISCO_F746NG.h" |
schmalzl | 8:98bd91792454 | 6 | #include "F746_GUI.hpp" |
schmalzl | 8:98bd91792454 | 7 | #include "SAI_InOut.hpp" |
schmalzl | 8:98bd91792454 | 8 | |
schmalzl | 8:98bd91792454 | 9 | using namespace Mikami; |
schmalzl | 8:98bd91792454 | 10 | |
schmalzl | 8:98bd91792454 | 11 | |
schmalzl | 8:98bd91792454 | 12 | LCD_DISCO_F746NG lcd; |
emilmont | 1:491820ee784d | 13 | |
emilmont | 1:491820ee784d | 14 | DigitalOut led1(LED1); |
emilmont | 1:491820ee784d | 15 | DigitalOut led2(LED2); |
schmalzl | 8:98bd91792454 | 16 | Thread read_adc; |
schmalzl | 8:98bd91792454 | 17 | Thread thread2; |
schmalzl | 8:98bd91792454 | 18 | const int FS = I2S_AUDIOFREQ_16K; // 16 kHz |
schmalzl | 8:98bd91792454 | 19 | |
schmalzl | 8:98bd91792454 | 20 | int data_length; // length of adc_data |
schmalzl | 8:98bd91792454 | 21 | Array<float> adc_data(513); // data - read from mic .. write to usb-stick |
schmalzl | 8:98bd91792454 | 22 | Mutex data_mutex; |
schmalzl | 8:98bd91792454 | 23 | |
schmalzl | 8:98bd91792454 | 24 | #define FastSpeedInterface 0 |
schmalzl | 8:98bd91792454 | 25 | #define HighSpeedInterface 1 |
schmalzl | 8:98bd91792454 | 26 | |
schmalzl | 8:98bd91792454 | 27 | void usb_task(void const *) { |
schmalzl | 8:98bd91792454 | 28 | |
schmalzl | 8:98bd91792454 | 29 | USBHostMSD msd("usb",HighSpeedInterface); |
schmalzl | 8:98bd91792454 | 30 | // USBHostMSD msd("usb",FastSpeedInterface); |
schmalzl | 8:98bd91792454 | 31 | |
schmalzl | 8:98bd91792454 | 32 | int i = 0; |
schmalzl | 8:98bd91792454 | 33 | int res; |
schmalzl | 8:98bd91792454 | 34 | int error=0,nBlocksWritten ; |
schmalzl | 8:98bd91792454 | 35 | char buf[50], buf2[50]; |
schmalzl | 8:98bd91792454 | 36 | while(1) { |
schmalzl | 8:98bd91792454 | 37 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"USB connect", CENTER_MODE); |
schmalzl | 8:98bd91792454 | 38 | |
schmalzl | 8:98bd91792454 | 39 | // try to connect a MSD device |
schmalzl | 8:98bd91792454 | 40 | while(!msd.connect()) { |
schmalzl | 8:98bd91792454 | 41 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"USB Waiting", CENTER_MODE); |
schmalzl | 8:98bd91792454 | 42 | |
schmalzl | 8:98bd91792454 | 43 | Thread::wait(500); |
schmalzl | 8:98bd91792454 | 44 | printf("waiting\n"); |
schmalzl | 8:98bd91792454 | 45 | } |
schmalzl | 8:98bd91792454 | 46 | |
schmalzl | 8:98bd91792454 | 47 | wait_ms(500); |
schmalzl | 8:98bd91792454 | 48 | msd.mount() ; |
schmalzl | 8:98bd91792454 | 49 | wait_ms(500); |
schmalzl | 8:98bd91792454 | 50 | |
schmalzl | 8:98bd91792454 | 51 | |
schmalzl | 8:98bd91792454 | 52 | // open a file |
schmalzl | 8:98bd91792454 | 53 | srand(time(NULL)); |
schmalzl | 8:98bd91792454 | 54 | sprintf(buf2,"/usb/test%i.txt\0", rand()%4); |
schmalzl | 8:98bd91792454 | 55 | FILE * fp = fopen(buf2, "w"); |
schmalzl | 8:98bd91792454 | 56 | lcd.DisplayStringAt(0, LINE(11), (uint8_t *)buf2, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 57 | // in a loop, append a file |
schmalzl | 8:98bd91792454 | 58 | // if the device is disconnected, we try to connect it again |
schmalzl | 8:98bd91792454 | 59 | |
schmalzl | 8:98bd91792454 | 60 | |
schmalzl | 8:98bd91792454 | 61 | while(1) { |
schmalzl | 8:98bd91792454 | 62 | |
schmalzl | 8:98bd91792454 | 63 | |
schmalzl | 8:98bd91792454 | 64 | |
schmalzl | 8:98bd91792454 | 65 | if (fp != NULL) { |
schmalzl | 8:98bd91792454 | 66 | i++; |
schmalzl | 8:98bd91792454 | 67 | |
schmalzl | 8:98bd91792454 | 68 | data_mutex.lock(); |
schmalzl | 8:98bd91792454 | 69 | |
schmalzl | 8:98bd91792454 | 70 | sprintf(buf, "Vor dem fwrite %i length %i \0", i, data_length); |
schmalzl | 8:98bd91792454 | 71 | lcd.DisplayStringAt(0, LINE(4), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 72 | |
schmalzl | 8:98bd91792454 | 73 | // Write the data block |
schmalzl | 8:98bd91792454 | 74 | nBlocksWritten = fwrite( |
schmalzl | 8:98bd91792454 | 75 | adc_data, // const void *ptr |
schmalzl | 8:98bd91792454 | 76 | 2, // size_t size 2byte=float |
schmalzl | 8:98bd91792454 | 77 | data_length, // size_t nmemb |
schmalzl | 8:98bd91792454 | 78 | fp // FILE *stream |
schmalzl | 8:98bd91792454 | 79 | ); |
schmalzl | 8:98bd91792454 | 80 | // fflush(fp); |
schmalzl | 8:98bd91792454 | 81 | |
schmalzl | 8:98bd91792454 | 82 | data_length = 0; |
schmalzl | 8:98bd91792454 | 83 | |
schmalzl | 8:98bd91792454 | 84 | |
schmalzl | 8:98bd91792454 | 85 | sprintf(buf, "Nach fwrite %i length %i \0",i, nBlocksWritten); |
schmalzl | 8:98bd91792454 | 86 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)buf, CENTER_MODE); |
emilmont | 1:491820ee784d | 87 | |
schmalzl | 8:98bd91792454 | 88 | Thread::wait(100); |
schmalzl | 8:98bd91792454 | 89 | |
schmalzl | 8:98bd91792454 | 90 | data_mutex.unlock(); |
schmalzl | 8:98bd91792454 | 91 | /* |
schmalzl | 8:98bd91792454 | 92 | if (error==1) |
schmalzl | 8:98bd91792454 | 93 | { |
schmalzl | 8:98bd91792454 | 94 | sprintf(buf,"Goodbye New World!\r\n"); |
schmalzl | 8:98bd91792454 | 95 | lcd.DisplayStringAt(0, LINE(7), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 96 | |
schmalzl | 8:98bd91792454 | 97 | error=0; |
schmalzl | 8:98bd91792454 | 98 | } |
schmalzl | 8:98bd91792454 | 99 | fclose(fp); |
schmalzl | 8:98bd91792454 | 100 | */ |
schmalzl | 8:98bd91792454 | 101 | /* if(res<0) |
schmalzl | 8:98bd91792454 | 102 | { |
schmalzl | 8:98bd91792454 | 103 | sprintf(buf,"FILE write ERROR\r\n"); |
schmalzl | 8:98bd91792454 | 104 | lcd.DisplayStringAt(0, LINE(7), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 105 | |
schmalzl | 8:98bd91792454 | 106 | msd.unmount(); |
schmalzl | 8:98bd91792454 | 107 | wait_ms(5000); |
schmalzl | 8:98bd91792454 | 108 | msd.mount() ; |
schmalzl | 8:98bd91792454 | 109 | error=1; |
schmalzl | 8:98bd91792454 | 110 | } |
schmalzl | 8:98bd91792454 | 111 | */ |
schmalzl | 8:98bd91792454 | 112 | } else { |
schmalzl | 8:98bd91792454 | 113 | |
schmalzl | 8:98bd91792454 | 114 | /* |
schmalzl | 8:98bd91792454 | 115 | sprintf(buf, "FILE == NULL\r\n"); |
schmalzl | 8:98bd91792454 | 116 | lcd.DisplayStringAt(0, LINE(7), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 117 | |
schmalzl | 8:98bd91792454 | 118 | msd.unmount(); |
schmalzl | 8:98bd91792454 | 119 | wait_ms(5000); |
schmalzl | 8:98bd91792454 | 120 | msd.mount() ; |
schmalzl | 8:98bd91792454 | 121 | error=1; |
schmalzl | 8:98bd91792454 | 122 | */ |
schmalzl | 8:98bd91792454 | 123 | } |
schmalzl | 8:98bd91792454 | 124 | |
schmalzl | 8:98bd91792454 | 125 | Thread::wait(500); |
schmalzl | 8:98bd91792454 | 126 | |
schmalzl | 8:98bd91792454 | 127 | // if device disconnected, try to connect again |
schmalzl | 8:98bd91792454 | 128 | if (!msd.connected()) |
schmalzl | 8:98bd91792454 | 129 | break; |
schmalzl | 8:98bd91792454 | 130 | } |
schmalzl | 8:98bd91792454 | 131 | |
schmalzl | 8:98bd91792454 | 132 | |
schmalzl | 8:98bd91792454 | 133 | } |
schmalzl | 8:98bd91792454 | 134 | } |
schmalzl | 8:98bd91792454 | 135 | |
schmalzl | 8:98bd91792454 | 136 | |
schmalzl | 8:98bd91792454 | 137 | |
schmalzl | 8:98bd91792454 | 138 | void adc_thread() { |
schmalzl | 8:98bd91792454 | 139 | int i=0; |
schmalzl | 8:98bd91792454 | 140 | char buf[50]; |
schmalzl | 8:98bd91792454 | 141 | |
schmalzl | 8:98bd91792454 | 142 | |
schmalzl | 8:98bd91792454 | 143 | lcd.SetTextColor(LCD_COLOR_WHITE); |
schmalzl | 8:98bd91792454 | 144 | |
schmalzl | 8:98bd91792454 | 145 | SaiIO mySai(SaiIO::INPUT, 512+1, FS, |
schmalzl | 8:98bd91792454 | 146 | INPUT_DEVICE_DIGITAL_MICROPHONE_2); |
schmalzl | 8:98bd91792454 | 147 | Thread::wait(1000); |
schmalzl | 8:98bd91792454 | 148 | int16_t xLn, xRn; |
schmalzl | 8:98bd91792454 | 149 | mySai.Input(xLn, xRn); // haengt wenn ich das nicht mache .. warum ?? |
schmalzl | 8:98bd91792454 | 150 | |
schmalzl | 8:98bd91792454 | 151 | |
schmalzl | 8:98bd91792454 | 152 | while (1) { |
schmalzl | 8:98bd91792454 | 153 | data_mutex.lock(); |
schmalzl | 8:98bd91792454 | 154 | |
schmalzl | 8:98bd91792454 | 155 | i++; |
schmalzl | 8:98bd91792454 | 156 | |
schmalzl | 8:98bd91792454 | 157 | sprintf(buf,"adc_read .. vor read %i",i); |
schmalzl | 8:98bd91792454 | 158 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 159 | |
schmalzl | 8:98bd91792454 | 160 | data_length = mySai.GetLength(); |
schmalzl | 8:98bd91792454 | 161 | |
schmalzl | 8:98bd91792454 | 162 | for (int n=0; n<data_length; n++) |
schmalzl | 8:98bd91792454 | 163 | { |
schmalzl | 8:98bd91792454 | 164 | int16_t xL, xR; |
schmalzl | 8:98bd91792454 | 165 | mySai.Input(xL, xR); |
schmalzl | 8:98bd91792454 | 166 | adc_data[n] = (float)xL; |
schmalzl | 8:98bd91792454 | 167 | |
schmalzl | 8:98bd91792454 | 168 | sprintf(buf,"adc_read .. nach read %i length %i",n, data_length); |
schmalzl | 8:98bd91792454 | 169 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 170 | |
schmalzl | 8:98bd91792454 | 171 | |
schmalzl | 8:98bd91792454 | 172 | } |
schmalzl | 8:98bd91792454 | 173 | mySai.ResetCaptured(); |
schmalzl | 8:98bd91792454 | 174 | |
schmalzl | 8:98bd91792454 | 175 | |
schmalzl | 8:98bd91792454 | 176 | sprintf(buf,"adc_read .. nach read %i length %i",i, data_length); |
schmalzl | 8:98bd91792454 | 177 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 178 | Thread::wait(100); |
schmalzl | 8:98bd91792454 | 179 | data_mutex.unlock(); |
schmalzl | 8:98bd91792454 | 180 | |
emilmont | 1:491820ee784d | 181 | } |
emilmont | 1:491820ee784d | 182 | } |
emilmont | 1:491820ee784d | 183 | |
schmalzl | 8:98bd91792454 | 184 | void led2_thread2() { |
schmalzl | 8:98bd91792454 | 185 | int i=0; |
schmalzl | 8:98bd91792454 | 186 | char buf[50]; |
schmalzl | 8:98bd91792454 | 187 | lcd.SetTextColor(LCD_COLOR_WHITE); |
schmalzl | 8:98bd91792454 | 188 | |
emilmont | 1:491820ee784d | 189 | while (true) { |
schmalzl | 8:98bd91792454 | 190 | sprintf(buf,"In der Thread2 %i",i++); |
schmalzl | 8:98bd91792454 | 191 | led2 = !led2; |
schmalzl | 8:98bd91792454 | 192 | // lcd.Clear(LCD_COLOR_GREEN); |
schmalzl | 8:98bd91792454 | 193 | // lcd.DisplayStringAt(0, LINE(3), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 194 | Thread::wait(100); |
schmalzl | 8:98bd91792454 | 195 | |
emilmont | 1:491820ee784d | 196 | } |
emilmont | 1:491820ee784d | 197 | } |
schmalzl | 8:98bd91792454 | 198 | int main() { |
schmalzl | 8:98bd91792454 | 199 | int i=0; |
schmalzl | 8:98bd91792454 | 200 | char buf[50]; |
schmalzl | 8:98bd91792454 | 201 | |
schmalzl | 8:98bd91792454 | 202 | read_adc.start(adc_thread); |
schmalzl | 8:98bd91792454 | 203 | thread2.start(led2_thread2); |
schmalzl | 8:98bd91792454 | 204 | |
schmalzl | 8:98bd91792454 | 205 | // thread2.set_priority(osPriorityNormal); |
schmalzl | 8:98bd91792454 | 206 | |
schmalzl | 8:98bd91792454 | 207 | Thread msdTask(usb_task, NULL, osPriorityNormal, 1024 * 4); |
schmalzl | 8:98bd91792454 | 208 | |
schmalzl | 8:98bd91792454 | 209 | lcd.Clear(LCD_COLOR_BLACK); |
schmalzl | 8:98bd91792454 | 210 | lcd.SetBackColor(LCD_COLOR_BLACK); |
schmalzl | 8:98bd91792454 | 211 | lcd.SetTextColor(LCD_COLOR_WHITE); |
schmalzl | 8:98bd91792454 | 212 | ButtonGroup bG(400, 200, 50, 30, 2, (string[]){"ON", "OFF"}, 0, 5, 1, 0); |
schmalzl | 8:98bd91792454 | 213 | |
schmalzl | 8:98bd91792454 | 214 | while (true) { |
schmalzl | 8:98bd91792454 | 215 | |
schmalzl | 8:98bd91792454 | 216 | wait(0.3); |
schmalzl | 8:98bd91792454 | 217 | sprintf(buf,"In Main %i",i++); |
schmalzl | 8:98bd91792454 | 218 | int num; |
schmalzl | 8:98bd91792454 | 219 | if (bG.GetTouchedNumber(num)) sprintf(buf,"ist on"); |
schmalzl | 8:98bd91792454 | 220 | lcd.SetBackColor(LCD_COLOR_BLACK); |
schmalzl | 8:98bd91792454 | 221 | |
schmalzl | 8:98bd91792454 | 222 | lcd.DisplayStringAt(0, LINE(9), (uint8_t *)buf, CENTER_MODE); |
schmalzl | 8:98bd91792454 | 223 | wait(.1); |
schmalzl | 8:98bd91792454 | 224 | |
schmalzl | 8:98bd91792454 | 225 | if(!num) { |
schmalzl | 8:98bd91792454 | 226 | |
schmalzl | 8:98bd91792454 | 227 | } |
schmalzl | 8:98bd91792454 | 228 | |
schmalzl | 8:98bd91792454 | 229 | } |
schmalzl | 8:98bd91792454 | 230 | } |