Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add firmata ble support using littleb library #604

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Merge remote-tracking branch 'upstream/master'
fixed littleb api issues

Shiran Ben-Melech <shiran.ben-melech@intel.com>
shiranbm committed Nov 3, 2016
commit 75bb92e2ed2f00ef6e27e439fc8a23ca29c669dd
35 changes: 19 additions & 16 deletions api/mraa/types.h
Original file line number Diff line number Diff line change
@@ -37,29 +37,32 @@ extern "C" {
* MRAA supported platform types
*/
typedef enum {
MRAA_INTEL_GALILEO_GEN1 = 0, /**< The Generation 1 Galileo platform (RevD) */
MRAA_INTEL_GALILEO_GEN2 = 1, /**< The Generation 2 Galileo platform (RevG/H) */
MRAA_INTEL_EDISON_FAB_C = 2, /**< The Intel Edison (FAB C) */
MRAA_INTEL_DE3815 = 3, /**< The Intel DE3815 Baytrail NUC */
MRAA_INTEL_MINNOWBOARD_MAX = 4, /**< The Intel Minnow Board Max */
MRAA_RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */
MRAA_BEAGLEBONE = 6, /**< The different BeagleBone Black Modes B/C */
MRAA_BANANA = 7, /**< Allwinner A20 based Banana Pi and Banana Pro */
MRAA_INTEL_NUC5 = 8, /**< The Intel 5th generations Broadwell NUCs */
MRAA_96BOARDS = 9, /**< Linaro 96boards */
MRAA_INTEL_SOFIA_3GR = 10, /**< The Intel SoFIA 3GR */
MRAA_INTEL_CHERRYHILLS = 11, /**< The Intel Braswell Cherryhills */
MRAA_UP = 12, /**< The UP Board */
MRAA_INTEL_GALILEO_GEN1 = 0, /**< The Generation 1 Galileo platform (RevD) */
MRAA_INTEL_GALILEO_GEN2 = 1, /**< The Generation 2 Galileo platform (RevG/H) */
MRAA_INTEL_EDISON_FAB_C = 2, /**< The Intel Edison (FAB C) */
MRAA_INTEL_DE3815 = 3, /**< The Intel DE3815 Baytrail NUC */
MRAA_INTEL_MINNOWBOARD_MAX = 4, /**< The Intel Minnow Board Max */
MRAA_RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */
MRAA_BEAGLEBONE = 6, /**< The different BeagleBone Black Modes B/C */
MRAA_BANANA = 7, /**< Allwinner A20 based Banana Pi and Banana Pro */
MRAA_INTEL_NUC5 = 8, /**< The Intel 5th generations Broadwell NUCs */
MRAA_96BOARDS = 9, /**< Linaro 96boards */
MRAA_INTEL_SOFIA_3GR = 10, /**< The Intel SoFIA 3GR */
MRAA_INTEL_CHERRYHILLS = 11, /**< The Intel Braswell Cherryhills */
MRAA_UP = 12, /**< The UP Board */
MRAA_INTEL_GT_TUCHUCK = 13, /**< The Intel GT Tuchuck Board */

// USB platform extenders start at 256
MRAA_FTDI_FT4222 = 256, /**< FTDI FT4222 USB to i2c bridge */
MRAA_FTDI_FT4222 = 256, /**< FTDI FT4222 USB to i2c bridge */

// contains bit 9 so is subplatform
MRAA_GENERIC_FIRMATA = 1280, /**< Firmata uart platform/bridge */
MRAA_GENERIC_FIRMATA = 1280, /**< Firmata uart platform/bridge */
MRAA_BLE_FIRMATA_BY_NAME = 1281, /**< Firmata ble platform by name */
MRAA_BLE_FIRMATA_BY_ADDRESS = 1282, /**< Firmata ble platform by address*/

MRAA_NULL_PLATFORM = 98, /**< Platform with no capabilities that hosts a sub platform */
MRAA_MOCK_PLATFORM = 96, /**< Mock platform, which requires no real hardware */
MRAA_JSON_PLATFORM = 97, /**< User initialised platform from json*/
MRAA_NULL_PLATFORM = 98, /**< Platform with no capabilities that hosts a sub platform */
MRAA_UNKNOWN_PLATFORM =
99 /**< An unknown platform type, typically will load INTEL_GALILEO_GEN1 */
} mraa_platform_t;
35 changes: 18 additions & 17 deletions api/mraa/types.hpp
Original file line number Diff line number Diff line change
@@ -38,26 +38,27 @@ namespace mraa
* MRAA supported platform types
*/
typedef enum {
INTEL_GALILEO_GEN1 = 0, /**< The Generation 1 Galileo platform (RevD) */
INTEL_GALILEO_GEN2 = 1, /**< The Generation 2 Galileo platform (RevG/H) */
INTEL_EDISON_FAB_C = 2, /**< The Intel Edison (FAB C) */
INTEL_DE3815 = 3, /**< The Intel DE3815 Baytrail NUC */
INTEL_MINNOWBOARD_MAX = 4, /**< The Intel Minnow Board Max */
RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */
BEAGLEBONE = 6, /**< The different BeagleBone Black Modes B/C */
BANANA = 7, /**< Allwinner A20 based Banana Pi and Banana Pro */
INTEL_NUC5 = 8, /**< The Intel 5th generations Broadwell NUCs */
A96BOARDS = 9, /**< Linaro 96boards, A prefix for 'ARM' since not allowed numerical */
INTEL_SOFIA_3GR = 10, /**< The Intel SoFIA 3GR */
INTEL_CHERRYHILLS = 11, /**< The Intel Braswell Cherryhills */

FTDI_FT4222 = 256, /**< FTDI FT4222 USB to i2c bridge */

GENERIC_FIRMATA = 1280, /**< Firmata uart platform/bridge */
INTEL_GALILEO_GEN1 = 0, /**< The Generation 1 Galileo platform (RevD) */
INTEL_GALILEO_GEN2 = 1, /**< The Generation 2 Galileo platform (RevG/H) */
INTEL_EDISON_FAB_C = 2, /**< The Intel Edison (FAB C) */
INTEL_DE3815 = 3, /**< The Intel DE3815 Baytrail NUC */
INTEL_MINNOWBOARD_MAX = 4, /**< The Intel Minnow Board Max */
RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */
BEAGLEBONE = 6, /**< The different BeagleBone Black Modes B/C */
BANANA = 7, /**< Allwinner A20 based Banana Pi and Banana Pro */
INTEL_NUC5 = 8, /**< The Intel 5th generations Broadwell NUCs */
A96BOARDS = 9, /**< Linaro 96boards, A prefix for 'ARM' since not allowed numerical */
INTEL_SOFIA_3GR = 10, /**< The Intel SoFIA 3GR */
INTEL_CHERRYHILLS = 11, /**< The Intel Braswell Cherryhills */
INTEL_UP = 12, /**< The UP Board */
INTEL_GT_TUCHUCK = 13, /**< The Intel GT Board */

FTDI_FT4222 = 256, /**< FTDI FT4222 USB to i2c bridge */

GENERIC_FIRMATA = 1280, /**< Firmata uart platform/bridge */
BLE_FIRMATA_BY_NAME = 1281, /**< Firmata ble platform by name */
BLE_FIRMATA_BY_ADDRESS = 1282, /**< Firmata ble platform by address */


NULL_PLATFORM = 98,
UNKNOWN_PLATFORM =
99 /**< An unknown platform type, typically will load INTEL_GALILEO_GEN1 */
38 changes: 38 additions & 0 deletions examples/python/aio_ble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python

# Author: Brendan Le Foll <brendan.le.foll@intel.com>
# Copyright (c) 2014 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import mraa
import time

print (mraa.getVersion())
mraa.addSubplatform(mraa.BLE_FIRMATA_BY_NAME, "FIRMATA")

try:
x = mraa.Aio(0+512)
for i in range(50):
print (x.read())
print ("%.5f" % x.readFloat())
time.sleep(1)
except:
print ("Are you sure you have an ADC?")
2 changes: 1 addition & 1 deletion examples/python/hello_gpio_ble.py
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@

print (mraa.getVersion())
mraa.addSubplatform(mraa.BLE_FIRMATA_BY_NAME, "FIRMATA")
x = mraa.Gpio(3 + 512)
x = mraa.Gpio(13 + 512)
x.dir(mraa.DIR_OUT)
print "Blinking"
for i in range(5):
1 change: 0 additions & 1 deletion include/firmata/firmata.h
Original file line number Diff line number Diff line change
@@ -96,7 +96,6 @@ typedef struct s_pin {
typedef struct s_firmata {
mraa_uart_context uart;
#ifdef FIRMATABLE
lb_context lb_ctx;
lb_bl_device* bl_dev;
#endif
t_pin pins[128];
36 changes: 18 additions & 18 deletions include/firmata/firmata_ble.h
Original file line number Diff line number Diff line change
@@ -35,24 +35,24 @@ void* liblittleb_lib;

lb_result_t (*dl_lb_init)();
lb_result_t (*dl_lb_destroy)();
lb_context (*dl_lb_context_new)();
lb_result_t (*dl_lb_context_free)(lb_context);
lb_result_t (*dl_lb_get_bl_devices)(lb_context, int);
lb_result_t (*dl_lb_connect_device)(lb_context, lb_bl_device*);
lb_result_t (*dl_lb_disconnect_device)(lb_context, lb_bl_device*);
lb_result_t (*dl_lb_pair_device)(lb_context, lb_bl_device*);
lb_result_t (*dl_lb_unpair_device)(lb_context, lb_bl_device*);
lb_result_t (*dl_lb_get_ble_characteristic_by_characteristic_path)(lb_context, lb_bl_device*, const char*, lb_ble_char**);
lb_result_t (*dl_lb_get_ble_characteristic_by_uuid)(lb_context, lb_bl_device*, const char*, lb_ble_char**);
lb_result_t (*dl_lb_get_ble_service_by_service_path)(lb_context lb_ctx, lb_bl_device*, const char*, lb_ble_service**);
lb_result_t (*dl_lb_get_ble_service_by_uuid)(lb_context, lb_bl_device*, const char*, lb_ble_service**);
lb_result_t (*dl_lb_get_ble_device_services)(lb_context, lb_bl_device*);
lb_result_t (*dl_lb_get_device_by_device_path)(lb_context, const char*, lb_bl_device**);
lb_result_t (*dl_lb_get_device_by_device_name)(lb_context, const char*, lb_bl_device**);
lb_result_t (*dl_lb_get_device_by_device_address)(lb_context, const char*, lb_bl_device**);
lb_result_t (*dl_lb_write_to_characteristic)(lb_context, lb_bl_device*, const char*, int, uint8_t*);
lb_result_t (*dl_lb_read_from_characteristic)(lb_context, lb_bl_device*, const char*, size_t*, uint8_t**);
lb_result_t (*dl_lb_register_characteristic_read_event)(lb_context,lb_bl_device*, const char*, sd_bus_message_handler_t, void*);
lb_result_t (*dl_lb_context_new)();
lb_result_t (*dl_lb_context_free)();
lb_result_t (*dl_lb_get_bl_devices)(int);
lb_result_t (*dl_lb_connect_device)(lb_bl_device*);
lb_result_t (*dl_lb_disconnect_device)(lb_bl_device*);
lb_result_t (*dl_lb_pair_device)(lb_bl_device*);
lb_result_t (*dl_lb_unpair_device)(lb_bl_device*);
lb_result_t (*dl_lb_get_ble_characteristic_by_characteristic_path)(lb_bl_device*, const char*, lb_ble_char**);
lb_result_t (*dl_lb_get_ble_characteristic_by_uuid)(lb_bl_device*, const char*, lb_ble_char**);
lb_result_t (*dl_lb_get_ble_service_by_service_path)(lb_bl_device*, const char*, lb_ble_service**);
lb_result_t (*dl_lb_get_ble_service_by_uuid)(lb_bl_device*, const char*, lb_ble_service**);
lb_result_t (*dl_lb_get_ble_device_services)(lb_bl_device*);
lb_result_t (*dl_lb_get_device_by_device_path)(const char*, lb_bl_device**);
lb_result_t (*dl_lb_get_device_by_device_name)(const char*, lb_bl_device**);
lb_result_t (*dl_lb_get_device_by_device_address)(const char*, lb_bl_device**);
lb_result_t (*dl_lb_write_to_characteristic)(lb_bl_device*, const char*, int, uint8_t*);
lb_result_t (*dl_lb_read_from_characteristic)(lb_bl_device*, const char*, size_t*, uint8_t**);
lb_result_t (*dl_lb_register_characteristic_read_event)(lb_bl_device*, const char*, sd_bus_message_handler_t, void*);
lb_result_t (*dl_lb_parse_uart_service_message)(sd_bus_message*, const void**, size_t*);

mraa_result_t mraa_firmata_ble_init();
45 changes: 23 additions & 22 deletions src/firmata/firmata.c
Original file line number Diff line number Diff line change
@@ -58,8 +58,8 @@ firmata_write_internal(t_firmata* firmata_dev, const char* buf, size_t len)
mraa_uart_write(firmata_dev->uart, buf, len);
}
#ifdef FIRMATABLE
else if (firmata_dev->lb_ctx != NULL) {
r = dl_lb_write_to_characteristic(firmata_dev->lb_ctx, firmata_dev->bl_dev,
else if (firmata_dev->bl_dev != NULL) {
r = dl_lb_write_to_characteristic(firmata_dev->bl_dev,
"6e400002-b5a3-f393-e0a9-e50e24dcca9e", len, (uint8_t*) buf);
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_write_to_characteristic\n");
@@ -118,6 +118,14 @@ firmata_ble_new(const char* name, mraa_platform_t type)
return NULL;
}

int ret = pthread_spin_init(&res->lock, PTHREAD_PROCESS_SHARED);
if (ret != 0) {
syslog(LOG_ERR, "firmata; could not init locking");
free(res);
return NULL;
}


res->bl_dev = NULL;

ble_res = mraa_firmata_ble_init();
@@ -134,14 +142,7 @@ firmata_ble_new(const char* name, mraa_platform_t type)
return NULL;
}

res->lb_ctx = dl_lb_context_new();
if (res->lb_ctx == NULL) {
syslog(LOG_ERR, "ERROR: lb_context_new\n");
free(res);
return NULL;
}

ble_res = dl_lb_get_bl_devices(res->lb_ctx, 5);
ble_res = dl_lb_get_bl_devices(5);
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_get_bl_devices\n");
firmata_ble_close(res);
@@ -150,9 +151,9 @@ firmata_ble_new(const char* name, mraa_platform_t type)
}

if (type == MRAA_BLE_FIRMATA_BY_NAME) {
ble_res = dl_lb_get_device_by_device_name(res->lb_ctx, name, &res->bl_dev);
ble_res = dl_lb_get_device_by_device_name(name, &res->bl_dev);
} else if (type == MRAA_BLE_FIRMATA_BY_ADDRESS) {
ble_res = dl_lb_get_device_by_device_address(res->lb_ctx, name, &res->bl_dev);
ble_res = dl_lb_get_device_by_device_address(name, &res->bl_dev);
}
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: Device FIRMATA not found\n");
@@ -161,7 +162,7 @@ firmata_ble_new(const char* name, mraa_platform_t type)
return NULL;
}

ble_res = dl_lb_connect_device(res->lb_ctx, res->bl_dev);
ble_res = dl_lb_connect_device(res->bl_dev);
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_connect_device\n");
firmata_ble_close(res);
@@ -176,15 +177,15 @@ firmata_ble_new(const char* name, mraa_platform_t type)
// return NULL;
//}

ble_res = dl_lb_get_ble_device_services(res->lb_ctx, res->bl_dev);
ble_res = dl_lb_get_ble_device_services(res->bl_dev);
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_get_ble_device_services\n");
firmata_ble_close(res);
free(res);
return NULL;
}

ble_res = dl_lb_register_characteristic_read_event(res->lb_ctx, res->bl_dev,
ble_res = dl_lb_register_characteristic_read_event(res->bl_dev,
"6e400003-b5a3-f393-e0a9-e50e24dcca9e",
firmata_uart_read_handler, res);
if (ble_res < 0) {
@@ -213,12 +214,12 @@ firmata_ble_close(t_firmata* firmata)
// exit(r);
//}

r = dl_lb_disconnect_device(firmata->lb_ctx, firmata->bl_dev);
r = dl_lb_disconnect_device(firmata->bl_dev);
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_disconnect_device\n");
}

r = dl_lb_context_free(firmata->lb_ctx);
r = dl_lb_context_free();
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_context_free\n");
}
@@ -410,16 +411,16 @@ firmata_endParse(t_firmata* firmata)
firmata->pins[pin].value |= (firmata->parse_buff[5] << 7);
if (firmata->parse_count > 7)
firmata->pins[pin].value |= (firmata->parse_buff[6] << 14);
// disable this to check the firmata_devs responses
// disable this to check the firmata_devs responses
} else if (firmata->parse_buff[1] == FIRMATA_I2C_REPLY) {
int addr = (firmata->parse_buff[2] & 0x7f) | ((firmata->parse_buff[3] & 0x7f) << 7);
int reg = (firmata->parse_buff[4] & 0x7f) | ((firmata->parse_buff[5] & 0x7f) << 7);
int i = 6;
int ii = 0;
for (ii; ii < (firmata->parse_count - 7) / 2; ii++) {
firmata->i2cmsg[addr][reg + ii] =
(firmata->parse_buff[i] & 0x7f) | ((firmata->parse_buff[i + 1] & 0x7f) << 7);
i = i + 2;
if (pthread_spin_lock(&firmata->lock) != 0) syslog(LOG_ERR, "firmata: Fatal spinlock deadlock, skipping i2c msg");
for (; ii < (firmata->parse_count - 7) / 2; ii++) {
firmata->i2cmsg[addr][reg+ii] = (firmata->parse_buff[i] & 0x7f) | ((firmata->parse_buff[i+1] & 0x7f) << 7);
i = i+2;
}
if (pthread_spin_unlock(&firmata->lock) != 0) syslog(LOG_ERR, "firmata: Fatal spinlock deadlock");
} else {
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.