nucleo側のプログラム
Dependents: serial_connected_mcu_nucleo serial_connected_mcu_nucleo
Fork of serial_connected_mcu by
serial_connected_mcu_slave.cpp@6:a0cc9c27cedf, 2016-07-10 (annotated)
- Committer:
- inst
- Date:
- Sun Jul 10 07:38:13 2016 +0000
- Revision:
- 6:a0cc9c27cedf
- Parent:
- 4:1323ef48a984
- Child:
- 5:77d6f1ddf2e4
- Child:
- 7:f8dd6ab9ce94
???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 3:c927b60f053c | 1 | #include "serial_connected_mcu_slave.hpp" |
inst | 3:c927b60f053c | 2 | |
inst | 3:c927b60f053c | 3 | namespace serial_connected_mcu{ |
inst | 3:c927b60f053c | 4 | serial_connected_mcu_slave::serial_connected_mcu_slave(){ |
inst | 3:c927b60f053c | 5 | port = new Serial(PIN_SERIAL_TX, PIN_SERIAL_RX); |
inst | 3:c927b60f053c | 6 | port->baud(BAUDRATE); |
inst | 3:c927b60f053c | 7 | array_of_read_data = new int16_t[SIZE_OF_READ_DATA]; |
inst | 3:c927b60f053c | 8 | array_of_write_data = new int16_t[SIZE_OF_WRITE_DATA]; |
inst | 3:c927b60f053c | 9 | is_locking = false; |
inst | 3:c927b60f053c | 10 | received_buffer.clear(); |
inst | 3:c927b60f053c | 11 | |
inst | 3:c927b60f053c | 12 | port->attach(this, &serial_connected_mcu_slave::on_received, Serial::RxIrq); |
inst | 3:c927b60f053c | 13 | } |
inst | 3:c927b60f053c | 14 | |
inst | 3:c927b60f053c | 15 | serial_connected_mcu_slave::~serial_connected_mcu_slave(){ |
inst | 3:c927b60f053c | 16 | delete (port); |
inst | 3:c927b60f053c | 17 | } |
inst | 3:c927b60f053c | 18 | |
inst | 3:c927b60f053c | 19 | void serial_connected_mcu_slave::set(int index_of_data, int16_t setting_data){ |
inst | 3:c927b60f053c | 20 | while (is_locking); |
inst | 3:c927b60f053c | 21 | is_locking = true; |
inst | 3:c927b60f053c | 22 | array_of_write_data[index_of_data] = setting_data; |
inst | 3:c927b60f053c | 23 | is_locking = false; |
inst | 3:c927b60f053c | 24 | } |
inst | 3:c927b60f053c | 25 | |
inst | 3:c927b60f053c | 26 | int16_t serial_connected_mcu_slave::get(int index_of_data){ |
inst | 3:c927b60f053c | 27 | return (array_of_read_data[index_of_data]); |
inst | 3:c927b60f053c | 28 | } |
inst | 3:c927b60f053c | 29 | |
inst | 3:c927b60f053c | 30 | void serial_connected_mcu_slave::on_received(){ |
inst | 3:c927b60f053c | 31 | char received_char = port->getc(); |
inst | 3:c927b60f053c | 32 | if (received_char == '\n'){ |
inst | 3:c927b60f053c | 33 | for (int i = 0;;i++){ |
inst | 3:c927b60f053c | 34 | port->printf("%d", array_of_write_data[i]); |
inst | 3:c927b60f053c | 35 | if (i == (SIZE_OF_WRITE_DATA - 1)){ |
inst | 3:c927b60f053c | 36 | break; |
inst | 3:c927b60f053c | 37 | } |
inst | 3:c927b60f053c | 38 | port->printf(","); |
inst | 3:c927b60f053c | 39 | } |
inst | 3:c927b60f053c | 40 | port->printf("\n"); |
inst | 3:c927b60f053c | 41 | string_to_int(); |
inst | 3:c927b60f053c | 42 | received_buffer.clear(); |
inst | 3:c927b60f053c | 43 | return; |
inst | 3:c927b60f053c | 44 | } |
inst | 3:c927b60f053c | 45 | received_buffer.push_back(received_char); |
inst | 3:c927b60f053c | 46 | } |
inst | 3:c927b60f053c | 47 | |
inst | 3:c927b60f053c | 48 | void serial_connected_mcu_slave::string_to_int(){ |
inst | 3:c927b60f053c | 49 | std::string s; |
inst | 3:c927b60f053c | 50 | int i = 0; |
inst | 3:c927b60f053c | 51 | |
inst | 3:c927b60f053c | 52 | while (is_locking); |
inst | 3:c927b60f053c | 53 | is_locking = true; |
inst | 3:c927b60f053c | 54 | s.clear(); |
inst | 3:c927b60f053c | 55 | |
inst | 3:c927b60f053c | 56 | for (int index = 0; index < received_buffer.size(); index++){ |
inst | 3:c927b60f053c | 57 | char c = received_buffer[index]; |
inst | 3:c927b60f053c | 58 | if (c == ','){ |
inst | 3:c927b60f053c | 59 | array_of_read_data[i] = atoi(s.c_str()); |
inst | 3:c927b60f053c | 60 | i++; |
inst | 3:c927b60f053c | 61 | s.clear(); |
inst | 3:c927b60f053c | 62 | } |
inst | 3:c927b60f053c | 63 | else { |
inst | 3:c927b60f053c | 64 | s.push_back(c); |
inst | 3:c927b60f053c | 65 | } |
inst | 3:c927b60f053c | 66 | } |
inst | 3:c927b60f053c | 67 | array_of_read_data[i] = atoi(s.c_str()); |
inst | 3:c927b60f053c | 68 | is_locking = false; |
inst | 3:c927b60f053c | 69 | } |
inst | 3:c927b60f053c | 70 | |
inst | 3:c927b60f053c | 71 | } |