serial_communication.cpp@2:27ac6b5ec2d1, 2016-04-03 (annotated)
- Committer:
- inst
- Date:
- Sun Apr 03 14:18:34 2016 +0000
- Revision:
- 2:27ac6b5ec2d1
- Parent:
- 1:36cd04f61e68
- Child:
- 3:7655bc258adf
?????;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:964bbeb0a1d3 | 1 | #include "serial_communication.hpp" |
inst | 0:964bbeb0a1d3 | 2 | #include "mbed.h" |
inst | 0:964bbeb0a1d3 | 3 | #include "SimpleMapDeSerializer.hpp" |
inst | 1:36cd04f61e68 | 4 | #include "communication.hpp" |
inst | 0:964bbeb0a1d3 | 5 | #include <map> |
inst | 0:964bbeb0a1d3 | 6 | |
inst | 0:964bbeb0a1d3 | 7 | serial_communication* serial_communication::instance_ = NULL; |
inst | 0:964bbeb0a1d3 | 8 | |
inst | 0:964bbeb0a1d3 | 9 | serial_communication::serial_communication() : serial_(USBTX, USBRX), |
inst | 0:964bbeb0a1d3 | 10 | semaphore_(true), |
inst | 0:964bbeb0a1d3 | 11 | is_new_data_existing_(false) { |
inst | 1:36cd04f61e68 | 12 | // 基準値で初期化 |
inst | 1:36cd04f61e68 | 13 | map_["stkLX"] = communication::velocity_offset_; |
inst | 1:36cd04f61e68 | 14 | map_["stkLY"] = communication::velocity_offset_; |
inst | 2:27ac6b5ec2d1 | 15 | |
inst | 0:964bbeb0a1d3 | 16 | serial_.baud(115200); |
inst | 0:964bbeb0a1d3 | 17 | serial_.format(7, RawSerial::None, 1); |
inst | 0:964bbeb0a1d3 | 18 | serial_.attach(this, &serial_communication::receive); |
inst | 0:964bbeb0a1d3 | 19 | } |
inst | 0:964bbeb0a1d3 | 20 | |
inst | 0:964bbeb0a1d3 | 21 | serial_communication::~serial_communication() { |
inst | 0:964bbeb0a1d3 | 22 | delete instance_; |
inst | 0:964bbeb0a1d3 | 23 | } |
inst | 0:964bbeb0a1d3 | 24 | |
inst | 1:36cd04f61e68 | 25 | int serial_communication::get(string key) { |
inst | 0:964bbeb0a1d3 | 26 | return map_[key]; |
inst | 0:964bbeb0a1d3 | 27 | } |
inst | 0:964bbeb0a1d3 | 28 | |
inst | 0:964bbeb0a1d3 | 29 | bool serial_communication::update() { |
inst | 0:964bbeb0a1d3 | 30 | if (is_new_data_existing_) { |
inst | 0:964bbeb0a1d3 | 31 | is_new_data_existing_ = false; |
inst | 0:964bbeb0a1d3 | 32 | SimpleMapSerialization::simpleMapDeSerializer(data_, ',', map_); |
inst | 0:964bbeb0a1d3 | 33 | return true; |
inst | 0:964bbeb0a1d3 | 34 | } |
inst | 0:964bbeb0a1d3 | 35 | |
inst | 0:964bbeb0a1d3 | 36 | return false; |
inst | 0:964bbeb0a1d3 | 37 | } |
inst | 0:964bbeb0a1d3 | 38 | |
inst | 0:964bbeb0a1d3 | 39 | void serial_communication::receive() { |
inst | 0:964bbeb0a1d3 | 40 | char c = serial_.getc(); |
inst | 0:964bbeb0a1d3 | 41 | |
inst | 0:964bbeb0a1d3 | 42 | switch (c) { |
inst | 0:964bbeb0a1d3 | 43 | case '\n': |
inst | 0:964bbeb0a1d3 | 44 | data_ = buffer_; |
inst | 0:964bbeb0a1d3 | 45 | buffer_.erase(); |
inst | 0:964bbeb0a1d3 | 46 | buffer_ += "a,0,"; |
inst | 0:964bbeb0a1d3 | 47 | is_new_data_existing_ = true; |
inst | 0:964bbeb0a1d3 | 48 | |
inst | 0:964bbeb0a1d3 | 49 | break; |
inst | 0:964bbeb0a1d3 | 50 | |
inst | 0:964bbeb0a1d3 | 51 | case '\r': |
inst | 0:964bbeb0a1d3 | 52 | break; |
inst | 0:964bbeb0a1d3 | 53 | |
inst | 0:964bbeb0a1d3 | 54 | default: |
inst | 0:964bbeb0a1d3 | 55 | //i2c.write(0x11 << 1, &c, 1); |
inst | 0:964bbeb0a1d3 | 56 | buffer_ += c; |
inst | 0:964bbeb0a1d3 | 57 | break; |
inst | 0:964bbeb0a1d3 | 58 | } |
inst | 0:964bbeb0a1d3 | 59 | } |