OSDN Git Service

Replace samples.
[nxt-jsp/etrobo-atk.git] / nxtOSEK / samples_c / btmaster / btmaster.c
1 /* btmaster.c */ \r
2 #include "kernel.h"\r
3 #include "kernel_id.h"\r
4 #include "ecrobot_interface.h"\r
5 \r
6 /* OSEK declarations */\r
7 DeclareCounter(SysTimerCnt);\r
8 DeclareTask(EventDispatcher); \r
9 DeclareTask(EventHandler);\r
10 DeclareTask(IdleTask);\r
11 DeclareEvent(TouchSensorOnEvent);\r
12 DeclareEvent(TouchSensorOffEvent); \r
13 \r
14 /* below macro enables run-time Bluetooth connection */\r
15 #define RUNTIME_CONNECTION\r
16 \r
17 /* slave device's Bluetooth Device Address (this has to be modified for your NXT) */\r
18 const U8 bd_addr[7] = {0x00, 0x16, 0x53, 0x04, 0xF1, 0xB3, 0x00};\r
19 \r
20 /* LEJOS OSEK hooks */\r
21 void ecrobot_device_initialize()\r
22 {\r
23 #ifndef RUNTIME_CONNECTION\r
24         ecrobot_init_bt_master(bd_addr, "LEJOS-OSEK");\r
25 #endif\r
26 }\r
27 void ecrobot_device_terminate()\r
28 {\r
29         ecrobot_term_bt_connection();\r
30 }\r
31 \r
32 /* LEJOS OSEK hook to be invoked from an ISR in category 2 */\r
33 void user_1ms_isr_type2(void)\r
34 {\r
35   StatusType ercd;\r
36 \r
37   ercd = SignalCounter(SysTimerCnt); /* Increment OSEK Alarm Counter */\r
38   if(ercd != E_OK)\r
39   {\r
40     ShutdownOS(ercd);\r
41   }\r
42 }\r
43 \r
44 /* EventDispatcher executed every 5ms */\r
45 TASK(EventDispatcher)\r
46 {\r
47   static U8 bt_receive_buf[32]; \r
48   static U8 TouchSensorStatus_old = 0;\r
49   U8 TouchSensorStatus; \r
50 \r
51   /* read packet data from the slave device */  \r
52   ecrobot_read_bt_packet(bt_receive_buf, 32);\r
53   if (bt_receive_buf[0] == 1)\r
54   {\r
55         ecrobot_set_motor_speed(NXT_PORT_B, 100);\r
56   }\r
57   else\r
58   {\r
59         ecrobot_set_motor_speed(NXT_PORT_B, 0);\r
60   }     \r
61 \r
62   TouchSensorStatus = ecrobot_get_touch_sensor(NXT_PORT_S4);\r
63   if (TouchSensorStatus == 1 && TouchSensorStatus_old == 0)\r
64   {\r
65     /* Send a Touch Sensor ON Event to the Handler */ \r
66     SetEvent(EventHandler, TouchSensorOnEvent);\r
67   }\r
68   else if (TouchSensorStatus == 0 && TouchSensorStatus_old == 1)\r
69   {\r
70     /* Send a Touch Sensor OFF Event to the Handler */ \r
71     SetEvent(EventHandler, TouchSensorOffEvent);\r
72   }\r
73   TouchSensorStatus_old = TouchSensorStatus;\r
74 \r
75   TerminateTask();\r
76 }\r
77 \r
78 /* EventHandler executed by OSEK Events */\r
79 TASK(EventHandler)\r
80 {\r
81   static U8 bt_send_buf[32]; \r
82 \r
83   while(1)\r
84   {\r
85     WaitEvent(TouchSensorOnEvent); /* Task is in waiting status until the Event comes */ \r
86     ClearEvent(TouchSensorOnEvent);\r
87         /* send packet data to the slave device */\r
88         bt_send_buf[0] = 1;\r
89         ecrobot_send_bt_packet(bt_send_buf, 32);\r
90 \r
91     WaitEvent(TouchSensorOffEvent); /* Task is in waiting status until the Event comes */\r
92     ClearEvent(TouchSensorOffEvent);\r
93         /* send packet data to the slave device */\r
94         bt_send_buf[0] = 0;\r
95         ecrobot_send_bt_packet(bt_send_buf, 32);\r
96   }\r
97 \r
98   TerminateTask();\r
99 }\r
100 \r
101 /* IdleTask */\r
102 TASK(IdleTask)\r
103 {\r
104   static SINT bt_status = BT_NO_INIT;\r
105 \r
106   while(1)\r
107   {  \r
108 #ifdef RUNTIME_CONNECTION\r
109     ecrobot_init_bt_master(bd_addr, "LEJOS-OSEK");\r
110 #endif\r
111 \r
112     if (ecrobot_get_bt_status() == BT_STREAM && bt_status != BT_STREAM)\r
113     {\r
114       display_clear(0);\r
115       display_goto_xy(0, 0);\r
116       display_string("[BT]");\r
117       display_update();\r
118     }\r
119     bt_status = ecrobot_get_bt_status();\r
120   }     \r
121 }\r