Save IR data

Dependencies:   mbed

Committer:
halfpitch
Date:
Wed Aug 31 15:50:30 2011 +0000
Revision:
0:decd8d14cc06
Rev.A

Who changed what in which revision?

UserRevisionLine numberNew contents of line
halfpitch 0:decd8d14cc06 1 //////////////////////////////////////
halfpitch 0:decd8d14cc06 2 // White Wizard Board (PAT.PEND) //
halfpitch 0:decd8d14cc06 3 // Save IR data to microSD card. //
halfpitch 0:decd8d14cc06 4 // //
halfpitch 0:decd8d14cc06 5 // Nest Egg Inc. 2011/7/9 //
halfpitch 0:decd8d14cc06 6 // //
halfpitch 0:decd8d14cc06 7 //////////////////////////////////////
halfpitch 0:decd8d14cc06 8 #include "mbed.h"
halfpitch 0:decd8d14cc06 9 #include "SDFileSystem.h"
halfpitch 0:decd8d14cc06 10
halfpitch 0:decd8d14cc06 11 #define SDch 0x05
halfpitch 0:decd8d14cc06 12 #define MAXdatanum 1000
halfpitch 0:decd8d14cc06 13 #define TimeLimit 3000
halfpitch 0:decd8d14cc06 14 #define pul38khz 0.0263
halfpitch 0:decd8d14cc06 15 #define pul38k_ON 0.0131
halfpitch 0:decd8d14cc06 16 #define pul38k_OFF 0.0
halfpitch 0:decd8d14cc06 17 //#define TEST 1
halfpitch 0:decd8d14cc06 18
halfpitch 0:decd8d14cc06 19 //prototype
halfpitch 0:decd8d14cc06 20 void SDsave(int[], int[], int);
halfpitch 0:decd8d14cc06 21 void send_pulse(int, int);
halfpitch 0:decd8d14cc06 22
halfpitch 0:decd8d14cc06 23 SDFileSystem sd(p5, p6, p7, p8, "sd");
halfpitch 0:decd8d14cc06 24
halfpitch 0:decd8d14cc06 25 //DigitalIn IR(p9);
halfpitch 0:decd8d14cc06 26
halfpitch 0:decd8d14cc06 27 InterruptIn IR(p9);
halfpitch 0:decd8d14cc06 28
halfpitch 0:decd8d14cc06 29 DigitalOut myled1(LED1);
halfpitch 0:decd8d14cc06 30 DigitalOut myled2(LED2);
halfpitch 0:decd8d14cc06 31 DigitalOut myled3(LED3);
halfpitch 0:decd8d14cc06 32 DigitalOut myled4(LED4);
halfpitch 0:decd8d14cc06 33
halfpitch 0:decd8d14cc06 34 PwmOut Pulse_pin(p21);
halfpitch 0:decd8d14cc06 35
halfpitch 0:decd8d14cc06 36 int trigger = 0;
halfpitch 0:decd8d14cc06 37 int saved = 0;
halfpitch 0:decd8d14cc06 38 int datanum = 0;
halfpitch 0:decd8d14cc06 39 int i = 0;
halfpitch 0:decd8d14cc06 40 int IRdata[MAXdatanum];
halfpitch 0:decd8d14cc06 41 int pulsetime[MAXdatanum];
halfpitch 0:decd8d14cc06 42
halfpitch 0:decd8d14cc06 43
halfpitch 0:decd8d14cc06 44 void triggerOn() {
halfpitch 0:decd8d14cc06 45 trigger = 1;
halfpitch 0:decd8d14cc06 46 IRdata[i] = 0;
halfpitch 0:decd8d14cc06 47 pulsetime[i]=0;
halfpitch 0:decd8d14cc06 48
halfpitch 0:decd8d14cc06 49 if(i < MAXdatanum){
halfpitch 0:decd8d14cc06 50 while(IR==0 && pulsetime[i]<=15000){
halfpitch 0:decd8d14cc06 51 ++pulsetime[i];
halfpitch 0:decd8d14cc06 52 wait_us(50);
halfpitch 0:decd8d14cc06 53
halfpitch 0:decd8d14cc06 54 }
halfpitch 0:decd8d14cc06 55
halfpitch 0:decd8d14cc06 56 if(pulsetime[i]>=15000){
halfpitch 0:decd8d14cc06 57 printf(" pulsetime[i] >= TimeLimit !\n");
halfpitch 0:decd8d14cc06 58 datanum = i;
halfpitch 0:decd8d14cc06 59 i = MAXdatanum;
halfpitch 0:decd8d14cc06 60 }
halfpitch 0:decd8d14cc06 61
halfpitch 0:decd8d14cc06 62 ++i;
halfpitch 0:decd8d14cc06 63 }
halfpitch 0:decd8d14cc06 64 }
halfpitch 0:decd8d14cc06 65
halfpitch 0:decd8d14cc06 66
halfpitch 0:decd8d14cc06 67 void triggerOff() {
halfpitch 0:decd8d14cc06 68 trigger = 0;
halfpitch 0:decd8d14cc06 69 pulsetime[i]=0;
halfpitch 0:decd8d14cc06 70 IRdata[i] = 1;
halfpitch 0:decd8d14cc06 71
halfpitch 0:decd8d14cc06 72 if(i < MAXdatanum){
halfpitch 0:decd8d14cc06 73 while(IR==1 && pulsetime[i]<=15000){
halfpitch 0:decd8d14cc06 74 ++pulsetime[i];
halfpitch 0:decd8d14cc06 75 wait_us(50);
halfpitch 0:decd8d14cc06 76
halfpitch 0:decd8d14cc06 77 }
halfpitch 0:decd8d14cc06 78
halfpitch 0:decd8d14cc06 79 if(pulsetime[i]>=15000){
halfpitch 0:decd8d14cc06 80 printf(" pulsetime[i] >= TimeLimit !\n");
halfpitch 0:decd8d14cc06 81 datanum = i;
halfpitch 0:decd8d14cc06 82 i = MAXdatanum;
halfpitch 0:decd8d14cc06 83 }
halfpitch 0:decd8d14cc06 84
halfpitch 0:decd8d14cc06 85 ++i;
halfpitch 0:decd8d14cc06 86 }
halfpitch 0:decd8d14cc06 87 }
halfpitch 0:decd8d14cc06 88
halfpitch 0:decd8d14cc06 89
halfpitch 0:decd8d14cc06 90 int main(){
halfpitch 0:decd8d14cc06 91 //Buffer is 100 pulses.
halfpitch 0:decd8d14cc06 92 //int IRdata[MAXdatanum];
halfpitch 0:decd8d14cc06 93 //int pulsetime[MAXdatanum];
halfpitch 0:decd8d14cc06 94 //int i = 0;
halfpitch 0:decd8d14cc06 95
halfpitch 0:decd8d14cc06 96 myled1 = 0;
halfpitch 0:decd8d14cc06 97 myled2 = 0;
halfpitch 0:decd8d14cc06 98 myled3 = 0;
halfpitch 0:decd8d14cc06 99 myled4 = 0;
halfpitch 0:decd8d14cc06 100
halfpitch 0:decd8d14cc06 101 myled1 = 1;
halfpitch 0:decd8d14cc06 102
halfpitch 0:decd8d14cc06 103 IR.fall(&triggerOn);
halfpitch 0:decd8d14cc06 104 IR.rise(&triggerOff);
halfpitch 0:decd8d14cc06 105
halfpitch 0:decd8d14cc06 106 printf("start!\n");
halfpitch 0:decd8d14cc06 107
halfpitch 0:decd8d14cc06 108 while(1){
halfpitch 0:decd8d14cc06 109 IR.fall(&triggerOn);
halfpitch 0:decd8d14cc06 110 IR.rise(&triggerOff);
halfpitch 0:decd8d14cc06 111
halfpitch 0:decd8d14cc06 112 if(i >= MAXdatanum && i!= (MAXdatanum + 100)){
halfpitch 0:decd8d14cc06 113 printf("Save start!\n");
halfpitch 0:decd8d14cc06 114 SDsave(IRdata, pulsetime,datanum);
halfpitch 0:decd8d14cc06 115 i = MAXdatanum + 100;
halfpitch 0:decd8d14cc06 116 printf("Finish!");
halfpitch 0:decd8d14cc06 117 }
halfpitch 0:decd8d14cc06 118 }
halfpitch 0:decd8d14cc06 119 #ifdef TEST
halfpitch 0:decd8d14cc06 120
halfpitch 0:decd8d14cc06 121 #else
halfpitch 0:decd8d14cc06 122 while(1){
halfpitch 0:decd8d14cc06 123
halfpitch 0:decd8d14cc06 124 if(trigger == 0 && saved == 0){ //no signal
halfpitch 0:decd8d14cc06 125 ;
halfpitch 0:decd8d14cc06 126 }else if(trigger == 1 && saved == 0){ //"save" start
halfpitch 0:decd8d14cc06 127 //IR.fall(NULL);
halfpitch 0:decd8d14cc06 128 for(i = 0; i < MAXdatanum; ++i){
halfpitch 0:decd8d14cc06 129 pulsetime[i] = 0; //init
halfpitch 0:decd8d14cc06 130
halfpitch 0:decd8d14cc06 131 if(IR == 1){
halfpitch 0:decd8d14cc06 132 //check signal type
halfpitch 0:decd8d14cc06 133 IRdata[i] = 1;
halfpitch 0:decd8d14cc06 134
halfpitch 0:decd8d14cc06 135 while(IR==1 && pulsetime[i]<=TimeLimit){ //while IR = 1
halfpitch 0:decd8d14cc06 136 //check time
halfpitch 0:decd8d14cc06 137 ++pulsetime[i];
halfpitch 0:decd8d14cc06 138 wait_us(50); //count by 100 usec
halfpitch 0:decd8d14cc06 139 }
halfpitch 0:decd8d14cc06 140
halfpitch 0:decd8d14cc06 141
halfpitch 0:decd8d14cc06 142 }else{
halfpitch 0:decd8d14cc06 143 //check signal type
halfpitch 0:decd8d14cc06 144 IRdata[i] = 0;
halfpitch 0:decd8d14cc06 145
halfpitch 0:decd8d14cc06 146 while(IR==0 && pulsetime[i]<=TimeLimit){ //while IR = 0
halfpitch 0:decd8d14cc06 147 //check time
halfpitch 0:decd8d14cc06 148 ++pulsetime[i];
halfpitch 0:decd8d14cc06 149 wait_us(50); //count by 100 usec
halfpitch 0:decd8d14cc06 150
halfpitch 0:decd8d14cc06 151 }
halfpitch 0:decd8d14cc06 152
halfpitch 0:decd8d14cc06 153 }
halfpitch 0:decd8d14cc06 154
halfpitch 0:decd8d14cc06 155 datanum = i;
halfpitch 0:decd8d14cc06 156
halfpitch 0:decd8d14cc06 157 if( pulsetime[i] >= TimeLimit){
halfpitch 0:decd8d14cc06 158 printf(" pulsetime[i] >= TimeLimit !\n");
halfpitch 0:decd8d14cc06 159 i = MAXdatanum;
halfpitch 0:decd8d14cc06 160 }
halfpitch 0:decd8d14cc06 161
halfpitch 0:decd8d14cc06 162 }
halfpitch 0:decd8d14cc06 163
halfpitch 0:decd8d14cc06 164 SDsave(IRdata, pulsetime,datanum);
halfpitch 0:decd8d14cc06 165 }else{
halfpitch 0:decd8d14cc06 166 ;
halfpitch 0:decd8d14cc06 167 }
halfpitch 0:decd8d14cc06 168 }
halfpitch 0:decd8d14cc06 169 #endif
halfpitch 0:decd8d14cc06 170
halfpitch 0:decd8d14cc06 171 #ifdef TEST
halfpitch 0:decd8d14cc06 172 IRdata[0] = 1;
halfpitch 0:decd8d14cc06 173 pulsetime[0] = 555;
halfpitch 0:decd8d14cc06 174 IRdata[1] = 0;
halfpitch 0:decd8d14cc06 175 pulsetime[1] = 777;
halfpitch 0:decd8d14cc06 176 SDsave(IRdata, pulsetime,2);
halfpitch 0:decd8d14cc06 177 #endif
halfpitch 0:decd8d14cc06 178
halfpitch 0:decd8d14cc06 179 while(1){
halfpitch 0:decd8d14cc06 180 }
halfpitch 0:decd8d14cc06 181
halfpitch 0:decd8d14cc06 182 }
halfpitch 0:decd8d14cc06 183
halfpitch 0:decd8d14cc06 184 void SDsave(int IRdata1[], int pulsetime1[], int datanum1){
halfpitch 0:decd8d14cc06 185
halfpitch 0:decd8d14cc06 186 int j = 0;
halfpitch 0:decd8d14cc06 187
halfpitch 0:decd8d14cc06 188 //channel No must be A4 A3 A2 A1
halfpitch 0:decd8d14cc06 189 sd.SetCh(SDch); //channel No 0101(=0x05), depend on your SPI module setting. (1:High 0:Low)
halfpitch 0:decd8d14cc06 190 wait(1);
halfpitch 0:decd8d14cc06 191
halfpitch 0:decd8d14cc06 192 printf("Start to save data\n");
halfpitch 0:decd8d14cc06 193 myled2 = 1;
halfpitch 0:decd8d14cc06 194
halfpitch 0:decd8d14cc06 195 FILE *fp = fopen("/sd/IRDATA.csv", "w");
halfpitch 0:decd8d14cc06 196 if(fp == NULL) {
halfpitch 0:decd8d14cc06 197 error("Could not open file to write\n");
halfpitch 0:decd8d14cc06 198 myled3 = 1;
halfpitch 0:decd8d14cc06 199 }
halfpitch 0:decd8d14cc06 200
halfpitch 0:decd8d14cc06 201 #ifdef TEST
halfpitch 0:decd8d14cc06 202 fprintf(fp, "%d\n",datanum1);
halfpitch 0:decd8d14cc06 203 fprintf(fp, "%d,%d,\n",IRdata1[0], pulsetime1[0]);
halfpitch 0:decd8d14cc06 204 fprintf(fp, "%d,%d,\n",IRdata1[1], pulsetime1[1]);
halfpitch 0:decd8d14cc06 205 #else
halfpitch 0:decd8d14cc06 206
halfpitch 0:decd8d14cc06 207 fprintf(fp, "%d\n",datanum1);
halfpitch 0:decd8d14cc06 208 for(j=0;j<datanum1;++j){
halfpitch 0:decd8d14cc06 209 fprintf(fp, "%d,%d,\n",IRdata1[j], pulsetime1[j]);
halfpitch 0:decd8d14cc06 210 }
halfpitch 0:decd8d14cc06 211 #endif
halfpitch 0:decd8d14cc06 212
halfpitch 0:decd8d14cc06 213
halfpitch 0:decd8d14cc06 214 fclose(fp);
halfpitch 0:decd8d14cc06 215 saved = 1;
halfpitch 0:decd8d14cc06 216 printf("Saved!\n");
halfpitch 0:decd8d14cc06 217
halfpitch 0:decd8d14cc06 218 sd.SetCh(0x00); //not select
halfpitch 0:decd8d14cc06 219
halfpitch 0:decd8d14cc06 220 //#ifdef TEST
halfpitch 0:decd8d14cc06 221 wait(1);
halfpitch 0:decd8d14cc06 222 sd.SetCh(SDch); //channel No 0101(=0x05), depend on your SPI module setting. (1:High 0:Low)
halfpitch 0:decd8d14cc06 223 printf("again\n");
halfpitch 0:decd8d14cc06 224 int pHL = 0;
halfpitch 0:decd8d14cc06 225 int t = 0;
halfpitch 0:decd8d14cc06 226 FILE *fp1 = fopen("/sd/IRDATA.csv", "r");
halfpitch 0:decd8d14cc06 227
halfpitch 0:decd8d14cc06 228 datanum = 0;
halfpitch 0:decd8d14cc06 229
halfpitch 0:decd8d14cc06 230 fscanf(fp1, "%d\n",&datanum1); //read from SD
halfpitch 0:decd8d14cc06 231 printf("datanum, %d\n",datanum1);
halfpitch 0:decd8d14cc06 232
halfpitch 0:decd8d14cc06 233 for(j=0;j<datanum1;++j){
halfpitch 0:decd8d14cc06 234 fscanf(fp1, "%d,%d,\n", &pHL, &t); //read from SD
halfpitch 0:decd8d14cc06 235
halfpitch 0:decd8d14cc06 236 send_pulse(pHL,t);
halfpitch 0:decd8d14cc06 237
halfpitch 0:decd8d14cc06 238 //printf("ans, %d,%d,\n", pHL, t);
halfpitch 0:decd8d14cc06 239 }
halfpitch 0:decd8d14cc06 240
halfpitch 0:decd8d14cc06 241 fclose(fp1);
halfpitch 0:decd8d14cc06 242 printf("close fp1\n");
halfpitch 0:decd8d14cc06 243
halfpitch 0:decd8d14cc06 244 //#endif
halfpitch 0:decd8d14cc06 245
halfpitch 0:decd8d14cc06 246 myled3 = 1;
halfpitch 0:decd8d14cc06 247 myled4 = 1;
halfpitch 0:decd8d14cc06 248 }
halfpitch 0:decd8d14cc06 249
halfpitch 0:decd8d14cc06 250
halfpitch 0:decd8d14cc06 251 void send_pulse(int pHL, int t){
halfpitch 0:decd8d14cc06 252
halfpitch 0:decd8d14cc06 253 int k = 0;
halfpitch 0:decd8d14cc06 254 int err = 0;
halfpitch 0:decd8d14cc06 255
halfpitch 0:decd8d14cc06 256 Pulse_pin.period(pul38khz);
halfpitch 0:decd8d14cc06 257
halfpitch 0:decd8d14cc06 258 if(pHL == 0){
halfpitch 0:decd8d14cc06 259 //38kHz pulse on
halfpitch 0:decd8d14cc06 260 Pulse_pin.pulsewidth(pul38k_ON);
halfpitch 0:decd8d14cc06 261
halfpitch 0:decd8d14cc06 262 }else if(pHL == 1){
halfpitch 0:decd8d14cc06 263 //38kHz pulse off
halfpitch 0:decd8d14cc06 264 Pulse_pin.pulsewidth(pul38k_OFF);
halfpitch 0:decd8d14cc06 265
halfpitch 0:decd8d14cc06 266 }else{
halfpitch 0:decd8d14cc06 267 Pulse_pin.pulsewidth(pul38k_OFF);
halfpitch 0:decd8d14cc06 268 printf("\n\n-----Data format is incorrect!!-----\n pHL = %d, t = %d \n", pHL, t);
halfpitch 0:decd8d14cc06 269 err = 1;
halfpitch 0:decd8d14cc06 270
halfpitch 0:decd8d14cc06 271 }
halfpitch 0:decd8d14cc06 272
halfpitch 0:decd8d14cc06 273 if(err == 0){
halfpitch 0:decd8d14cc06 274 for(k = 0; k < t; ++k){
halfpitch 0:decd8d14cc06 275 wait_us(50);
halfpitch 0:decd8d14cc06 276 }
halfpitch 0:decd8d14cc06 277 }
halfpitch 0:decd8d14cc06 278
halfpitch 0:decd8d14cc06 279 }
halfpitch 0:decd8d14cc06 280