etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
app.cpp
[詳解]
1 
9 #include "ev3api.h"
10 #include "app.h"
11 #include "util.h"
12 #include "ColorSensor.h"
13 #include "SonarSensor.h"
14 #include "TouchSensor.h"
15 #include <string>
16 
17 using namespace ev3api;
18 
19 #if defined(BUILD_MODULE)
20 #include "module_cfg.h"
21 #else
22 #include "kernel_cfg.h"
23 #endif
24 
25 #define DEBUG
26 
27 #ifdef DEBUG
28 #define _debug(x) (x)
29 #else
30 #define _debug(x)
31 #endif
32 
33 static int g_bluetooth_command = 0; // Bluetoothコマンド 1:リモートスタート
34 static FILE* g_bluetooth = NULL; // Bluetoothファイルハンドル
35 
36 /* メインタスク */
37 void main_task(intptr_t unused)
38 {
39  /* Open Bluetooth file */
40  g_bluetooth = ev3_serial_open_file(EV3_SERIAL_BT);
41  assert(g_bluetooth != NULL);
42 
43  /* Bluetooth通信タスクの起動 */
44  act_tsk(BT_TASK);
45 
46  msg_f("Check Colors", 1);
47  msg_f(" create from github.com/korosuke613/etrobocon2017", 2);
48  ColorSensor colorSensor(PORT_3);
49  TouchSensor touchSensor(PORT_1);
50  SonarSensor sonarSensor(PORT_2);
51  int time_count = 0;
52  char msg[32];
53  int color_num;
54  std::string color_name[8]
55  = { "NONE", "BLACK", "BLUE", "GREEN", "YELLOW", "RED", "WHITE", "BROWN" };
56  rgb_raw_t rgb;
57  while(1) {
58  color_num = (int)colorSensor.getColorNumber();
59  colorSensor.getRawColor(rgb);
60  // sprintf(msg, "LightValue: %d", colorSensor.getBrightness());
61  // msg_f(msg, 4);
62 
63  sprintf(msg, "ColorNumber: %s", color_name[color_num].c_str());
64  msg_f(msg, 5);
65  sprintf(msg, " R:%3d G:%3d B:%3d", rgb.r, rgb.g, rgb.b);
66  msg_f(msg, 6);
67 
68  sprintf(msg, "DistanceEye: %d", sonarSensor.getDistance());
69  msg_f(msg, 7);
70 
71  if(sonarSensor.getDistance() < 10) {
72  time_count++;
73  }
74 
75  sprintf(msg, "DistanceTime: %d", time_count);
76  msg_f(msg, 8);
77 
78  if(touchSensor.isPressed() == 1) {
79  break; /* タッチセンサが押された */
80  }
81  tslp_tsk(4);
82  }
83 
84  ter_tsk(BT_TASK);
85  fclose(g_bluetooth);
86 
87  ext_tsk();
88 }
89 
97 void bt_task(intptr_t unused)
98 {
99  while(1) {
100  uint8_t c = fgetc(g_bluetooth); /* 受信 */
101  switch(c) {
102  case '1':
103  g_bluetooth_command = 1;
104  break;
105  default:
106  break;
107  }
108  fputc(c, g_bluetooth); /* エコーバック */
109  }
110 }
void getRawColor(rgb_raw_t &rgb) const
Definition: ColorSensor.cpp:48
void main_task(intptr_t unused)
Definition: app.cpp:37
void msg_f(const char *str, int32_t line)
Definition: util.cpp:17
Definition: Port.h:20
int16_t getDistance(void) const
Definition: SonarSensor.cpp:29
Definition: Port.h:22
colorid_t getColorNumber(void) const
Definition: ColorSensor.cpp:41
void bt_task(intptr_t unused)
Definition: app.cpp:97
bool isPressed(void) const
Definition: TouchSensor.cpp:20
Definition: Port.h:21