ETrobocon2017 - 片山研究所モデルベース開発推進事業部
app.cpp
[詳解]
1 
9 #include <string>
10 #include "ev3api.h"
11 #include "app.h"
12 #include "util.h"
13 #include "ColorSensor.h"
14 #include "TouchSensor.h"
15 #include "SonarSensor.h"
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 
47  msg_f("Check Colors", 1);
48  msg_f(" create from github.com/korosuke613/etrobocon2017", 2);
49  ColorSensor colorSensor(PORT_3);
50  TouchSensor touchSensor(PORT_1);
51  SonarSensor sonarSensor(PORT_2);
52  int time_count = 0;
53  char msg[32];
54  int color_num;
55  std::string color_name[8] = {"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  {
80  break; /* タッチセンサが押された */
81  }
82  tslp_tsk ( 4 ) ;
83  }
84 
85  ter_tsk( BT_TASK );
86  fclose( g_bluetooth );
87 
88  ext_tsk();
89 }
90 
98 void bt_task( intptr_t unused )
99 {
100  while( 1 )
101  {
102  uint8_t c = fgetc( g_bluetooth ); /* 受信 */
103  switch( c )
104  {
105  case '1':
106  g_bluetooth_command = 1;
107  break;
108  default:
109  break;
110  }
111  fputc( c, g_bluetooth ); /* エコーバック */
112  }
113 }
Definition: Clock.h:12
Definition: Port.h:20
void main_task(intptr_t unused)
Definition: app.cpp:37
Definition: Port.h:22
void bt_task(intptr_t unused)
Definition: app.cpp:98
bool isPressed(void) const
Definition: TouchSensor.cpp:20
void msg_f(const char *str, int32_t line)
Definition: util.cpp:17
int8_t getBrightness(void) const
Definition: ColorSensor.cpp:34
colorid_t getColorNumber(void) const
Definition: ColorSensor.cpp:41
Definition: Port.h:21
void getRawColor(rgb_raw_t &rgb) const
Definition: ColorSensor.cpp:48
int16_t getDistance(void) const
Definition: SonarSensor.cpp:29