ジャパンオープン用のLPC824専用プログラム

Dependencies:   mbed-src Ping SDFileSystem

Revision:
1:ea7307724f1c
Parent:
0:6c79a352010b
Child:
2:83b8bf9f0ea1
--- a/main/format.cpp	Sat Feb 27 06:35:13 2016 +0000
+++ b/main/format.cpp	Sun Feb 28 06:38:03 2016 +0000
@@ -1,78 +1,6 @@
 #include "mbed.h"
 #include "extern.h"
 
-#ifdef SD_CARD
-void Sd_System(void){
-    
-    char buf[BUFSIZE];
-    uint16_t FileData[FDATA_NUM]={0}, num, data;
-    uint8_t spi_num, h_byte, l_byte, val, packet;
-    int i;
-    FILE *fp;
-    char *fname="/sd/mydir/sdtest.txt";
-    char *dname="/sd/mydir";
-
-    mkdir(dname, 0777);
-
-    pc.printf("Hello World!\n");
-
-    fp = fopen(fname, "r");
-        if(fp == NULL) {
-            error("Could not open file for read\n");
-        }
-        while(fgets(buf, sizeof(buf), fp) != NULL){
-            if(buf[0] == '#') continue;
-            
-            num = atol(strtok(buf, ","));
-            data = atol(strtok(NULL, "\r\n\0"));
-            
-            if(num<FDATA_NUM){
-                FileData[num] = data;
-                pc.printf("%d, %ld\n", num, FileData[num]);
-            }
-            else{
-                continue;
-            }
-        }
-        fclose(fp);
-        
-    while(1){
-        
-        val = nucleo.receive();
-        if(!val) continue;
-        
-        spi_num = nucleo.read();
-        h_byte = nucleo.read();
-        l_byte = nucleo.read();
-        
-        if((spi_num&0x80)>>7 == 1){//writing to sd
-            spi_num = spi_num & 0x7F;
-            if(spi_num<FDATA_NUM){
-                FileData[spi_num] = (h_byte<<8) | l_byte;
-                //pc.printf("%d, %ld\n", num, FileData[num]);
-                fp = fopen(fname, "w");
-                for(i=0; i<FDATA_NUM; i++) fprintf(fp, "%d, %ld\n", i, FileData[i]);
-                fclose(fp);
-            }
-        }
-        else{//reading from sd
-            spi_num = spi_num & 0x7F;
-            if(spi_num<FDATA_NUM){
-                h_byte = (FileData[spi_num] & 0xFF00)>>8;
-                l_byte = (FileData[spi_num] & 0x00FF);
-
-                packet = h_byte;
-                nucleo.reply(packet);
-
-                packet = l_byte;
-                nucleo.reply(packet);
-            }
-        }
-        //pc.printf("Goodbye World!\n");
-    }
-}
-#endif /*SD_CARD*/
-
 #if defined(ULTRA_SONIC) || defined(ULTRA_SONIC_2)
 
 uint16_t packet, val, order;
@@ -128,39 +56,18 @@
         //emergency();
     }
 }
-
 #endif /*(ULTRA_SONIC) || (ULTRA_SONIC_2)*/
 
-#ifdef COLOR_SENSOR
-void Color_System(void){
-    uint8_t val, order, packet;
-    uint16_t data;
-
-    while(1){
-        while(1){
-            val = nucleo.receive();
-            if(val){
-                order = nucleo.read();
-                order %= SENSOR_X_COLOR;
-                data = ColorSensor[order].read_u16();
-                packet = (data & 0xFF00)>>8;
-                nucleo.reply(packet);
-            }
-            else{
-                break;
-            }
-        }
-    }
-}
-#endif /*COLOR_SENSOR*/
 
 #ifdef IR_SENSOR
 
 uint8_t ir_notice=0;//4bit
+uint8_t ir_val_phase[2];
 static uint8_t const ir_key=IR_KEY;//4bit
 uint8_t ir_position[2]={0};//4bit*2
 
 void emergency(void){
+    
     uint16_t order,packet,val;
     //ir_key=0xA;
     //ir_notice=0xC;
@@ -170,8 +77,17 @@
     if(val==1){
         order = nucleo.read();
         if(order == 0xABCD){
-            //key/notice/long/short
-            packet = ((ir_key<<12)|(ir_notice<<8)|(ir_position[LONG_SPOT]<<4)|(ir_position[SHORT_SPOT]<<0))&0xFFFF;nucleo.reply(packet);
+            //key/phaseL/phaseS/long/short/
+            // 2/     3/     3/   4/    4/
+            packet = (
+                ((0xC000)&(ir_key<<14))                 |//1100000000000000
+                ((0x3800)&(ir_val_phase[IR_LONG]<<11))  |//0011100000000000
+                ((0x0700)&(ir_val_phase[IR_SHORT]<<8))  |//0000011100000000
+                ((0x00F0)&(ir_position[IR_LONG]<<4))    |//0000000011110000
+                ((0x000F)&(ir_position[IR_SHORT]<<0))    //0000000000001111
+            )&0xFFFF;
+            
+            nucleo.reply(packet);
         }
         else{
             packet = 0xFFFF;nucleo.reply(packet);
@@ -200,6 +116,11 @@
     uint8_t ir_posi_short[12];
     static uint8_t const ch_num[3]={8, 8, 4};
     
+    static uint16_t const ir_dis_range[2][8]={
+        {3900,  3000,  2750,  2500,  2250,  2000,  1500,  0,  },//IR_SHORT 0
+        {2000,  1750,  1500,  1250,  1000,  750,   500,   0,  },//IR_LONG 1
+    }
+    
     nucleo.format(16, 3);
     nucleo.frequency(1000000);
     nucleo.reply(0x0000);
@@ -246,8 +167,8 @@
         //検出した最大値と最小値の差
         ir_val_diff = ir_val_short[0]-ir_val_short[11];//After bubble
         //最もボールがあるとされる位置を代入
-        ir_position[SHORT_SPOT] = ir_posi_short[11];//After bubble
-        ir_position[LONG_SPOT] = ir_posi_long[7];//After bubble
+        ir_position[IR_SHORT] = ir_posi_short[11];//After bubble
+        ir_position[IR_LONG] = ir_posi_long[7];//After bubble
         
         /*
         #define IR_NOTE_NONE    0x0
@@ -255,7 +176,67 @@
         #define IR_NOTE_CLOSE   0x2
         #define IR_NOTE_CLOSER  0x3
         */
-        //ボールが遠くにあるか,近くか,フィールドにないかを判断
+        //ボールが遠くにあるか,近くか,フィールドにないかを判断(8段階)
+        
+        
+        //short
+        ir_val_phase[IR_SHORT] = DIS_7;
+        
+        if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_7]){
+            ir_val_phase[IR_SHORT] = DIS_7;
+        }
+        else if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_6]){
+            ir_val_phase[IR_SHORT] = DIS_6;
+        }
+        else if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_5]){
+            ir_val_phase[IR_SHORT] = DIS_5;
+        }
+        else if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_4]){
+            ir_val_phase[IR_SHORT] = DIS_4;
+        }
+        else if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_3]){
+            ir_val_phase[IR_SHORT] = DIS_3;
+        }
+        else if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_2]){
+            ir_val_phase[IR_SHORT] = DIS_2;
+        }
+        else if(ir_val_short[11]>=ir_dis_range[IR_SHORT][DIS_1]){
+            ir_val_phase[IR_SHORT] = DIS_1;
+        }
+        else{//ir_val_short[11]>=0
+            ir_val_phase[IR_SHORT] = DIS_0;
+        }
+        
+        //long
+        ir_val_phase[IR_LONG] = DIS_7;
+        
+        if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_7]){
+            ir_val_phase[IR_LONG] = DIS_7;
+        }
+        else if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_6]){
+            ir_val_phase[IR_LONG] = DIS_6;
+        }
+        else if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_5]){
+            ir_val_phase[IR_LONG] = DIS_5;
+        }
+        else if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_4]){
+            ir_val_phase[IR_LONG] = DIS_4;
+        }
+        else if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_3]){
+            ir_val_phase[IR_LONG] = DIS_3;
+        }
+        else if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_2]){
+            ir_val_phase[IR_LONG] = DIS_2;
+        }
+        else if(ir_val_long[7]>=ir_dis_range[IR_LONG][DIS_1]){
+            ir_val_phase[IR_LONG] = DIS_1;
+        }
+        else{//ir_val_long[7]>=0
+            ir_val_phase[IR_LONG] = DIS_0;
+        }
+        
+        
+        //old
         ir_notice = IR_NOTE_NONE;//none
         
         if((ir_val_short[11]>=3900)&&(1)){
@@ -357,7 +338,7 @@
     };
     return ir_location[ic][channel];
 }
-void BubbleSort(uint16_t *data, uint8_t *data2, uint8_t n)//昇順にする
+void BubbleSort(uint16_t *data, uint8_t *data2, uint8_t n)//降順にする
 {
     bool flag;
     uint8_t i, j;//inclement