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

Dependencies:   TextLCD mbed MultiSerial Pswitch_Lib

Committer:
lilac0112_1
Date:
Thu Oct 16 11:00:30 2014 +0000
Revision:
10:8b49bbf4c0c9
Parent:
9:39d3392a9587
Add new a bit data.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lilac0112_1 0:d3702a4e32ea 1 #include "mbed.h"
lilac0112_1 0:d3702a4e32ea 2 #include "TextLCD.h"
lilac0112_1 1:84625a611c5f 3 #include "MultiSerial.h"
lilac0112_1 9:39d3392a9587 4 #include "Pswitch.h"
lilac0112_1 9:39d3392a9587 5
lilac0112_1 2:7820849bf534 6 #define DATA_NUM 2
lilac0112_1 2:7820849bf534 7 #define KEYCODE 0xAA
lilac0112_1 9:39d3392a9587 8 #define INTERVAL 12
lilac0112_1 0:d3702a4e32ea 9
lilac0112_1 0:d3702a4e32ea 10 enum HAND{RIGHT=0, LEFT};
lilac0112_1 0:d3702a4e32ea 11 enum FINGER{INDEX=0, MIDDLE};
lilac0112_1 0:d3702a4e32ea 12 enum COMPASS{NORTH=0, EAST, SOUTH, WEST};
lilac0112_1 7:8c15316bdba6 13 enum COLOR{YELLOW=0, RED, GREEN, BLUE};
lilac0112_1 0:d3702a4e32ea 14
lilac0112_1 10:8b49bbf4c0c9 15
lilac0112_1 10:8b49bbf4c0c9 16 #define INITIAL_TIME 180
lilac0112_1 10:8b49bbf4c0c9 17
lilac0112_1 10:8b49bbf4c0c9 18 LocalFileSystem local("local");
lilac0112_1 10:8b49bbf4c0c9 19
lilac0112_1 10:8b49bbf4c0c9 20 Ticker Watch;
lilac0112_1 10:8b49bbf4c0c9 21
lilac0112_1 10:8b49bbf4c0c9 22 uint8_t GivenTime=INITIAL_TIME;
lilac0112_1 10:8b49bbf4c0c9 23
lilac0112_1 10:8b49bbf4c0c9 24 char *FileName = "/local/GIVEN180.txt";
lilac0112_1 10:8b49bbf4c0c9 25 char *format = "%d\n";
lilac0112_1 10:8b49bbf4c0c9 26 FILE *fp;
lilac0112_1 10:8b49bbf4c0c9 27
lilac0112_1 10:8b49bbf4c0c9 28 void TickTack(void);
lilac0112_1 10:8b49bbf4c0c9 29
lilac0112_1 10:8b49bbf4c0c9 30
lilac0112_1 10:8b49bbf4c0c9 31
lilac0112_1 10:8b49bbf4c0c9 32
lilac0112_1 10:8b49bbf4c0c9 33
lilac0112_1 9:39d3392a9587 34 uint8_t INdata[DATA_NUM]={0}, EXdata[DATA_NUM]={0};
lilac0112_1 9:39d3392a9587 35 uint8_t count=0;
lilac0112_1 7:8c15316bdba6 36 bool SW;
lilac0112_1 0:d3702a4e32ea 37
lilac0112_1 7:8c15316bdba6 38 void SetUp();
lilac0112_1 7:8c15316bdba6 39 uint8_t LinkBit(bool eight, bool seven, bool six, bool five, bool fore, bool three, bool two, bool one);
lilac0112_1 7:8c15316bdba6 40 void illumination();
lilac0112_1 0:d3702a4e32ea 41
lilac0112_1 7:8c15316bdba6 42 Ticker flick;
lilac0112_1 7:8c15316bdba6 43 BusOut LED(LED1, LED2, LED3, LED4);
lilac0112_1 7:8c15316bdba6 44
lilac0112_1 0:d3702a4e32ea 45 DigitalOut YLED(p6);
lilac0112_1 0:d3702a4e32ea 46
lilac0112_1 7:8c15316bdba6 47 DigitalIn CrossKey[4][2]={{p8, p30}, {p11, p29}, {p15, p28}, {p16, p27}};//p7, p12=reserve
lilac0112_1 7:8c15316bdba6 48 DigitalIn SideKey[2][2]={{p17, p20}, {p18, p19}};
lilac0112_1 5:a5c324942570 49 DigitalIn Toggle(p5);
lilac0112_1 9:39d3392a9587 50 //Pswitch Toggle(p5);
lilac0112_1 5:a5c324942570 51
lilac0112_1 9:39d3392a9587 52 MultiSerial Xbee(p9, p10, write);//or p13, p14
lilac0112_1 9:39d3392a9587 53 Serial pc(USBTX, USBRX);
lilac0112_1 4:39edc16ee5c3 54
lilac0112_1 9:39d3392a9587 55 TextLCD lcd(p26, p25, p24, p23, p22, p21, TextLCD::LCD16x2); // rs, e, d4-d7
lilac0112_1 9:39d3392a9587 56
lilac0112_1 9:39d3392a9587 57 int main() {
lilac0112_1 10:8b49bbf4c0c9 58 /*fp = fopen(FileName, "r");
lilac0112_1 10:8b49bbf4c0c9 59 if (fp) {
lilac0112_1 10:8b49bbf4c0c9 60 fscanf(fp, format, &GivenTime);
lilac0112_1 10:8b49bbf4c0c9 61 fclose(fp);
lilac0112_1 10:8b49bbf4c0c9 62 }
lilac0112_1 10:8b49bbf4c0c9 63 fp = fopen(FileName, "w");
lilac0112_1 10:8b49bbf4c0c9 64 if (fp) {
lilac0112_1 10:8b49bbf4c0c9 65 fprintf(fp, format, GivenTime);
lilac0112_1 10:8b49bbf4c0c9 66 fclose(fp);
lilac0112_1 10:8b49bbf4c0c9 67 }
lilac0112_1 10:8b49bbf4c0c9 68
lilac0112_1 10:8b49bbf4c0c9 69 if((GivenTime == 0)||(GivenTime > 180)) GivenTime = INITIAL_TIME;
lilac0112_1 10:8b49bbf4c0c9 70 */
lilac0112_1 10:8b49bbf4c0c9 71 Watch.attach(TickTack, 1);
lilac0112_1 10:8b49bbf4c0c9 72
lilac0112_1 10:8b49bbf4c0c9 73
lilac0112_1 10:8b49bbf4c0c9 74
lilac0112_1 5:a5c324942570 75
lilac0112_1 7:8c15316bdba6 76 SetUp();
lilac0112_1 7:8c15316bdba6 77
lilac0112_1 5:a5c324942570 78 Xbee.write_data(EXdata, KEYCODE);
lilac0112_1 0:d3702a4e32ea 79 while(1) {
lilac0112_1 0:d3702a4e32ea 80
lilac0112_1 7:8c15316bdba6 81 YLED=!Toggle;
lilac0112_1 9:39d3392a9587 82 if(Toggle){
lilac0112_1 9:39d3392a9587 83 YLED=!Toggle.read();
lilac0112_1 9:39d3392a9587 84 }
lilac0112_1 4:39edc16ee5c3 85
lilac0112_1 10:8b49bbf4c0c9 86 if(YLED&&SW) lcd.locate(0, 0), lcd.printf("Setting Mode\n"), SW=0, Watch.detach();
lilac0112_1 10:8b49bbf4c0c9 87 if((!YLED)&&(!SW)) lcd.locate(0, 0), lcd.printf("Run Mode \n"), SW=1, Watch.attach(TickTack, 1);
lilac0112_1 4:39edc16ee5c3 88
lilac0112_1 9:39d3392a9587 89 if(Toggle.read()){
lilac0112_1 3:cfd7cd361088 90
lilac0112_1 10:8b49bbf4c0c9 91
lilac0112_1 10:8b49bbf4c0c9 92
lilac0112_1 3:cfd7cd361088 93 EXdata[0] = LinkBit(!CrossKey[YELLOW][LEFT], !CrossKey[RED][LEFT], !CrossKey[GREEN][LEFT], !CrossKey[BLUE][LEFT],
lilac0112_1 3:cfd7cd361088 94 !CrossKey[YELLOW][RIGHT], !CrossKey[RED][RIGHT], !CrossKey[GREEN][RIGHT], !CrossKey[BLUE][RIGHT]);
lilac0112_1 10:8b49bbf4c0c9 95 EXdata[1] = LinkBit(0, 0, 0, 1, !SideKey[INDEX][LEFT], !SideKey[MIDDLE][LEFT], !SideKey[INDEX][RIGHT], !SideKey[MIDDLE][RIGHT]);
lilac0112_1 3:cfd7cd361088 96 }else{
lilac0112_1 3:cfd7cd361088 97
lilac0112_1 10:8b49bbf4c0c9 98 EXdata[0] = 0x00;
lilac0112_1 10:8b49bbf4c0c9 99 EXdata[1] = 0x00;
lilac0112_1 10:8b49bbf4c0c9 100
lilac0112_1 10:8b49bbf4c0c9 101 /*if((GivenTime!=INITIAL_TIME)&&(LinkBit(0, 0, 0, 0, !SideKey[INDEX][LEFT], !SideKey[MIDDLE][LEFT], !SideKey[INDEX][RIGHT], !SideKey[MIDDLE][RIGHT])==0xF)){
lilac0112_1 10:8b49bbf4c0c9 102 GivenTime = INITIAL_TIME;
lilac0112_1 10:8b49bbf4c0c9 103 lcd.locate(0, 1), lcd.printf("%d:%02d \n", GivenTime/60, GivenTime%60);
lilac0112_1 10:8b49bbf4c0c9 104 }*/
lilac0112_1 7:8c15316bdba6 105 }
lilac0112_1 0:d3702a4e32ea 106
lilac0112_1 9:39d3392a9587 107 LED = Toggle*LinkBit(0, 0, 0, 0, !SideKey[INDEX][LEFT], !SideKey[MIDDLE][LEFT], !SideKey[INDEX][RIGHT], !SideKey[MIDDLE][RIGHT]);
lilac0112_1 9:39d3392a9587 108
lilac0112_1 0:d3702a4e32ea 109 }
lilac0112_1 0:d3702a4e32ea 110 }
lilac0112_1 0:d3702a4e32ea 111
lilac0112_1 7:8c15316bdba6 112 void SetUp(void){
lilac0112_1 0:d3702a4e32ea 113
lilac0112_1 10:8b49bbf4c0c9 114 //SW = !Toggle;
lilac0112_1 9:39d3392a9587 115
lilac0112_1 10:8b49bbf4c0c9 116 if(!Toggle) lcd.locate(0, 0), lcd.printf("Setting Mode\n"), SW=0;
lilac0112_1 10:8b49bbf4c0c9 117 else lcd.locate(0, 0), lcd.printf("Run Mode \n"), SW=1;
lilac0112_1 10:8b49bbf4c0c9 118
lilac0112_1 10:8b49bbf4c0c9 119 lcd.locate(0, 1), lcd.printf("%d:%02d \n", GivenTime/60, GivenTime%60);
lilac0112_1 9:39d3392a9587 120
lilac0112_1 0:d3702a4e32ea 121 CrossKey[NORTH][RIGHT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 122 CrossKey[EAST][RIGHT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 123 CrossKey[WEST][RIGHT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 124 CrossKey[SOUTH][RIGHT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 125 CrossKey[NORTH][LEFT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 126 CrossKey[EAST][LEFT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 127 CrossKey[WEST][LEFT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 128 CrossKey[SOUTH][LEFT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 129
lilac0112_1 0:d3702a4e32ea 130 SideKey[INDEX][RIGHT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 131 SideKey[MIDDLE][RIGHT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 132 SideKey[INDEX][LEFT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 133 SideKey[MIDDLE][LEFT].mode(PullUp);
lilac0112_1 0:d3702a4e32ea 134
lilac0112_1 9:39d3392a9587 135 Toggle.mode(PullUp);
lilac0112_1 7:8c15316bdba6 136
lilac0112_1 9:39d3392a9587 137 //flick.attach(illumination, INTERVAL);
lilac0112_1 6:08b5c5bdace9 138 }
lilac0112_1 7:8c15316bdba6 139 uint8_t LinkBit(bool eight, bool seven, bool six, bool five, bool fore, bool three, bool two, bool one){//0と1を集めて整数を作る
lilac0112_1 0:d3702a4e32ea 140 return 0x80*eight + 0x40*seven + 0x20*six + 0x10*five + 0x08*fore + 0x04*three + 0x02*two + 0x01*one;
lilac0112_1 0:d3702a4e32ea 141 }
lilac0112_1 0:d3702a4e32ea 142 void illumination(void){//literally
lilac0112_1 0:d3702a4e32ea 143 count++;
lilac0112_1 0:d3702a4e32ea 144 LED = count;
lilac0112_1 10:8b49bbf4c0c9 145 }
lilac0112_1 10:8b49bbf4c0c9 146
lilac0112_1 10:8b49bbf4c0c9 147
lilac0112_1 10:8b49bbf4c0c9 148
lilac0112_1 10:8b49bbf4c0c9 149 void TickTack(void){
lilac0112_1 10:8b49bbf4c0c9 150 if(!Toggle) return;
lilac0112_1 10:8b49bbf4c0c9 151 lcd.locate(0, 1);
lilac0112_1 10:8b49bbf4c0c9 152
lilac0112_1 10:8b49bbf4c0c9 153 if(GivenTime==0){
lilac0112_1 10:8b49bbf4c0c9 154 lcd.printf("GameSet! \n");
lilac0112_1 10:8b49bbf4c0c9 155 Watch.detach();
lilac0112_1 10:8b49bbf4c0c9 156 }
lilac0112_1 10:8b49bbf4c0c9 157 else{
lilac0112_1 10:8b49bbf4c0c9 158 GivenTime--;
lilac0112_1 10:8b49bbf4c0c9 159 /*fp = fopen(FileName, "w");
lilac0112_1 10:8b49bbf4c0c9 160 fprintf(fp, format, GivenTime);
lilac0112_1 10:8b49bbf4c0c9 161 fclose(fp);*/
lilac0112_1 10:8b49bbf4c0c9 162 lcd.printf("%d:%02d \n", GivenTime/60, GivenTime%60);
lilac0112_1 10:8b49bbf4c0c9 163 }
lilac0112_1 7:8c15316bdba6 164 }