gyo
SBDBT.cpp@15:8c49a263e344, 2019-05-11 (annotated)
- Committer:
- nanjo
- Date:
- Sat May 11 07:50:16 2019 +0000
- Revision:
- 15:8c49a263e344
- Parent:
- 11:026d2242ef47
- Child:
- 16:976121fe5c9d
add start select key
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nanjo | 11:026d2242ef47 | 1 | #include "SBDBT.h" |
nanjo | 11:026d2242ef47 | 2 | //#include "YKNCT.h" |
nanjo | 9:8538fc8f6259 | 3 | SBDBT::SBDBT(PinName p1, PinName p2) |
gotto0 | 0:afe58893bfe7 | 4 | { |
gotto0 | 0:afe58893bfe7 | 5 | i = 0; |
gotto0 | 3:16a15d26ebe8 | 6 | readable = 0; |
gotto0 | 1:8810976be033 | 7 | length = 8; |
gotto0 | 1:8810976be033 | 8 | value = 128; |
gotto0 | 1:8810976be033 | 9 | position = 0; |
gotto0 | 2:1472e7c5317d | 10 | tmp = new char [length]; |
gotto0 | 1:8810976be033 | 11 | data = new char [length]; |
gotto0 | 0:afe58893bfe7 | 12 | sr = new Serial(p1,p2); |
gotto0 | 0:afe58893bfe7 | 13 | sr -> baud(2400); |
gotto0 | 1:8810976be033 | 14 | sr -> attach(this,&SBDBT::getf,Serial::RxIrq); |
gotto0 | 0:afe58893bfe7 | 15 | } |
gotto0 | 0:afe58893bfe7 | 16 | |
gotto0 | 1:8810976be033 | 17 | SBDBT::SBDBT(PinName p1, PinName p2, int baud) |
gotto0 | 1:8810976be033 | 18 | { |
gotto0 | 1:8810976be033 | 19 | i = 0; |
gotto0 | 2:1472e7c5317d | 20 | readable = 0; |
gotto0 | 1:8810976be033 | 21 | length = 8; |
gotto0 | 1:8810976be033 | 22 | value = 128; |
gotto0 | 1:8810976be033 | 23 | position = 0; |
gotto0 | 2:1472e7c5317d | 24 | tmp = new char [length]; |
gotto0 | 1:8810976be033 | 25 | data = new char [length]; |
gotto0 | 1:8810976be033 | 26 | sr = new Serial(p1,p2); |
gotto0 | 1:8810976be033 | 27 | sr -> baud(baud); |
gotto0 | 1:8810976be033 | 28 | sr -> attach(this,&SBDBT::getf,Serial::RxIrq); |
gotto0 | 1:8810976be033 | 29 | } |
gotto0 | 1:8810976be033 | 30 | |
gotto0 | 1:8810976be033 | 31 | void SBDBT::getf() |
gotto0 | 0:afe58893bfe7 | 32 | { |
gotto0 | 2:1472e7c5317d | 33 | tmp[i] = sr -> getc(); |
gotto0 | 2:1472e7c5317d | 34 | if(tmp[i] == value) //受信したデータが訂正用定数であり |
gotto0 | 0:afe58893bfe7 | 35 | { |
gotto0 | 1:8810976be033 | 36 | if(i != position) //訂正用定数を受信すべき位置でないとき |
gotto0 | 1:8810976be033 | 37 | { |
gotto0 | 2:1472e7c5317d | 38 | tmp[position] = value; //正しい位置に訂正用定数を代入 |
gotto0 | 1:8810976be033 | 39 | i = position + 1; //受信先も正しい位置へもどす |
gotto0 | 1:8810976be033 | 40 | } |
gotto0 | 1:8810976be033 | 41 | else //訂正用定数を受信すべき位置であるとき |
gotto0 | 1:8810976be033 | 42 | i++; //次の位置で受信する |
gotto0 | 1:8810976be033 | 43 | } |
gotto0 | 1:8810976be033 | 44 | else //受信したデータが訂正用定数でなく |
gotto0 | 1:8810976be033 | 45 | { |
gotto0 | 1:8810976be033 | 46 | if(i == position){} //訂正用定数を受信すべき位置であるとき |
gotto0 | 1:8810976be033 | 47 | //再度同じ位置で受信する |
gotto0 | 1:8810976be033 | 48 | else //訂正用定数を受信すべき場所でないとき |
gotto0 | 1:8810976be033 | 49 | i++; //次の位置で受信する |
gotto0 | 1:8810976be033 | 50 | } |
gotto0 | 1:8810976be033 | 51 | if(i>=length) //位置がデータの長さを超えたら |
gotto0 | 1:8810976be033 | 52 | { |
gotto0 | 2:1472e7c5317d | 53 | data[0] = tmp[0]; |
gotto0 | 2:1472e7c5317d | 54 | data[1] = tmp[1]; |
gotto0 | 2:1472e7c5317d | 55 | data[2] = tmp[2]; |
gotto0 | 2:1472e7c5317d | 56 | data[3] = tmp[3]; |
gotto0 | 2:1472e7c5317d | 57 | data[4] = tmp[4]; |
gotto0 | 2:1472e7c5317d | 58 | data[5] = tmp[5]; |
gotto0 | 2:1472e7c5317d | 59 | data[6] = tmp[6]; |
gotto0 | 2:1472e7c5317d | 60 | data[7] = tmp[7]; |
gotto0 | 2:1472e7c5317d | 61 | readable = 1; //全データ受信完了 |
nanjo | 9:8538fc8f6259 | 62 | check(); |
gotto0 | 1:8810976be033 | 63 | i=0; //位置を初期化 |
gotto0 | 0:afe58893bfe7 | 64 | } |
gotto0 | 0:afe58893bfe7 | 65 | } |
gotto0 | 0:afe58893bfe7 | 66 | |
gotto0 | 6:da71294502ee | 67 | void SBDBT::print() |
gotto0 | 6:da71294502ee | 68 | { |
nanjo | 9:8538fc8f6259 | 69 | printf("%3d %3d %3d %3d %3d %3d %3d %3d\r\n",data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]); |
gotto0 | 5:a4a994824c96 | 70 | } |
gotto0 | 5:a4a994824c96 | 71 | |
nanjo | 9:8538fc8f6259 | 72 | int8_t SBDBT::State_check() |
gotto0 | 5:a4a994824c96 | 73 | { |
nanjo | 9:8538fc8f6259 | 74 | if(readable == 1)return 1; |
nanjo | 9:8538fc8f6259 | 75 | else return 0; |
gotto0 | 5:a4a994824c96 | 76 | } |
gotto0 | 5:a4a994824c96 | 77 | |
nanjo | 9:8538fc8f6259 | 78 | void SBDBT::check() |
nanjo | 9:8538fc8f6259 | 79 | { |
nanjo | 9:8538fc8f6259 | 80 | SQUARE=((data[1] & 1) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 81 | L1 = ((data[1] & 2) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 82 | L2 = ((data[1] & 4) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 83 | L3 = ((data[1] & 8) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 84 | R1 = ((data[1] & 16)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 85 | R2 = ((data[1] & 32)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 86 | R3 = ((data[1] & 64)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 87 | |
nanjo | 9:8538fc8f6259 | 88 | UPkey = ((data[2] & 1) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 89 | DOWNkey = ((data[2] & 2) ? 1 : 0); |
nanjo | 15:8c49a263e344 | 90 | START = ((data[2] & 3) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 91 | RIGHTkey = ((data[2] & 4) ? 1 : 0); |
nanjo | 9:8538fc8f6259 | 92 | LEFTkey = ((data[2] & 8) ? 1 : 0); |
nanjo | 15:8c49a263e344 | 93 | SELECT = ((data[2] & 12)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 94 | TRIANGLE = ((data[2] & 16)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 95 | CROSS = ((data[2] & 32)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 96 | CIRCLE = ((data[2] & 64)? 1 : 0); |
nanjo | 9:8538fc8f6259 | 97 | |
nanjo | 9:8538fc8f6259 | 98 | LX = data[3]; |
nanjo | 9:8538fc8f6259 | 99 | LY = data[4]; |
nanjo | 9:8538fc8f6259 | 100 | RX = data[5]; |
nanjo | 9:8538fc8f6259 | 101 | RY = data[6]; |
gotto0 | 5:a4a994824c96 | 102 | } |
gotto0 | 5:a4a994824c96 | 103 | |
gotto0 | 6:da71294502ee | 104 | |
gotto0 | 6:da71294502ee | 105 | char SBDBT::rs_x_check() |
gotto0 | 6:da71294502ee | 106 | { |
gotto0 | 6:da71294502ee | 107 | return data[5]==data[5] ? 0 : 1; |
gotto0 | 5:a4a994824c96 | 108 | } |
gotto0 | 5:a4a994824c96 | 109 | |
gotto0 | 6:da71294502ee | 110 | char SBDBT::rs_y_check() |
gotto0 | 5:a4a994824c96 | 111 | { |
gotto0 | 6:da71294502ee | 112 | return data[6]==data[6] ? 0 : 1; |
gotto0 | 5:a4a994824c96 | 113 | } |
gotto0 | 5:a4a994824c96 | 114 | |
gotto0 | 6:da71294502ee | 115 | char SBDBT::ls_x_check() |
gotto0 | 5:a4a994824c96 | 116 | { |
gotto0 | 6:da71294502ee | 117 | return data[3]==data[3] ? 0 : 1; |
gotto0 | 6:da71294502ee | 118 | } |
gotto0 | 6:da71294502ee | 119 | |
gotto0 | 6:da71294502ee | 120 | char SBDBT::ls_y_check() |
gotto0 | 6:da71294502ee | 121 | { |
gotto0 | 6:da71294502ee | 122 | return data[4]==data[4] ? 0 : 1; |
gotto0 | 5:a4a994824c96 | 123 | } |
gotto0 | 5:a4a994824c96 | 124 | |
gotto0 | 0:afe58893bfe7 | 125 | SBDBT::~SBDBT() |
gotto0 | 0:afe58893bfe7 | 126 | { |
gotto0 | 0:afe58893bfe7 | 127 | delete sr; |
gotto0 | 6:da71294502ee | 128 | delete tmp; |
gotto0 | 6:da71294502ee | 129 | delete data; |
gotto0 | 0:afe58893bfe7 | 130 | } |
nanjo | 9:8538fc8f6259 | 131 |