gyo
SBDBT.cpp@6:da71294502ee, 2017-08-13 (annotated)
- Committer:
- gotto0
- Date:
- Sun Aug 13 03:35:38 2017 +0000
- Revision:
- 6:da71294502ee
- Parent:
- 5:a4a994824c96
- Child:
- 7:30e05998769b
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gotto0 | 0:afe58893bfe7 | 1 | #include "SBDBT.h" |
gotto0 | 0:afe58893bfe7 | 2 | |
gotto0 | 0:afe58893bfe7 | 3 | namespace raven |
gotto0 | 0:afe58893bfe7 | 4 | { |
gotto0 | 0:afe58893bfe7 | 5 | |
gotto0 | 0:afe58893bfe7 | 6 | SBDBT::SBDBT(PinName p1, PinName p2) |
gotto0 | 0:afe58893bfe7 | 7 | { |
gotto0 | 0:afe58893bfe7 | 8 | i = 0; |
gotto0 | 3:16a15d26ebe8 | 9 | readable = 0; |
gotto0 | 1:8810976be033 | 10 | length = 8; |
gotto0 | 1:8810976be033 | 11 | value = 128; |
gotto0 | 1:8810976be033 | 12 | position = 0; |
gotto0 | 2:1472e7c5317d | 13 | tmp = new char [length]; |
gotto0 | 1:8810976be033 | 14 | data = new char [length]; |
gotto0 | 0:afe58893bfe7 | 15 | sr = new Serial(p1,p2); |
gotto0 | 0:afe58893bfe7 | 16 | sr -> baud(2400); |
gotto0 | 1:8810976be033 | 17 | sr -> attach(this,&SBDBT::getf,Serial::RxIrq); |
gotto0 | 0:afe58893bfe7 | 18 | } |
gotto0 | 0:afe58893bfe7 | 19 | |
gotto0 | 1:8810976be033 | 20 | SBDBT::SBDBT(PinName p1, PinName p2, int baud) |
gotto0 | 1:8810976be033 | 21 | { |
gotto0 | 1:8810976be033 | 22 | i = 0; |
gotto0 | 2:1472e7c5317d | 23 | readable = 0; |
gotto0 | 1:8810976be033 | 24 | length = 8; |
gotto0 | 1:8810976be033 | 25 | value = 128; |
gotto0 | 1:8810976be033 | 26 | position = 0; |
gotto0 | 2:1472e7c5317d | 27 | tmp = new char [length]; |
gotto0 | 1:8810976be033 | 28 | data = new char [length]; |
gotto0 | 1:8810976be033 | 29 | sr = new Serial(p1,p2); |
gotto0 | 1:8810976be033 | 30 | sr -> baud(baud); |
gotto0 | 1:8810976be033 | 31 | sr -> attach(this,&SBDBT::getf,Serial::RxIrq); |
gotto0 | 1:8810976be033 | 32 | } |
gotto0 | 1:8810976be033 | 33 | |
gotto0 | 1:8810976be033 | 34 | void SBDBT::getf() |
gotto0 | 0:afe58893bfe7 | 35 | { |
gotto0 | 2:1472e7c5317d | 36 | tmp[i] = sr -> getc(); |
gotto0 | 2:1472e7c5317d | 37 | if(tmp[i] == value) //受信したデータが訂正用定数であり |
gotto0 | 0:afe58893bfe7 | 38 | { |
gotto0 | 1:8810976be033 | 39 | if(i != position) //訂正用定数を受信すべき位置でないとき |
gotto0 | 1:8810976be033 | 40 | { |
gotto0 | 2:1472e7c5317d | 41 | tmp[position] = value; //正しい位置に訂正用定数を代入 |
gotto0 | 1:8810976be033 | 42 | i = position + 1; //受信先も正しい位置へもどす |
gotto0 | 1:8810976be033 | 43 | } |
gotto0 | 1:8810976be033 | 44 | else //訂正用定数を受信すべき位置であるとき |
gotto0 | 1:8810976be033 | 45 | i++; //次の位置で受信する |
gotto0 | 1:8810976be033 | 46 | } |
gotto0 | 1:8810976be033 | 47 | else //受信したデータが訂正用定数でなく |
gotto0 | 1:8810976be033 | 48 | { |
gotto0 | 1:8810976be033 | 49 | if(i == position){} //訂正用定数を受信すべき位置であるとき |
gotto0 | 1:8810976be033 | 50 | //再度同じ位置で受信する |
gotto0 | 1:8810976be033 | 51 | else //訂正用定数を受信すべき場所でないとき |
gotto0 | 1:8810976be033 | 52 | i++; //次の位置で受信する |
gotto0 | 1:8810976be033 | 53 | } |
gotto0 | 1:8810976be033 | 54 | if(i>=length) //位置がデータの長さを超えたら |
gotto0 | 1:8810976be033 | 55 | { |
gotto0 | 2:1472e7c5317d | 56 | data[0] = tmp[0]; |
gotto0 | 2:1472e7c5317d | 57 | data[1] = tmp[1]; |
gotto0 | 2:1472e7c5317d | 58 | data[2] = tmp[2]; |
gotto0 | 2:1472e7c5317d | 59 | data[3] = tmp[3]; |
gotto0 | 2:1472e7c5317d | 60 | data[4] = tmp[4]; |
gotto0 | 2:1472e7c5317d | 61 | data[5] = tmp[5]; |
gotto0 | 2:1472e7c5317d | 62 | data[6] = tmp[6]; |
gotto0 | 2:1472e7c5317d | 63 | data[7] = tmp[7]; |
gotto0 | 2:1472e7c5317d | 64 | readable = 1; //全データ受信完了 |
gotto0 | 1:8810976be033 | 65 | i=0; //位置を初期化 |
gotto0 | 0:afe58893bfe7 | 66 | } |
gotto0 | 0:afe58893bfe7 | 67 | } |
gotto0 | 0:afe58893bfe7 | 68 | |
gotto0 | 6:da71294502ee | 69 | int SBDBT::sw() |
gotto0 | 6:da71294502ee | 70 | { |
gotto0 | 6:da71294502ee | 71 | return data[1]+data[2]<<8; |
gotto0 | 6:da71294502ee | 72 | } |
gotto0 | 6:da71294502ee | 73 | |
gotto0 | 6:da71294502ee | 74 | void SBDBT::print() |
gotto0 | 6:da71294502ee | 75 | { |
gotto0 | 6:da71294502ee | 76 | printf("%3d %3d %3d %3d %3d %3d %3d %3d\n",data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]); |
gotto0 | 6:da71294502ee | 77 | } |
gotto0 | 6:da71294502ee | 78 | |
gotto0 | 5:a4a994824c96 | 79 | char SBDBT::maru() |
gotto0 | 5:a4a994824c96 | 80 | { |
gotto0 | 5:a4a994824c96 | 81 | if(data[2] & 64) |
gotto0 | 5:a4a994824c96 | 82 | return 1; |
gotto0 | 5:a4a994824c96 | 83 | return 0; |
gotto0 | 5:a4a994824c96 | 84 | } |
gotto0 | 5:a4a994824c96 | 85 | |
gotto0 | 5:a4a994824c96 | 86 | char SBDBT::batu() |
gotto0 | 5:a4a994824c96 | 87 | { |
gotto0 | 5:a4a994824c96 | 88 | if(data[2] & 32) |
gotto0 | 5:a4a994824c96 | 89 | return 1; |
gotto0 | 5:a4a994824c96 | 90 | return 0; |
gotto0 | 5:a4a994824c96 | 91 | } |
gotto0 | 5:a4a994824c96 | 92 | |
gotto0 | 5:a4a994824c96 | 93 | char SBDBT::sikaku() |
gotto0 | 5:a4a994824c96 | 94 | { |
gotto0 | 5:a4a994824c96 | 95 | if(data[1] & 1) |
gotto0 | 5:a4a994824c96 | 96 | return 1; |
gotto0 | 5:a4a994824c96 | 97 | return 0; |
gotto0 | 5:a4a994824c96 | 98 | } |
gotto0 | 5:a4a994824c96 | 99 | |
gotto0 | 5:a4a994824c96 | 100 | char SBDBT::sankaku() |
gotto0 | 5:a4a994824c96 | 101 | { |
gotto0 | 5:a4a994824c96 | 102 | if(data[2] & 16) |
gotto0 | 5:a4a994824c96 | 103 | return 1; |
gotto0 | 5:a4a994824c96 | 104 | return 0; |
gotto0 | 5:a4a994824c96 | 105 | } |
gotto0 | 5:a4a994824c96 | 106 | |
gotto0 | 5:a4a994824c96 | 107 | char SBDBT::L1() |
gotto0 | 5:a4a994824c96 | 108 | { |
gotto0 | 5:a4a994824c96 | 109 | if(data[1] & 2) |
gotto0 | 5:a4a994824c96 | 110 | return 1; |
gotto0 | 5:a4a994824c96 | 111 | return 0; |
gotto0 | 5:a4a994824c96 | 112 | } |
gotto0 | 5:a4a994824c96 | 113 | |
gotto0 | 5:a4a994824c96 | 114 | char SBDBT::L2() |
gotto0 | 5:a4a994824c96 | 115 | { |
gotto0 | 5:a4a994824c96 | 116 | if(data[1] & 4) |
gotto0 | 5:a4a994824c96 | 117 | return 1; |
gotto0 | 5:a4a994824c96 | 118 | return 0; |
gotto0 | 5:a4a994824c96 | 119 | } |
gotto0 | 5:a4a994824c96 | 120 | |
gotto0 | 5:a4a994824c96 | 121 | char SBDBT::R1() |
gotto0 | 5:a4a994824c96 | 122 | { |
gotto0 | 5:a4a994824c96 | 123 | if(data[1] & 8) |
gotto0 | 5:a4a994824c96 | 124 | return 1; |
gotto0 | 5:a4a994824c96 | 125 | return 0; |
gotto0 | 5:a4a994824c96 | 126 | } |
gotto0 | 5:a4a994824c96 | 127 | |
gotto0 | 5:a4a994824c96 | 128 | char SBDBT::R2() |
gotto0 | 5:a4a994824c96 | 129 | { |
gotto0 | 5:a4a994824c96 | 130 | if(data[1] & 16) |
gotto0 | 5:a4a994824c96 | 131 | return 1; |
gotto0 | 5:a4a994824c96 | 132 | return 0; |
gotto0 | 5:a4a994824c96 | 133 | } |
gotto0 | 5:a4a994824c96 | 134 | |
gotto0 | 5:a4a994824c96 | 135 | char SBDBT::ue() |
gotto0 | 5:a4a994824c96 | 136 | { |
gotto0 | 5:a4a994824c96 | 137 | if(data[2] & 1) |
gotto0 | 5:a4a994824c96 | 138 | return 1; |
gotto0 | 5:a4a994824c96 | 139 | return 0; |
gotto0 | 5:a4a994824c96 | 140 | } |
gotto0 | 5:a4a994824c96 | 141 | |
gotto0 | 5:a4a994824c96 | 142 | char SBDBT::sita() |
gotto0 | 5:a4a994824c96 | 143 | { |
gotto0 | 5:a4a994824c96 | 144 | if(data[2] & 2) |
gotto0 | 5:a4a994824c96 | 145 | return 1; |
gotto0 | 5:a4a994824c96 | 146 | return 0; |
gotto0 | 5:a4a994824c96 | 147 | } |
gotto0 | 5:a4a994824c96 | 148 | |
gotto0 | 5:a4a994824c96 | 149 | char SBDBT::migi() |
gotto0 | 5:a4a994824c96 | 150 | { |
gotto0 | 5:a4a994824c96 | 151 | if(data[2] & 4) |
gotto0 | 5:a4a994824c96 | 152 | return 1; |
gotto0 | 5:a4a994824c96 | 153 | return 0; |
gotto0 | 5:a4a994824c96 | 154 | } |
gotto0 | 5:a4a994824c96 | 155 | |
gotto0 | 5:a4a994824c96 | 156 | char SBDBT::hidari() |
gotto0 | 5:a4a994824c96 | 157 | { |
gotto0 | 5:a4a994824c96 | 158 | if(data[2] & 8) |
gotto0 | 5:a4a994824c96 | 159 | return 1; |
gotto0 | 5:a4a994824c96 | 160 | return 0; |
gotto0 | 5:a4a994824c96 | 161 | } |
gotto0 | 5:a4a994824c96 | 162 | |
gotto0 | 6:da71294502ee | 163 | signed char SBDBT::rs_x() |
gotto0 | 6:da71294502ee | 164 | { |
gotto0 | 6:da71294502ee | 165 | return data[5]-64; |
gotto0 | 6:da71294502ee | 166 | } |
gotto0 | 6:da71294502ee | 167 | |
gotto0 | 6:da71294502ee | 168 | signed char SBDBT::rs_y() |
gotto0 | 5:a4a994824c96 | 169 | { |
gotto0 | 6:da71294502ee | 170 | return data[6]-64; |
gotto0 | 6:da71294502ee | 171 | } |
gotto0 | 6:da71294502ee | 172 | |
gotto0 | 6:da71294502ee | 173 | signed char SBDBT::ls_x() |
gotto0 | 6:da71294502ee | 174 | { |
gotto0 | 6:da71294502ee | 175 | return data[3]-64; |
gotto0 | 5:a4a994824c96 | 176 | } |
gotto0 | 5:a4a994824c96 | 177 | |
gotto0 | 6:da71294502ee | 178 | signed char SBDBT::ls_y() |
gotto0 | 5:a4a994824c96 | 179 | { |
gotto0 | 6:da71294502ee | 180 | return data[4]-64; |
gotto0 | 6:da71294502ee | 181 | } |
gotto0 | 6:da71294502ee | 182 | |
gotto0 | 6:da71294502ee | 183 | char SBDBT::rs_x_check() |
gotto0 | 6:da71294502ee | 184 | { |
gotto0 | 6:da71294502ee | 185 | return data[5]==data[5] ? 0 : 1; |
gotto0 | 5:a4a994824c96 | 186 | } |
gotto0 | 5:a4a994824c96 | 187 | |
gotto0 | 6:da71294502ee | 188 | char SBDBT::rs_y_check() |
gotto0 | 5:a4a994824c96 | 189 | { |
gotto0 | 6:da71294502ee | 190 | return data[6]==data[6] ? 0 : 1; |
gotto0 | 5:a4a994824c96 | 191 | } |
gotto0 | 5:a4a994824c96 | 192 | |
gotto0 | 6:da71294502ee | 193 | char SBDBT::ls_x_check() |
gotto0 | 5:a4a994824c96 | 194 | { |
gotto0 | 6:da71294502ee | 195 | return data[3]==data[3] ? 0 : 1; |
gotto0 | 6:da71294502ee | 196 | } |
gotto0 | 6:da71294502ee | 197 | |
gotto0 | 6:da71294502ee | 198 | char SBDBT::ls_y_check() |
gotto0 | 6:da71294502ee | 199 | { |
gotto0 | 6:da71294502ee | 200 | return data[4]==data[4] ? 0 : 1; |
gotto0 | 5:a4a994824c96 | 201 | } |
gotto0 | 5:a4a994824c96 | 202 | |
gotto0 | 0:afe58893bfe7 | 203 | SBDBT::~SBDBT() |
gotto0 | 0:afe58893bfe7 | 204 | { |
gotto0 | 0:afe58893bfe7 | 205 | delete sr; |
gotto0 | 6:da71294502ee | 206 | delete tmp; |
gotto0 | 6:da71294502ee | 207 | delete data; |
gotto0 | 0:afe58893bfe7 | 208 | } |
gotto0 | 0:afe58893bfe7 | 209 | }//namespace |