nucleo側のプログラム

Dependents:   serial_connected_mcu_nucleo serial_connected_mcu_nucleo

Fork of serial_connected_mcu by tarou yamada

Committer:
kachikyun
Date:
Sun Jul 10 07:40:44 2016 +0000
Revision:
7:f8dd6ab9ce94
Parent:
4:1323ef48a984
Child:
8:b916e6f45f75

        

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{
kachikyun 7:f8dd6ab9ce94 4 const PinName serial_connected_mcu_master::PIN_SERIAL_TX = SERIAL_TX;
kachikyun 7:f8dd6ab9ce94 5
kachikyun 7:f8dd6ab9ce94 6 const PinName serial_connected_mcu_master::PIN_SERIAL_RX = SERIAL_RX;
kachikyun 7:f8dd6ab9ce94 7
inst 3:c927b60f053c 8 serial_connected_mcu_slave::serial_connected_mcu_slave(){
inst 3:c927b60f053c 9 port = new Serial(PIN_SERIAL_TX, PIN_SERIAL_RX);
inst 3:c927b60f053c 10 port->baud(BAUDRATE);
inst 3:c927b60f053c 11 array_of_read_data = new int16_t[SIZE_OF_READ_DATA];
inst 3:c927b60f053c 12 array_of_write_data = new int16_t[SIZE_OF_WRITE_DATA];
inst 3:c927b60f053c 13 is_locking = false;
inst 3:c927b60f053c 14 received_buffer.clear();
inst 3:c927b60f053c 15
inst 3:c927b60f053c 16 port->attach(this, &serial_connected_mcu_slave::on_received, Serial::RxIrq);
inst 3:c927b60f053c 17 }
inst 3:c927b60f053c 18
inst 3:c927b60f053c 19 serial_connected_mcu_slave::~serial_connected_mcu_slave(){
inst 3:c927b60f053c 20 delete (port);
inst 3:c927b60f053c 21 }
inst 3:c927b60f053c 22
inst 3:c927b60f053c 23 void serial_connected_mcu_slave::set(int index_of_data, int16_t setting_data){
inst 3:c927b60f053c 24 while (is_locking);
inst 3:c927b60f053c 25 is_locking = true;
inst 3:c927b60f053c 26 array_of_write_data[index_of_data] = setting_data;
inst 3:c927b60f053c 27 is_locking = false;
inst 3:c927b60f053c 28 }
inst 3:c927b60f053c 29
inst 3:c927b60f053c 30 int16_t serial_connected_mcu_slave::get(int index_of_data){
inst 3:c927b60f053c 31 return (array_of_read_data[index_of_data]);
inst 3:c927b60f053c 32 }
inst 3:c927b60f053c 33
inst 3:c927b60f053c 34 void serial_connected_mcu_slave::on_received(){
inst 3:c927b60f053c 35 char received_char = port->getc();
inst 3:c927b60f053c 36 if (received_char == '\n'){
inst 3:c927b60f053c 37 for (int i = 0;;i++){
inst 3:c927b60f053c 38 port->printf("%d", array_of_write_data[i]);
inst 3:c927b60f053c 39 if (i == (SIZE_OF_WRITE_DATA - 1)){
inst 3:c927b60f053c 40 break;
inst 3:c927b60f053c 41 }
inst 3:c927b60f053c 42 port->printf(",");
inst 3:c927b60f053c 43 }
inst 3:c927b60f053c 44 port->printf("\n");
inst 3:c927b60f053c 45 string_to_int();
inst 3:c927b60f053c 46 received_buffer.clear();
inst 3:c927b60f053c 47 return;
inst 3:c927b60f053c 48 }
inst 3:c927b60f053c 49 received_buffer.push_back(received_char);
inst 3:c927b60f053c 50 }
inst 3:c927b60f053c 51
inst 3:c927b60f053c 52 void serial_connected_mcu_slave::string_to_int(){
inst 3:c927b60f053c 53 std::string s;
inst 3:c927b60f053c 54 int i = 0;
inst 3:c927b60f053c 55
inst 3:c927b60f053c 56 while (is_locking);
inst 3:c927b60f053c 57 is_locking = true;
inst 3:c927b60f053c 58 s.clear();
inst 3:c927b60f053c 59
inst 3:c927b60f053c 60 for (int index = 0; index < received_buffer.size(); index++){
inst 3:c927b60f053c 61 char c = received_buffer[index];
inst 3:c927b60f053c 62 if (c == ','){
inst 3:c927b60f053c 63 array_of_read_data[i] = atoi(s.c_str());
inst 3:c927b60f053c 64 i++;
inst 3:c927b60f053c 65 s.clear();
inst 3:c927b60f053c 66 }
inst 3:c927b60f053c 67 else {
inst 3:c927b60f053c 68 s.push_back(c);
inst 3:c927b60f053c 69 }
inst 3:c927b60f053c 70 }
inst 3:c927b60f053c 71 array_of_read_data[i] = atoi(s.c_str());
inst 3:c927b60f053c 72 is_locking = false;
inst 3:c927b60f053c 73 }
inst 3:c927b60f053c 74
inst 3:c927b60f053c 75 }