aanon4 / BlueBasic

BASIC interpreter for CC2540 and CC2541 Bluetooth LE chips
https://gitlab.com/timwilkinson/BlueBasic
97 stars 55 forks source link

How to create characteristics with uint8 value respond #28

Open 0xFACE opened 8 years ago

0xFACE commented 8 years ago

BlueBasic is using 32-bit variables. When I create characteristic for reading, the response value is 32bit long. Is it possible to respond with uint8 values ?

According to the specification: alert level characteristic is uint8: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.alert_level.xml&u=org.bluetooth.characteristic.alert_level.xml

32bit response gives invalid value :

screenshot_20160813-105457

kscheff commented 7 years ago

As far as I can see, it is always fixed to 4 bytes, big endian. When using a array variable you can have different number of bytes, e.g. use DIM A(1) for a 1 byte characteristic.

0xFACE commented 7 years ago

I tried to use arrays: GATT READ DIM A(1) ONREAD GOSUB 300

But can't run the code, it exits with Error Syntax seems to be incorrect.

kscheff commented 7 years ago

@0xFACE try to using the below code. Your syntax is indeed wrong, you cannot use DIM A(1) inside the GATT command. DIM A(1) must be outside.

A running idle loop keeps the DIM A(1) alive. If the basic program terminates, it falls back to Int32 type and you get the 4 bytes by reading. You can also not declare DIM A(1) in the GOSUB area, since this would hide the instance variable which gets accessed by GATT.

Another thing I observed is, that the DIM variable gets lost for GATT when you use a DELAY instruction during GATT access in the GOSUB section. Sometimes an "DELAY 1" is required in interactive sessions, to give control back to the BLE stack in order not to loose the console connection. But this is only the case when you execute longer tasks in the BASIC program. For GATT access I try to avoid this, by e.g. using a TIMER to shift some processing outside the GATT access (see e.g. line 400 below).

-Kai

NEW
10 DIM A(1)

// setup advertsing
20 ADVERT GENERAL 
30 ADVERT "105a84de-40bd-428b-bf06-698e5e422cd9"
40 ADVERT END 
50 SCAN NAME "oneByteDemo"
60 SCAN END

// setup GATT profile

100 GATT SERVICE "105a84de-40bd-428b-bf06-698e5e422cd9" ONCONNECT GOSUB 400
110 GATT CHARACTERISTIC "00d8a2c6-65f3-40c2-a342-b4e5126f45f0" "sec"
120 GATT READ WRITE A ONREAD GOSUB 300
130 GATT END 

150 TIMER 0, 1000 REPEAT GOSUB 1000

// Idle loop keeping DIM A(1) alive
200 DELAY 10000
210 GOTO 200

// GATT read A
300 A(0) = T % 60
310 RETURN

// GATT Service on connect
400 TIMER 1, 2000 GOSUB 500
410 RETURN 

// welcome
500 PRINT "Hello World!"
510 RETURN 

// time tick
1000 T = T + 1
1010 RETURN

// this last return seems to be required
65529 RETURN

AUTORUN ON
END
REBOOT

oneByte.bbasic.zip

0xFACE commented 7 years ago

Wow, great. Idle loop did the trick. Thanks a lot ;-)