serial_communication.cpp@4:835d7aff5c58, 2016-04-09 (annotated)
- Committer:
- inst
- Date:
- Sat Apr 09 02:09:17 2016 +0000
- Revision:
- 4:835d7aff5c58
- Parent:
- 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 | 4:835d7aff5c58 | 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 | 4:835d7aff5c58 | 19 | */ |
inst | 0:964bbeb0a1d3 | 20 | } |
inst | 0:964bbeb0a1d3 | 21 | |
inst | 0:964bbeb0a1d3 | 22 | serial_communication::~serial_communication() { |
inst | 0:964bbeb0a1d3 | 23 | delete instance_; |
inst | 0:964bbeb0a1d3 | 24 | } |
inst | 0:964bbeb0a1d3 | 25 | |
inst | 1:36cd04f61e68 | 26 | int serial_communication::get(string key) { |
inst | 0:964bbeb0a1d3 | 27 | return map_[key]; |
inst | 0:964bbeb0a1d3 | 28 | } |
inst | 0:964bbeb0a1d3 | 29 | |
inst | 0:964bbeb0a1d3 | 30 | bool serial_communication::update() { |
inst | 0:964bbeb0a1d3 | 31 | if (is_new_data_existing_) { |
inst | 0:964bbeb0a1d3 | 32 | is_new_data_existing_ = false; |
inst | 0:964bbeb0a1d3 | 33 | SimpleMapSerialization::simpleMapDeSerializer(data_, ',', map_); |
inst | 0:964bbeb0a1d3 | 34 | return true; |
inst | 0:964bbeb0a1d3 | 35 | } |
inst | 0:964bbeb0a1d3 | 36 | |
inst | 0:964bbeb0a1d3 | 37 | return false; |
inst | 0:964bbeb0a1d3 | 38 | } |
inst | 0:964bbeb0a1d3 | 39 | |
inst | 0:964bbeb0a1d3 | 40 | void serial_communication::receive() { |
inst | 0:964bbeb0a1d3 | 41 | char c = serial_.getc(); |
inst | 0:964bbeb0a1d3 | 42 | |
inst | 0:964bbeb0a1d3 | 43 | switch (c) { |
inst | 0:964bbeb0a1d3 | 44 | case '\n': |
inst | 0:964bbeb0a1d3 | 45 | data_ = buffer_; |
inst | 0:964bbeb0a1d3 | 46 | buffer_.erase(); |
inst | 0:964bbeb0a1d3 | 47 | buffer_ += "a,0,"; |
inst | 0:964bbeb0a1d3 | 48 | is_new_data_existing_ = true; |
inst | 0:964bbeb0a1d3 | 49 | |
inst | 0:964bbeb0a1d3 | 50 | break; |
inst | 0:964bbeb0a1d3 | 51 | |
inst | 0:964bbeb0a1d3 | 52 | case '\r': |
inst | 0:964bbeb0a1d3 | 53 | break; |
inst | 0:964bbeb0a1d3 | 54 | |
inst | 0:964bbeb0a1d3 | 55 | default: |
inst | 0:964bbeb0a1d3 | 56 | buffer_ += c; |
inst | 0:964bbeb0a1d3 | 57 | break; |
inst | 0:964bbeb0a1d3 | 58 | } |
inst | 0:964bbeb0a1d3 | 59 | } |