受け取りのコントローラのプログラム

Dependencies:   TextLCD mbed MultiSerial Pswitch_Lib

Revision:
6:08b5c5bdace9
Parent:
5:a5c324942570
Child:
7:8c15316bdba6
--- a/main.cpp	Thu Sep 25 08:20:34 2014 +0000
+++ b/main.cpp	Thu Sep 25 10:28:28 2014 +0000
@@ -6,12 +6,12 @@
 #define LINKCODE 0xCC
 #define LINK_INTERVAL 100
 #define KEYCODE 0xAA
-#define INTERVAL .5 //12
+/*#define INTERVAL .5 //12
 
 enum HAND{RIGHT=0, LEFT};
 enum FINGER{INDEX=0, MIDDLE};
 enum COMPASS{NORTH=0, EAST, SOUTH, WEST};
-enum COLOR{YELLOW=0, RED, GREEN, BLUE};
+enum COLOR{YELLOW=0, RED, GREEN, BLUE};*/
 
 uint8_t TXdata[DATA_NUM]={0};
 uint8_t RXdata[DATA_NUM]={0};
@@ -19,55 +19,59 @@
 uint8_t INdata[DATA_NUM]={0};
 uint8_t EXdata[DATA_NUM]={0};
 
-volatile uint8_t count=0;
-bool SW;
+//volatile uint8_t count=0;
+//bool SW;
+bool GAT=1;
 
-void SetUp();
+//void SetUp();
 void connect_check();
-uint8_t LinkBit(bool eight, bool seven, bool six, bool five, bool fore, bool three, bool two, bool one);
-void illumination();
+void GiveAndTake();
+//uint8_t LinkBit(bool eight, bool seven, bool six, bool five, bool fore, bool three, bool two, bool one);
+//void illumination();
 
-Ticker flick;
+//Ticker flick;
 BusOut LED(LED1, LED2, LED3, LED4);
 
+Ticker TAX;
+
 DigitalOut YLED(p6);
 
-DigitalIn CrossKey[4][2]={{p8, p30}, {p11, p29}, {p15, p28}, {p16, p27}};//p7, p12=reserve
-DigitalIn SideKey[2][2]={{p17, p20}, {p18, p19}};
+//DigitalIn CrossKey[4][2]={{p8, p30}, {p11, p29}, {p15, p28}, {p16, p27}};//p7, p12=reserve
+//DigitalIn SideKey[2][2]={{p17, p20}, {p18, p19}};
 DigitalIn Toggle(p5);
 
 MultiSerial Xbee(p9, p10);
-Serial pc(USBTX,USBRX);
+//Serial pc(USBTX,USBRX);
 
 
 int main()
 {
-    SetUp();
+    //SetUp();
     
     Toggle.mode(PullUp);
-    
+    TAX.attach(GiveAndTake, 0.1);
+    while(1);
     connect_check();
     
-    TextLCD lcd(p26, p25, p24, p23, p22, p21, TextLCD::LCD16x2); // rs, e, d4-d7
+    //TextLCD lcd(p26, p25, p24, p23, p22, p21, TextLCD::LCD16x2); // rs, e, d4-d7
     
-    if(TXdata[0]==LINKCODE) lcd.cls(), lcd.printf("3SecondsLater,\n YouCanRun");
+    //if(TXdata[0]==LINKCODE) lcd.cls(), lcd.printf("3SecondsLater,\n YouCanRun");
     
     wait(3);
     
-    YLED=!Toggle;
-    if(YLED) lcd.cls(), lcd.printf("Pause Mode\n"), SW=0;
-    if(!YLED) lcd.cls(), lcd.printf("Run Mode\n"), SW=1;
+    //SW = Toggle;
     
+    Xbee.start_write();
     Xbee.write_data(EXdata, KEYCODE);
     
     while(1) {
         
-        YLED=!Toggle;
+        //YLED=!Toggle;
         
-        if(YLED&&SW) lcd.cls(), lcd.printf("Pause Mode\n"), SW=0;
-        if((!YLED)&&(!SW)) lcd.cls(), lcd.printf("Run Mode\n"), SW=1;
+        //if(YLED&&SW) lcd.cls(), lcd.printf("Pause Mode\n"), SW=0;
+        //if((!YLED)&&(!SW)) lcd.cls(), lcd.printf("Run Mode\n"), SW=1;
         
-        if(Toggle){
+        /*if(Toggle){
             
             EXdata[0] = LinkBit(!CrossKey[YELLOW][LEFT], !CrossKey[RED][LEFT], !CrossKey[GREEN][LEFT], !CrossKey[BLUE][LEFT], 
                                 !CrossKey[YELLOW][RIGHT], !CrossKey[RED][RIGHT], !CrossKey[GREEN][RIGHT], !CrossKey[BLUE][RIGHT]);
@@ -75,13 +79,13 @@
         }else{
             
             EXdata[0] = EXdata[1] = 0x00;
-        }
+        }*/
         
     }
     
 }
 
-void SetUp(void){
+/*void SetUp(void){
     
     CrossKey[NORTH][RIGHT].mode(PullUp);
     CrossKey[EAST][RIGHT].mode(PullUp);
@@ -99,20 +103,41 @@
     
     flick.attach(illumination, INTERVAL);
     flick.detach();
-}
+}*/
 void connect_check(void){
     
     while(1){
-        
-        //YLED = !Toggle;
-        //LED = 12;
+        /*
+        YLED = !Toggle;
+        LED = 12;
         Xbee.start_write();
         TXdata[0]=LINKCODE;
         Xbee.write_data(TXdata,KEYCODE);
-        wait_ms(100);
+        //wait_ms(100);
         Xbee.stop_write();
         
         Xbee.start_read();
+        
+        Xbee.read_data(RXdata,KEYCODE);
+        
+        LED = 3;
+        
+        //wait_ms(100);
+        if(RXdata[0]==LINKCODE) break;
+        while(INdata[0]!=LINKCODE) YLED = !Toggle;
+        
+        while(INdata[0]!=LINKCODE) YLED = !Toggle;
+        */
+        
+        /*YLED = !Toggle;
+        LED = 12;
+        Xbee.start_write();
+        TXdata[0]=LINKCODE;
+        Xbee.write_data(TXdata,KEYCODE);
+        //wait_ms(100);
+        Xbee.stop_write();
+        Xbee.start_read();
+        
         Xbee.read_data(RXdata,KEYCODE);
         LED = 3;
         wait_ms(100);
@@ -120,13 +145,40 @@
         
         Xbee.stop_read();
         
+        //continue;
+        while(INdata[0]!=LINKCODE) YLED = !Toggle;
+        while(INdata[0]!=LINKCODE) YLED = !Toggle;*/
+        
     }
     
 }
-uint8_t LinkBit(bool eight, bool seven, bool six, bool five, bool fore, bool three, bool two, bool one){//0と1を集めて整数を作る
+void GiveAndTake(){
+    if(GAT){
+        YLED = !Toggle;
+        LED = 12;
+        Xbee.start_write();
+        TXdata[0]=LINKCODE;
+        Xbee.write_data(TXdata,KEYCODE);
+        //wait_us(100);
+        Xbee.stop_write();
+    }
+    else{
+        YLED = !Toggle;
+        Xbee.start_read();
+        
+        Xbee.read_data(RXdata,KEYCODE);
+        LED = 3;
+        wait_ms(100);
+        if(RXdata[0]==LINKCODE) TAX.detach();
+        
+        Xbee.stop_read();
+    }
+    GAT = !GAT;
+}
+/*uint8_t LinkBit(bool eight, bool seven, bool six, bool five, bool fore, bool three, bool two, bool one){//0と1を集めて整数を作る
     return 0x80*eight + 0x40*seven + 0x20*six + 0x10*five + 0x08*fore + 0x04*three + 0x02*two + 0x01*one;
 }
 void illumination(void){//literally
     count++;
     LED = count;
-}
\ No newline at end of file
+}*/
\ No newline at end of file