Normal CS setting data for saving IR data.

Dependencies:   mbed

Committer:
halfpitch
Date:
Sat Sep 03 17:06:30 2011 +0000
Revision:
0:558646c716d0
Rev.A

Who changed what in which revision?

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