ROBOSTEP_5期 / Mbed 2 deprecated serial_to_python

Dependencies:   mbed

Committer:
yuto17320508
Date:
Sun Sep 01 09:46:15 2019 +0000
Revision:
0:36b37fd10b58
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuto17320508 0:36b37fd10b58 1 #include "mbed.h"
yuto17320508 0:36b37fd10b58 2 #include "string"
yuto17320508 0:36b37fd10b58 3 #include "vector"
yuto17320508 0:36b37fd10b58 4
yuto17320508 0:36b37fd10b58 5 DigitalOut led(LED1);
yuto17320508 0:36b37fd10b58 6
yuto17320508 0:36b37fd10b58 7 Serial pc(USBTX, USBRX); //rtosの機能を使うときはSerialではなくRawSerialを使えとの情報をみてこうしています。
yuto17320508 0:36b37fd10b58 8
yuto17320508 0:36b37fd10b58 9 char data[100];
yuto17320508 0:36b37fd10b58 10 int data_index = 0;
yuto17320508 0:36b37fd10b58 11
yuto17320508 0:36b37fd10b58 12 std::vector<std::string> separate_string(std::string input)
yuto17320508 0:36b37fd10b58 13 {
yuto17320508 0:36b37fd10b58 14 std::vector<std::string> return_data;
yuto17320508 0:36b37fd10b58 15 std::string separator = std::string(",");
yuto17320508 0:36b37fd10b58 16 int separator_length = separator.length();
yuto17320508 0:36b37fd10b58 17 std::string::size_type offset = std::string::size_type(0);
yuto17320508 0:36b37fd10b58 18 while(1)
yuto17320508 0:36b37fd10b58 19 {
yuto17320508 0:36b37fd10b58 20 int pos = input.find(separator, offset);
yuto17320508 0:36b37fd10b58 21 if (pos == std::string::npos)
yuto17320508 0:36b37fd10b58 22 {
yuto17320508 0:36b37fd10b58 23 return_data.push_back(input.substr(offset));
yuto17320508 0:36b37fd10b58 24 break;
yuto17320508 0:36b37fd10b58 25 }
yuto17320508 0:36b37fd10b58 26 return_data.push_back(input.substr(offset,pos-offset));
yuto17320508 0:36b37fd10b58 27 offset = pos + separator_length;
yuto17320508 0:36b37fd10b58 28 }
yuto17320508 0:36b37fd10b58 29 return return_data;
yuto17320508 0:36b37fd10b58 30 }
yuto17320508 0:36b37fd10b58 31
yuto17320508 0:36b37fd10b58 32
yuto17320508 0:36b37fd10b58 33
yuto17320508 0:36b37fd10b58 34 int main(void){
yuto17320508 0:36b37fd10b58 35 while(1){
yuto17320508 0:36b37fd10b58 36 if (!pc.readable()) continue;
yuto17320508 0:36b37fd10b58 37
yuto17320508 0:36b37fd10b58 38 char c = pc.getc();
yuto17320508 0:36b37fd10b58 39 data[data_index++] = c;
yuto17320508 0:36b37fd10b58 40 if (c == '\n')
yuto17320508 0:36b37fd10b58 41 {
yuto17320508 0:36b37fd10b58 42 data[data_index] = '\0';
yuto17320508 0:36b37fd10b58 43 std::string str;
yuto17320508 0:36b37fd10b58 44 str = data;
yuto17320508 0:36b37fd10b58 45 std::vector<std::string> data_list = separate_string(str);
yuto17320508 0:36b37fd10b58 46 for(int i=0;data_list.size();++i)
yuto17320508 0:36b37fd10b58 47 {
yuto17320508 0:36b37fd10b58 48 pc.puts(data_list[i].c_str());
yuto17320508 0:36b37fd10b58 49 pc.puts("+");
yuto17320508 0:36b37fd10b58 50 }
yuto17320508 0:36b37fd10b58 51 data_index = 0;
yuto17320508 0:36b37fd10b58 52 }
yuto17320508 0:36b37fd10b58 53
yuto17320508 0:36b37fd10b58 54 }
yuto17320508 0:36b37fd10b58 55 }