serial_communication.cpp@0:964bbeb0a1d3, 2016-03-31 (annotated)
- Committer:
- inst
- Date:
- Thu Mar 31 04:42:48 2016 +0000
- Revision:
- 0:964bbeb0a1d3
- Child:
- 1:36cd04f61e68
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 | 0:964bbeb0a1d3 | 4 | #include <map> |
inst | 0:964bbeb0a1d3 | 5 | |
inst | 0:964bbeb0a1d3 | 6 | serial_communication* serial_communication::instance_ = NULL; |
inst | 0:964bbeb0a1d3 | 7 | |
inst | 0:964bbeb0a1d3 | 8 | serial_communication::serial_communication() : serial_(USBTX, USBRX), |
inst | 0:964bbeb0a1d3 | 9 | semaphore_(true), |
inst | 0:964bbeb0a1d3 | 10 | is_new_data_existing_(false) { |
inst | 0:964bbeb0a1d3 | 11 | serial_.baud(115200); |
inst | 0:964bbeb0a1d3 | 12 | serial_.format(7, RawSerial::None, 1); |
inst | 0:964bbeb0a1d3 | 13 | serial_.attach(this, &serial_communication::receive); |
inst | 0:964bbeb0a1d3 | 14 | } |
inst | 0:964bbeb0a1d3 | 15 | |
inst | 0:964bbeb0a1d3 | 16 | serial_communication::~serial_communication() { |
inst | 0:964bbeb0a1d3 | 17 | delete instance_; |
inst | 0:964bbeb0a1d3 | 18 | } |
inst | 0:964bbeb0a1d3 | 19 | |
inst | 0:964bbeb0a1d3 | 20 | int serial_communication::get_data(string key) { |
inst | 0:964bbeb0a1d3 | 21 | //while (!semaphore_) {} |
inst | 0:964bbeb0a1d3 | 22 | return map_[key]; |
inst | 0:964bbeb0a1d3 | 23 | } |
inst | 0:964bbeb0a1d3 | 24 | |
inst | 0:964bbeb0a1d3 | 25 | bool serial_communication::update() { |
inst | 0:964bbeb0a1d3 | 26 | if (is_new_data_existing_) { |
inst | 0:964bbeb0a1d3 | 27 | is_new_data_existing_ = false; |
inst | 0:964bbeb0a1d3 | 28 | SimpleMapSerialization::simpleMapDeSerializer(data_, ',', map_); |
inst | 0:964bbeb0a1d3 | 29 | return true; |
inst | 0:964bbeb0a1d3 | 30 | } |
inst | 0:964bbeb0a1d3 | 31 | |
inst | 0:964bbeb0a1d3 | 32 | return false; |
inst | 0:964bbeb0a1d3 | 33 | } |
inst | 0:964bbeb0a1d3 | 34 | |
inst | 0:964bbeb0a1d3 | 35 | I2C i2c(I2C_SDA, I2C_SCL); |
inst | 0:964bbeb0a1d3 | 36 | |
inst | 0:964bbeb0a1d3 | 37 | void serial_communication::receive() { |
inst | 0:964bbeb0a1d3 | 38 | char c = serial_.getc(); |
inst | 0:964bbeb0a1d3 | 39 | |
inst | 0:964bbeb0a1d3 | 40 | switch (c) { |
inst | 0:964bbeb0a1d3 | 41 | case '\n': |
inst | 0:964bbeb0a1d3 | 42 | /* |
inst | 0:964bbeb0a1d3 | 43 | semaphore_ = false; |
inst | 0:964bbeb0a1d3 | 44 | SimpleMapSerialization::simpleMapDeSerializer(buffer_, ',', map_); |
inst | 0:964bbeb0a1d3 | 45 | semaphore_ = true; |
inst | 0:964bbeb0a1d3 | 46 | */ |
inst | 0:964bbeb0a1d3 | 47 | data_ = buffer_; |
inst | 0:964bbeb0a1d3 | 48 | buffer_.erase(); |
inst | 0:964bbeb0a1d3 | 49 | buffer_ += "a,0,"; |
inst | 0:964bbeb0a1d3 | 50 | is_new_data_existing_ = true; |
inst | 0:964bbeb0a1d3 | 51 | |
inst | 0:964bbeb0a1d3 | 52 | break; |
inst | 0:964bbeb0a1d3 | 53 | |
inst | 0:964bbeb0a1d3 | 54 | case '\r': |
inst | 0:964bbeb0a1d3 | 55 | break; |
inst | 0:964bbeb0a1d3 | 56 | |
inst | 0:964bbeb0a1d3 | 57 | default: |
inst | 0:964bbeb0a1d3 | 58 | //i2c.write(0x11 << 1, &c, 1); |
inst | 0:964bbeb0a1d3 | 59 | buffer_ += c; |
inst | 0:964bbeb0a1d3 | 60 | break; |
inst | 0:964bbeb0a1d3 | 61 | } |
inst | 0:964bbeb0a1d3 | 62 | } |