nucleo側のプログラム

Dependents:   serial_connected_mcu_nucleo serial_connected_mcu_nucleo

Fork of serial_connected_mcu by tarou yamada

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