Committer:
kachikyun
Date:
Sun Jul 10 06:50:33 2016 +0000
Revision:
0:fb5a8d9d8aba

        

Who changed what in which revision?

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