Sille Van Landschoot
/
racing_robots
Racing Robots Session
Fork of racing_robots by
xbee.cpp@10:c67825bf5f6b, 2015-06-03 (annotated)
- Committer:
- sillevl
- Date:
- Wed Jun 03 11:28:28 2015 +0000
- Revision:
- 10:c67825bf5f6b
- Parent:
- 8:597ce8a7d34b
initial project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sillevl | 8:597ce8a7d34b | 1 | #include "xbee.h" |
sillevl | 8:597ce8a7d34b | 2 | #include "MbedJSONValue.h" |
sillevl | 8:597ce8a7d34b | 3 | |
sillevl | 8:597ce8a7d34b | 4 | Xbee::Xbee(PinName tx, PinName rx){ |
sillevl | 8:597ce8a7d34b | 5 | xbee = new Serial(tx, rx); |
sillevl | 8:597ce8a7d34b | 6 | xbee->baud(115200); |
sillevl | 8:597ce8a7d34b | 7 | xbee->attach(this, &Xbee::received, Serial::RxIrq); |
sillevl | 8:597ce8a7d34b | 8 | run = false; |
sillevl | 8:597ce8a7d34b | 9 | printf("xbee constructor\r\n"); |
sillevl | 8:597ce8a7d34b | 10 | rst = new DigitalOut(p26); |
sillevl | 8:597ce8a7d34b | 11 | reset(); |
sillevl | 8:597ce8a7d34b | 12 | setCode(-1); |
sillevl | 8:597ce8a7d34b | 13 | } |
sillevl | 8:597ce8a7d34b | 14 | |
sillevl | 8:597ce8a7d34b | 15 | void Xbee::reset(){ |
sillevl | 8:597ce8a7d34b | 16 | rst->write(0); |
sillevl | 8:597ce8a7d34b | 17 | wait_ms(1); |
sillevl | 8:597ce8a7d34b | 18 | rst->write(1); |
sillevl | 8:597ce8a7d34b | 19 | wait_ms(1); |
sillevl | 8:597ce8a7d34b | 20 | } |
sillevl | 8:597ce8a7d34b | 21 | |
sillevl | 8:597ce8a7d34b | 22 | void Xbee::setCode(int code){ |
sillevl | 10:c67825bf5f6b | 23 | if( code < 0 && code >= 1000){ |
sillevl | 8:597ce8a7d34b | 24 | error("Code must be between 0 and 999."); |
sillevl | 8:597ce8a7d34b | 25 | } |
sillevl | 8:597ce8a7d34b | 26 | this->code = code; |
sillevl | 8:597ce8a7d34b | 27 | } |
sillevl | 8:597ce8a7d34b | 28 | |
sillevl | 8:597ce8a7d34b | 29 | int Xbee::hasCode(){ |
sillevl | 8:597ce8a7d34b | 30 | return code != -1; |
sillevl | 8:597ce8a7d34b | 31 | } |
sillevl | 8:597ce8a7d34b | 32 | |
sillevl | 8:597ce8a7d34b | 33 | int Xbee::running(){ |
sillevl | 8:597ce8a7d34b | 34 | return run; |
sillevl | 8:597ce8a7d34b | 35 | } |
sillevl | 8:597ce8a7d34b | 36 | |
sillevl | 8:597ce8a7d34b | 37 | int Xbee::stopped(){ |
sillevl | 8:597ce8a7d34b | 38 | return !running(); |
sillevl | 8:597ce8a7d34b | 39 | } |
sillevl | 8:597ce8a7d34b | 40 | |
sillevl | 8:597ce8a7d34b | 41 | void Xbee::received(){ |
sillevl | 8:597ce8a7d34b | 42 | char c; |
sillevl | 8:597ce8a7d34b | 43 | while(xbee->readable()){ |
sillevl | 8:597ce8a7d34b | 44 | c = xbee->getc(); |
sillevl | 8:597ce8a7d34b | 45 | putc(c, stdout); |
sillevl | 8:597ce8a7d34b | 46 | buffer[buffer_pos] = c; |
sillevl | 8:597ce8a7d34b | 47 | buffer_pos++; |
sillevl | 8:597ce8a7d34b | 48 | if(c == '\n'){ |
sillevl | 8:597ce8a7d34b | 49 | buffer[buffer_pos] = '\0'; |
sillevl | 8:597ce8a7d34b | 50 | |
sillevl | 8:597ce8a7d34b | 51 | printf("buffer: %s\r\n", buffer); |
sillevl | 8:597ce8a7d34b | 52 | |
sillevl | 8:597ce8a7d34b | 53 | buffer_pos = 0; |
sillevl | 8:597ce8a7d34b | 54 | MbedJSONValue json; |
sillevl | 8:597ce8a7d34b | 55 | parse(json, buffer); |
sillevl | 8:597ce8a7d34b | 56 | |
sillevl | 8:597ce8a7d34b | 57 | int code = -1; |
sillevl | 8:597ce8a7d34b | 58 | if(json.hasMember("start")){ |
sillevl | 8:597ce8a7d34b | 59 | code = json["start"].get<int>(); |
sillevl | 8:597ce8a7d34b | 60 | } else if(json.hasMember("stop")){ |
sillevl | 8:597ce8a7d34b | 61 | code = json["stop"].get<int>(); |
sillevl | 8:597ce8a7d34b | 62 | } else { |
sillevl | 8:597ce8a7d34b | 63 | break; |
sillevl | 8:597ce8a7d34b | 64 | } |
sillevl | 8:597ce8a7d34b | 65 | |
sillevl | 8:597ce8a7d34b | 66 | printf("code: %d\r\n", code); |
sillevl | 8:597ce8a7d34b | 67 | |
sillevl | 8:597ce8a7d34b | 68 | if(json.hasMember("start") && code == this->code){ |
sillevl | 8:597ce8a7d34b | 69 | run = true; |
sillevl | 8:597ce8a7d34b | 70 | printf("start\r\n"); |
sillevl | 8:597ce8a7d34b | 71 | } else if(code == this->code){ |
sillevl | 8:597ce8a7d34b | 72 | run = false; |
sillevl | 8:597ce8a7d34b | 73 | printf("stop\r\n"); |
sillevl | 8:597ce8a7d34b | 74 | } |
sillevl | 8:597ce8a7d34b | 75 | } |
sillevl | 8:597ce8a7d34b | 76 | } |
sillevl | 8:597ce8a7d34b | 77 | } |