1 | /*
|
---|
2 | * ãµã¼ãã»ãã¼ãé¢é£
|
---|
3 | *
|
---|
4 | * Copyright (c) 2016 Wakayama.rb Ruby Board developers
|
---|
5 | *
|
---|
6 | * This software is released under the MIT License.
|
---|
7 | * https://github.com/wakayamarb/wrbb-v2lib-firm/blob/master/MITL
|
---|
8 | *
|
---|
9 | */
|
---|
10 | #include <arduino.h>
|
---|
11 | /*#include <servo.h>*/
|
---|
12 |
|
---|
13 | #include <mruby.h>
|
---|
14 | /*#include <mruby/string.h>*/
|
---|
15 | /*#include <mruby/variable.h>*/
|
---|
16 |
|
---|
17 | #include "../llbruby.h"
|
---|
18 | #include "sKernel.h"
|
---|
19 |
|
---|
20 | #define ATTACH_MAX 12
|
---|
21 | struct pin_node *servo[ATTACH_MAX];
|
---|
22 |
|
---|
23 | /******************************************************/
|
---|
24 | /* ãµã¼ãåºåãä»»æã®ãã³ã«å²ãå½ã¦ã¾ã: Servo.attach*/
|
---|
25 | /* Servo.attach(ch, pin[,min,max])*/
|
---|
26 | /* ch: ãµã¼ãã®ãã£ãã« 0ï½11ã¾ã§æå®ã§ãã¾ã*/
|
---|
27 | /* pin: å²ãå½ã¦ããã³çªå·*/
|
---|
28 | /* min: ãµã¼ãã®è§åº¦ã0度ã®ã¨ãã®ãã«ã¹å¹
|
---|
29 | (ãã¤ã¯ãç§)ãããã©ã«ãã¯544*/
|
---|
30 | /* max: ãµã¼ãã®è§åº¦ã180度ã®ã¨ãã®ãã«ã¹å¹
|
---|
31 | (ãã¤ã¯ãç§)ãããã©ã«ãã¯2400*/
|
---|
32 | /******************************************************/
|
---|
33 | mrb_value mrb_servo_attach(mrb_state *mrb, mrb_value self)
|
---|
34 | {
|
---|
35 | int ch;
|
---|
36 | int pinno;
|
---|
37 | int min;
|
---|
38 | int max;
|
---|
39 | struct pin_node *pin;
|
---|
40 |
|
---|
41 | int n = mrb_get_args(mrb, "ii|ii", &ch, &pinno, &min, &max);
|
---|
42 |
|
---|
43 | if (pinno >= 20) {
|
---|
44 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
45 | }
|
---|
46 |
|
---|
47 | /* nã2以ä¸ã®ã¨ãã¯ãminãå¼æ°ã«ç¡ãã®ã§ã */
|
---|
48 | if (n <= 2) {
|
---|
49 | min = 544;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* nã3以ä¸ã®ã¨ãã¯ãmaxãå¼æ°ã«ç¡ãã®ã§ã */
|
---|
53 | if (n <= 3) {
|
---|
54 | max = 2400;
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
58 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (servo[ch] != 0) {
|
---|
62 | pwmout_free(&servo[ch]->pwmout);
|
---|
63 | /*servo[ch] = 0;*/
|
---|
64 | }
|
---|
65 |
|
---|
66 | pin = get_pin(pintype_pwmout, pinno);
|
---|
67 | if (pin == NULL)
|
---|
68 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
69 |
|
---|
70 | servo[ch] = pin;
|
---|
71 | servo[ch]->min = min;
|
---|
72 | servo[ch]->max = max;
|
---|
73 |
|
---|
74 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
75 | }
|
---|
76 |
|
---|
77 | /******************************************************/
|
---|
78 | /* ãµã¼ãã®è§åº¦ãã»ãããã¾ã: Servo.write*/
|
---|
79 | /* Servo.write(ch,angle)*/
|
---|
80 | /* ch: ãµã¼ãã®ãã£ãã« 0ï½11ã¾ã§æå®ã§ãã¾ã*/
|
---|
81 | /* angle: è§åº¦ 0ï½180*/
|
---|
82 | /******************************************************/
|
---|
83 | mrb_value mrb_servo_write(mrb_state *mrb, mrb_value self)
|
---|
84 | {
|
---|
85 | int ch;
|
---|
86 | int angle;
|
---|
87 | int min;
|
---|
88 | int max;
|
---|
89 |
|
---|
90 | mrb_get_args(mrb, "ii", &ch, &angle);
|
---|
91 |
|
---|
92 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
93 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (servo[ch] == NULL) {
|
---|
97 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
98 | }
|
---|
99 |
|
---|
100 | DEBUG_PRINT("Servo", angle);
|
---|
101 |
|
---|
102 | min = servo[ch]->min;
|
---|
103 | max = servo[ch]->max;
|
---|
104 | angle *= servo[ch]->period;
|
---|
105 |
|
---|
106 | pwmout_pulsewidth_us(&servo[ch]->pwmout, (angle * (max - min)) / 180 + min);
|
---|
107 |
|
---|
108 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
109 | }
|
---|
110 |
|
---|
111 | /******************************************************/
|
---|
112 | /* ãµã¼ãã¢ã¼ã¿ã«usåä½ã§è§åº¦ãæå®ãã: Servo.us*/
|
---|
113 | /* Servo.us(ch,us)*/
|
---|
114 | /* ch: ãµã¼ãã®ãã£ãã« 0ï½11ã¾ã§æå®ã§ãã¾ã*/
|
---|
115 | /* us: åºåããããã«ã¹ã®å¹
|
---|
116 | 1ï½19999, 0ã§åºå OFF*/
|
---|
117 | /* ãµã¼ãã¢ã¼ã¿ã«ä¸ãããããã«ã¹ã¯20mså¨æã§ã1å¨æä¸ã®Highã®æéãç´æ¥æå®ããã*/
|
---|
118 | /* å®è³ªçã«PWMåºåãé£ç¶å転ã¿ã¤ãã®ãµã¼ãã§ã¯ãå転ã®ã¹ãã¼ããè¨å®ãããã¨ãã§ããã*/
|
---|
119 | /******************************************************/
|
---|
120 | mrb_value mrb_servo_us(mrb_state *mrb, mrb_value self)
|
---|
121 | {
|
---|
122 | int ch;
|
---|
123 | int us;
|
---|
124 |
|
---|
125 | mrb_get_args(mrb, "ii", &ch, &us);
|
---|
126 |
|
---|
127 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
128 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (servo[ch] == 0) {
|
---|
132 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
133 | }
|
---|
134 |
|
---|
135 | pwmout_pulsewidth_us(&servo[ch]->pwmout, us);
|
---|
136 |
|
---|
137 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
138 | }
|
---|
139 |
|
---|
140 | /******************************************************/
|
---|
141 | /* æå¾ã«è¨å®ãããè§åº¦ãèªã¿åºã: Servo.read*/
|
---|
142 | /* Servo.read(ch)*/
|
---|
143 | /* ch: ãµã¼ãã®ãã£ãã« 0ï½11ã¾ã§æå®ã§ãã¾ã*/
|
---|
144 | /* æ»ãå¤*/
|
---|
145 | /* ãã¤ã¯ãç§åä½ us(ch) ã§ä¸ããå¤ã¯èªã¿ã¨ããªã*/
|
---|
146 | /******************************************************/
|
---|
147 | mrb_value mrb_servo_read(mrb_state *mrb, mrb_value self)
|
---|
148 | {
|
---|
149 | int ch;
|
---|
150 | int ret = 0;
|
---|
151 |
|
---|
152 | mrb_get_args(mrb, "i", &ch);
|
---|
153 |
|
---|
154 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
155 | return mrb_fixnum_value(ret);
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (servo[ch] == 0) {
|
---|
159 | return mrb_fixnum_value(ret);
|
---|
160 | }
|
---|
161 |
|
---|
162 | ret = servo[ch]->pulsewidth;
|
---|
163 |
|
---|
164 | return mrb_fixnum_value(ret);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /******************************************************/
|
---|
168 | /* ãã³ã«ãµã¼ããå²ãå½ã¦ããã¦ãããã確èªãã: Servo.attached*/
|
---|
169 | /* Servo.attached(ch)*/
|
---|
170 | /* ch: ãµã¼ãã®ãã£ãã« 0ï½11ã¾ã§æå®ã§ãã¾ã*/
|
---|
171 | /* æ»ãå¤*/
|
---|
172 | /* 1: å²ãå½ã¦ããã¦ãã*/
|
---|
173 | /* 0: å²ãå½ã¦ã¯ãªã*/
|
---|
174 | /******************************************************/
|
---|
175 | mrb_value mrb_servo_attached(mrb_state *mrb, mrb_value self)
|
---|
176 | {
|
---|
177 | int ch;
|
---|
178 | int ret = 0;
|
---|
179 |
|
---|
180 | mrb_get_args(mrb, "i", &ch);
|
---|
181 |
|
---|
182 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
183 | return mrb_fixnum_value(ret);
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (servo[ch] == 0) {
|
---|
187 | return mrb_fixnum_value(ret);
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (get_pin_type(servo[ch]->pin) == pintype_pwmout) {
|
---|
191 | ret = 1;
|
---|
192 | }
|
---|
193 |
|
---|
194 | return mrb_fixnum_value(ret);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /******************************************************/
|
---|
198 | /* ãµã¼ãã®åä½ãæ¢ããå²ãè¾¼ã¿ãç¦æ¢ãã: Servo.detach*/
|
---|
199 | /* Servo.detach(ch)*/
|
---|
200 | /* ch: ãµã¼ãã®ãã£ãã« 0ï½9ã¾ã§æå®ã§ãã¾ã*/
|
---|
201 | /******************************************************/
|
---|
202 | mrb_value mrb_servo_detach(mrb_state *mrb, mrb_value self)
|
---|
203 | {
|
---|
204 | int ch;
|
---|
205 |
|
---|
206 | mrb_get_args(mrb, "i", &ch);
|
---|
207 |
|
---|
208 | if (ch < 0 || ch >= ATTACH_MAX) {
|
---|
209 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (servo[ch] == NULL) {
|
---|
213 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
214 | }
|
---|
215 |
|
---|
216 | pwmout_free(&servo[ch]->pwmout);
|
---|
217 | servo[ch] = 0;
|
---|
218 |
|
---|
219 | return mrb_nil_value(); /*æ»ãå¤ã¯ç¡ãã§ããã*/
|
---|
220 | }
|
---|
221 |
|
---|
222 | /******************************************************/
|
---|
223 | /* ã©ã¤ãã©ãªãå®ç¾©ãã¾ã*/
|
---|
224 | /******************************************************/
|
---|
225 | void servo_Init(mrb_state *mrb)
|
---|
226 | {
|
---|
227 | for (int i = 0; i < ATTACH_MAX; i++) {
|
---|
228 | servo[i] = 0;
|
---|
229 | }
|
---|
230 |
|
---|
231 | struct RClass *servoModule = mrb_define_module(mrb, "Servo");
|
---|
232 |
|
---|
233 | mrb_define_module_function(mrb, servoModule, "attach", mrb_servo_attach, MRB_ARGS_REQ(2) | MRB_ARGS_OPT(2));
|
---|
234 | mrb_define_module_function(mrb, servoModule, "write", mrb_servo_write, MRB_ARGS_REQ(2));
|
---|
235 | mrb_define_module_function(mrb, servoModule, "us", mrb_servo_us, MRB_ARGS_REQ(2));
|
---|
236 | mrb_define_module_function(mrb, servoModule, "read", mrb_servo_read, MRB_ARGS_REQ(1));
|
---|
237 | mrb_define_module_function(mrb, servoModule, "attached", mrb_servo_attached, MRB_ARGS_REQ(1));
|
---|
238 | mrb_define_module_function(mrb, servoModule, "detach", mrb_servo_detach, MRB_ARGS_REQ(1));
|
---|
239 | }
|
---|