-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
executable file
·190 lines (153 loc) · 5.9 KB
/
app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
******************************************************************************
** ファイル名 : app.c
**
** 概要 : 2輪倒立振子ライントレースロボットのTOPPERS/HRP2用Cサンプルプログラム
**
** 注記 : sample_c4 (sample_c3にBluetooth通信リモートスタート機能を追加)
** C用のサンプルコードsample_c4をmrubyに移植し、Ruby向けに調整
******************************************************************************
**/
#include "ev3api.h"
#include "syssvc/serial.h" // for Bluetooth(log)
#include "app.h"
#if defined(BUILD_MODULE)
#include "module_cfg.h"
#else
#include "kernel_cfg.h"
#endif
#include "mruby.h"
#include "mruby/irep.h"
#include "mruby/string.h"
#define DEBUG
#ifdef DEBUG
#define _debug(x) (x)
#else
#define _debug(x)
#endif
/* LCDフォントサイズ */
#define CALIB_FONT (EV3_FONT_SMALL)
#define CALIB_FONT_WIDTH (6/*TODO: magic number*/)
#define CALIB_FONT_HEIGHT (8/*TODO: magic number*/)
/* void* */
/* mrb_tlsf_allocf(mrb_state *mrb, void *p, size_t size, void *ud) */
/* { */
/* if (size == 0) { */
/* tlsf_free(p); */
/* return NULL; */
/* } */
/* else { */
/* return tlsf_realloc(p, size); */
/* } */
/* } */
/* メインタスク */
void main_task(intptr_t unused)
{
/* LCD画面にログを表示 */
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
ev3_lcd_draw_string("EV3way-ET step 1", 0, CALIB_FONT_HEIGHT*1);
static mrb_state *mrb = NULL;
mrb_value ret;
mrb = mrb_open();
struct RClass * ev3rt = mrb_class_get(mrb, "EV3RT");
mrb_define_const(mrb, ev3rt, "BALANCE_TASK_ID", mrb_fixnum_value(BALANCE_TASK));
mrb_define_const(mrb, ev3rt, "WATCH_TASK_ID", mrb_fixnum_value(WATCH_TASK));
mrb_define_const(mrb, ev3rt, "MAIN_TASK_ID", mrb_fixnum_value(MAIN_TASK));
mrb_define_const(mrb, ev3rt, "BALANCE_CYC_ID", mrb_fixnum_value(BALANCE_CYC));
mrb_define_const(mrb, ev3rt, "WATCH_CYC_ID", mrb_fixnum_value(WATCH_CYC));
//mrb->code_fetch_hook = code_fetch; //デバッグできるようになったら使う
/* LCD画面にログを表示 */
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
ev3_lcd_draw_string("EV3way-ET step 2", 0, CALIB_FONT_HEIGHT*1);
#include "app_ruby.h"
/* LCD画面にログを表示 */
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
ev3_lcd_draw_string("EV3way-ET step 3", 0, CALIB_FONT_HEIGHT*1);
ret = mrb_load_irep (mrb, bcode);
if(mrb->exc){
if(!mrb_undef_p(ret)){
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
ev3_lcd_draw_string("EV3way-ET ERR", 0, CALIB_FONT_HEIGHT*1);
mrb_value s = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
if (mrb_string_p(s)) {
ev3_lcd_draw_string(RSTRING_PTR(s), 0, CALIB_FONT_HEIGHT*3);
serial_wri_dat(SIO_PORT_BT, RSTRING_PTR(s), RSTRING_LEN(s));
} else {
ev3_lcd_draw_string("error unknown", 0, CALIB_FONT_HEIGHT*3);
}
}
}
mrb_close(mrb);
ext_tsk();
}
// 停止ボタン監視タスク
void cyclick_handler_status_check(intptr_t exinf) {
// const char msg[23] = "cyc_handler wake up!\r\n";
// serial_wri_dat(SIO_PORT_BT, (const char *)msg, 23);
wup_tsk(WATCH_TASK);
}
void watch_task(intptr_t exinf) {
static mrb_state *mrb = NULL;
mrb_value ret;
mrb = mrb_open();
struct RClass * ev3rt = mrb_class_get(mrb, "EV3RT");
mrb_define_const(mrb, ev3rt, "BALANCE_TASK_ID", mrb_fixnum_value(BALANCE_TASK));
mrb_define_const(mrb, ev3rt, "WATCH_TASK_ID", mrb_fixnum_value(WATCH_TASK));
mrb_define_const(mrb, ev3rt, "MAIN_TASK_ID", mrb_fixnum_value(MAIN_TASK));
mrb_define_const(mrb, ev3rt, "BALANCE_CYC_ID", mrb_fixnum_value(BALANCE_CYC));
mrb_define_const(mrb, ev3rt, "WATCH_CYC_ID", mrb_fixnum_value(WATCH_CYC));
#include "watch_ruby.h"
ret = mrb_load_irep (mrb, watchcode);
if(mrb->exc){
if(!mrb_undef_p(ret)){
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
ev3_lcd_draw_string("EV3way-ET ERR", 0, CALIB_FONT_HEIGHT*1);
mrb_value s = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
if (mrb_string_p(s)) {
ev3_lcd_draw_string(RSTRING_PTR(s), 0, CALIB_FONT_HEIGHT*3);
serial_wri_dat(SIO_PORT_BT, RSTRING_PTR(s), RSTRING_LEN(s));
} else {
ev3_lcd_draw_string("error unknown", 0, CALIB_FONT_HEIGHT*3);
}
}
}
mrb_close(mrb);
}
void cyclick_handler(intptr_t exinf) {
// const char msg[23] = "cyc_handler wake up!\r\n";
// serial_wri_dat(SIO_PORT_BT, (const char *)msg, 23);
wup_tsk(BALANCE_TASK);
}
// 倒立制御タスク
void cyclick_balance(intptr_t exinf) {
// const char msg[23] = "cyc_balance wake up!\r\n";
// serial_wri_dat(SIO_PORT_BT, (const char *)msg, 23);
static mrb_state *mrb = NULL;
mrb_value ret;
mrb = mrb_open();
struct RClass * ev3rt = mrb_class_get(mrb, "EV3RT");
mrb_define_const(mrb, ev3rt, "BALANCE_TASK_ID", mrb_fixnum_value(BALANCE_TASK));
mrb_define_const(mrb, ev3rt, "WATCH_TASK_ID", mrb_fixnum_value(WATCH_TASK));
mrb_define_const(mrb, ev3rt, "MAIN_TASK_ID", mrb_fixnum_value(MAIN_TASK));
mrb_define_const(mrb, ev3rt, "BALANCE_CYC_ID", mrb_fixnum_value(BALANCE_CYC));
mrb_define_const(mrb, ev3rt, "WATCH_CYC_ID", mrb_fixnum_value(WATCH_CYC));
#include "balance_ruby.h"
ret = mrb_load_irep (mrb, cyccode);
if(mrb->exc){
if(!mrb_undef_p(ret)){
ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
ev3_lcd_draw_string("EV3way-ET ERR", 0, CALIB_FONT_HEIGHT*1);
mrb_value s = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
if (mrb_string_p(s)) {
ev3_lcd_draw_string(RSTRING_PTR(s), 0, CALIB_FONT_HEIGHT*3);
serial_wri_dat(SIO_PORT_BT, RSTRING_PTR(s), RSTRING_LEN(s));
} else {
ev3_lcd_draw_string("error unknown", 0, CALIB_FONT_HEIGHT*3);
}
}
}
mrb_close(mrb);
}
int _fini(void){
return 0;
}