nucleo側のプログラム

Dependents:   serial_connected_mcu_nucleo serial_connected_mcu_nucleo

Fork of serial_connected_mcu by tarou yamada

Committer:
inst
Date:
Sun Jul 10 06:53:44 2016 +0000
Revision:
3:c927b60f053c
Child:
4:1323ef48a984
add serial_connected_mcu_slave;

Who changed what in which revision?

UserRevisionLine numberNew 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 //port->printf("%d,%d,%d,%d,%d,%d\n", array_of_write_data[0], array_of_write_data[1], array_of_write_data[2], array_of_write_data[3], array_of_write_data[4], array_of_write_data[5]);
inst 3:c927b60f053c 34 for (int i = 0;;i++){
inst 3:c927b60f053c 35 port->printf("%d", array_of_write_data[i]);
inst 3:c927b60f053c 36 if (i == (SIZE_OF_WRITE_DATA - 1)){
inst 3:c927b60f053c 37 break;
inst 3:c927b60f053c 38 }
inst 3:c927b60f053c 39 port->printf(",");
inst 3:c927b60f053c 40 }
inst 3:c927b60f053c 41 port->printf("\n");
inst 3:c927b60f053c 42 string_to_int();
inst 3:c927b60f053c 43 received_buffer.clear();
inst 3:c927b60f053c 44 return;
inst 3:c927b60f053c 45 }
inst 3:c927b60f053c 46 received_buffer.push_back(received_char);
inst 3:c927b60f053c 47 }
inst 3:c927b60f053c 48
inst 3:c927b60f053c 49 void serial_connected_mcu_slave::string_to_int(){
inst 3:c927b60f053c 50 std::string s;
inst 3:c927b60f053c 51 int i = 0;
inst 3:c927b60f053c 52
inst 3:c927b60f053c 53 while (is_locking);
inst 3:c927b60f053c 54 is_locking = true;
inst 3:c927b60f053c 55 s.clear();
inst 3:c927b60f053c 56
inst 3:c927b60f053c 57 for (int index = 0; index < received_buffer.size(); index++){
inst 3:c927b60f053c 58 char c = received_buffer[index];
inst 3:c927b60f053c 59 if (c == ','){
inst 3:c927b60f053c 60 array_of_read_data[i] = atoi(s.c_str());
inst 3:c927b60f053c 61 i++;
inst 3:c927b60f053c 62 s.clear();
inst 3:c927b60f053c 63 }
inst 3:c927b60f053c 64 else {
inst 3:c927b60f053c 65 s.push_back(c);
inst 3:c927b60f053c 66 }
inst 3:c927b60f053c 67 }
inst 3:c927b60f053c 68 array_of_read_data[i] = atoi(s.c_str());
inst 3:c927b60f053c 69 is_locking = false;
inst 3:c927b60f053c 70 }
inst 3:c927b60f053c 71
inst 3:c927b60f053c 72 }