Skip to content

Commit c05ffef

Browse files
committed
IOIO: implements veml6030 light sensor sample
1 parent dca593c commit c05ffef

File tree

3 files changed

+107
-42
lines changed

3 files changed

+107
-42
lines changed

ioio/main.cpp

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,6 @@ struct IOTask {
133133
return result;
134134
}
135135

136-
// int foo(int)
137-
int invokeIntInt(const char *name, int value, var_s *retval) {
138-
int result = 0;
139-
if (_instance != nullptr) {
140-
jmethodID method = env->GetMethodID(_clazz, name, "(I)I");
141-
var_num_t value = 0;
142-
if (method != nullptr) {
143-
value = env->CallIntMethod(_instance, method, value);
144-
}
145-
if (!checkException(retval)) {
146-
v_setint(retval, value);
147-
result = 1;
148-
}
149-
}
150-
return result;
151-
}
152-
153136
// void foo(boolean)
154137
int invokeVoidBool(const char *name, int value, var_s *retval) {
155138
int result = 0;
@@ -238,6 +221,61 @@ struct IOTask {
238221
return result;
239222
}
240223

224+
// int readWrite(int address, byte[] write) {
225+
int invokeReadWrite(int argc, slib_par_t *arg, var_s *retval) {
226+
int result = 0;
227+
if (_instance != nullptr) {
228+
jmethodID method = env->GetMethodID(_clazz, "readWrite", "(I[B)I");
229+
var_num_t value = 0;
230+
if (method != nullptr) {
231+
jbyteArray array = env->NewByteArray(argc - 1);
232+
jbyte *elements = env->GetByteArrayElements(array, nullptr);
233+
populateByteArray(argc, arg, elements);
234+
auto address = get_param_int(argc, arg, 0, 0);
235+
236+
value = env->CallIntMethod(_instance, method, address, array);
237+
releaseArray(array, elements);
238+
}
239+
if (!checkException(retval)) {
240+
v_setint(retval, value);
241+
result = 1;
242+
}
243+
}
244+
return result;
245+
}
246+
247+
// int readWrite(int address, byte[] write) {
248+
int invokeWrite(int argc, slib_par_t *arg, var_s *retval) {
249+
int result = 0;
250+
if (_instance != nullptr) {
251+
jmethodID method = env->GetMethodID(_clazz, "write", "(I[B)V");
252+
if (method != nullptr) {
253+
jbyteArray array = env->NewByteArray(argc - 1);
254+
jbyte *elements = env->GetByteArrayElements(array, nullptr);
255+
populateByteArray(argc, arg, elements);
256+
auto address = get_param_int(argc, arg, 0, 0);
257+
258+
env->CallVoidMethod(_instance, method, address, array);
259+
releaseArray(array, elements);
260+
}
261+
if (!checkException(retval)) {
262+
result = 1;
263+
}
264+
}
265+
return result;
266+
}
267+
268+
void populateByteArray(int argc, slib_par_t *arg, jbyte *elements) {
269+
for (int i = 1; i < argc; i++) {
270+
elements[i] = get_param_int(argc, arg, i, 0);
271+
}
272+
}
273+
274+
void releaseArray(jbyteArray array, jbyte *elements) {
275+
env->ReleaseByteArrayElements(array, elements, 0);
276+
env->DeleteLocalRef(array);
277+
}
278+
241279
int open(int pin, var_s *retval) {
242280
return invokeVoidInt("open", pin, retval);
243281
}
@@ -271,30 +309,27 @@ static int get_io_class_id(var_s *map, var_s *retval) {
271309
return result;
272310
}
273311

274-
static int cmd_twimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
312+
static int cmd_twimaster_readwrite(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
275313
int result = 0;
276-
if (argc != 2) {
277-
error(retval, "TwiMaster.write", 2);
314+
if (argc < 2) {
315+
error(retval, "TwiMaster.readWrite", 2, 10);
278316
} else {
279317
int id = get_io_class_id(self, retval);
280318
if (id != -1) {
281-
auto address = get_param_int(argc, arg, 0, 0);
282-
auto data = get_param_int(argc, arg, 1, 0);
283-
result = _ioTaskMap.at(id).invokeVoidInt2("write", address, data, retval);
319+
result = _ioTaskMap.at(id).invokeReadWrite(argc, arg, retval);
284320
}
285321
}
286322
return result;
287323
}
288324

289-
static int cmd_twimaster_read(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
325+
static int cmd_twimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
290326
int result = 0;
291-
if (argc != 1) {
292-
error(retval, "TwiMaster.read", 1);
327+
if (argc < 2) {
328+
error(retval, "TwiMaster.write", 2, 10);
293329
} else {
294330
int id = get_io_class_id(self, retval);
295331
if (id != -1) {
296-
auto address = get_param_int(argc, arg, 0, 0);
297-
result = _ioTaskMap.at(id).invokeIntInt("read", address, retval);
332+
result = _ioTaskMap.at(id).invokeWrite(argc, arg, retval);
298333
}
299334
}
300335
return result;
@@ -317,7 +352,7 @@ static int cmd_spimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *re
317352

318353
static void create_twimaster(var_t *map) {
319354
v_create_callback(map, "write", cmd_twimaster_write);
320-
v_create_callback(map, "read", cmd_twimaster_read);
355+
v_create_callback(map, "readWrite", cmd_twimaster_readwrite);
321356
}
322357

323358
static void create_spimaster(var_t *map) {

ioio/samples/veml6030.bas

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
rem
2+
rem https://learn.sparkfun.com/tutorials/qwiic-ambient-light-sensor-veml6030-hookup-guide/all
3+
rem https://piico.dev/p3
4+
rem PiicoDev Ambient Light Sensor VEML6030
5+
rem
6+
7+
import ioio
8+
9+
rem i2c address
10+
const address = 0x10
11+
12+
rem register where the light sensing data is stored
13+
const alsDataReg = 0x04
14+
15+
rem ambient light sensing configuration register
16+
const alsConfReg = 0
17+
18+
rem default settings
19+
rem initialise gain:1x, integration 100ms, persistence 1, disable interrupt
20+
const alsConf = 0
21+
22+
p3 = ioio.openTwiMaster(1, 0)
23+
24+
ioio.waitForConnect(10)
25+
26+
rem configure default settings
27+
p3.write(address, alsConfReg, alsConf)
28+
29+
while 1
30+
print p3.readWrite(address, alsDataReg)
31+
delay 2000
32+
wend

ioio/src/main/java/net/sourceforge/smallbasic/ioio/TwiMasterImpl.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.sourceforge.smallbasic.ioio;
22

33
import java.io.IOException;
4-
import java.util.concurrent.atomic.AtomicInteger;
54

65
import ioio.lib.api.IOIO;
76
import ioio.lib.api.TwiMaster;
@@ -34,23 +33,22 @@ public void open(int twiNum, int smbus) throws IOException {
3433
this.smbus = (smbus == 1);
3534
}
3635

37-
public int read(int address) {
36+
public int readWrite(int address, byte[] write) {
3837
handleError();
39-
AtomicInteger atomicLong = new AtomicInteger();
40-
lock.invoke((i) -> {
41-
byte[] buffer = new byte[4];
42-
twiMaster.writeRead(address, false, null, 0, buffer, 4);
43-
int value = 0; // TODO read buffer into value
44-
atomicLong.set(value);
38+
return lock.invokeInt((i) -> {
39+
byte[] read = new byte[4];
40+
twiMaster.writeRead(address, false, write, write.length, read, read.length);
41+
Log.i(TAG, "read = " + read.length + " " + address + " "
42+
+ read[0] + " " + read[1] + " " + read[2] + " " + read[3]);
43+
int value = 0; // TODO read read into value
44+
return value;
4545
});
46-
return atomicLong.get();
4746
}
4847

49-
public void write(int address, int data) {
48+
public void write(int address, byte[] write) {
5049
handleError();
5150
lock.invoke((i) -> {
52-
byte[] buffer = {(byte) data};
53-
twiMaster.writeRead(address, false, buffer, buffer.length, null, 0);
51+
twiMaster.writeRead(address, false, write, write.length, null, 0);
5452
});
5553
}
5654

@@ -61,7 +59,7 @@ void loop() throws ConnectionLostException, InterruptedException {
6159

6260
@Override
6361
void setup(IOIO ioio) throws ConnectionLostException {
64-
Log.i(TAG, "setup entered");
62+
Log.i(TAG, "setup entered: " + twiNum);
6563
twiMaster = ioio.openTwiMaster(twiNum, rate, smbus);
6664
}
6765
}

0 commit comments

Comments
 (0)