gyo
SBDBT.cpp@1:8810976be033, 2017-05-06 (annotated)
- Committer:
- gotto0
- Date:
- Sat May 06 02:32:51 2017 +0000
- Revision:
- 1:8810976be033
- Parent:
- 0:afe58893bfe7
- Child:
- 2:1472e7c5317d
reworked
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 | 0:afe58893bfe7 | 9 | flag = 0; |
gotto0 | 1:8810976be033 | 10 | length = 8; |
gotto0 | 1:8810976be033 | 11 | value = 128; |
gotto0 | 1:8810976be033 | 12 | position = 0; |
gotto0 | 1:8810976be033 | 13 | data = new char [length]; |
gotto0 | 1:8810976be033 | 14 | olddata = 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 | 1:8810976be033 | 23 | flag = 0; |
gotto0 | 1:8810976be033 | 24 | length = 8; |
gotto0 | 1:8810976be033 | 25 | value = 128; |
gotto0 | 1:8810976be033 | 26 | position = 0; |
gotto0 | 1:8810976be033 | 27 | data = new char [length]; |
gotto0 | 1:8810976be033 | 28 | olddata = 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 | 0:afe58893bfe7 | 36 | olddata[i] = data[i]; |
gotto0 | 0:afe58893bfe7 | 37 | data[i] = sr -> getc(); |
gotto0 | 1:8810976be033 | 38 | if(data[i] == value) //受信したデータが訂正用定数であり |
gotto0 | 0:afe58893bfe7 | 39 | { |
gotto0 | 1:8810976be033 | 40 | if(i != position) //訂正用定数を受信すべき位置でないとき |
gotto0 | 1:8810976be033 | 41 | { |
gotto0 | 1:8810976be033 | 42 | data[position] = value; //正しい位置に訂正用定数を代入 |
gotto0 | 1:8810976be033 | 43 | i = position + 1; //受信先も正しい位置へもどす |
gotto0 | 1:8810976be033 | 44 | } |
gotto0 | 1:8810976be033 | 45 | else //訂正用定数を受信すべき位置であるとき |
gotto0 | 1:8810976be033 | 46 | i++; //次の位置で受信する |
gotto0 | 1:8810976be033 | 47 | } |
gotto0 | 1:8810976be033 | 48 | else //受信したデータが訂正用定数でなく |
gotto0 | 1:8810976be033 | 49 | { |
gotto0 | 1:8810976be033 | 50 | if(i == position){} //訂正用定数を受信すべき位置であるとき |
gotto0 | 1:8810976be033 | 51 | //再度同じ位置で受信する |
gotto0 | 1:8810976be033 | 52 | else //訂正用定数を受信すべき場所でないとき |
gotto0 | 1:8810976be033 | 53 | i++; //次の位置で受信する |
gotto0 | 1:8810976be033 | 54 | } |
gotto0 | 1:8810976be033 | 55 | if(i>=length) //位置がデータの長さを超えたら |
gotto0 | 1:8810976be033 | 56 | { |
gotto0 | 1:8810976be033 | 57 | i=0; //位置を初期化 |
gotto0 | 1:8810976be033 | 58 | flag = 1; //全データ受信完了 |
gotto0 | 0:afe58893bfe7 | 59 | } |
gotto0 | 0:afe58893bfe7 | 60 | } |
gotto0 | 0:afe58893bfe7 | 61 | |
gotto0 | 0:afe58893bfe7 | 62 | SBDBT::~SBDBT() |
gotto0 | 0:afe58893bfe7 | 63 | { |
gotto0 | 0:afe58893bfe7 | 64 | delete sr; |
gotto0 | 0:afe58893bfe7 | 65 | } |
gotto0 | 0:afe58893bfe7 | 66 | }//namespace |