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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ option (BUILDSWIGNODE "Build swig node modules." ON)
option (BUILDSWIGJAVA "Build Java API." OFF)
option (USBPLAT "Detection USB platform." OFF)
option (FIRMATA "Add Firmata support to mraa." OFF)
option (FIRMATABLE "Add Firmata BLE support to mraa." OFF)
option (ONEWIRE "Add Onewire support to mraa." ON)
option (JSONPLAT "Add Platform loading via a json file." ON)
option (IMRAA "Add Imraa support to mraa." OFF)
Expand Down
2 changes: 2 additions & 0 deletions api/mraa/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ typedef enum {

// contains bit 9 so is subplatform
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_MOCK_PLATFORM = 96, /**< Mock platform, which requires no real hardware */
MRAA_JSON_PLATFORM = 97, /**< User initialised platform from json*/
Expand Down
2 changes: 2 additions & 0 deletions api/mraa/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ typedef enum {
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 =
Expand Down
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 <[email protected]>
# 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?")
37 changes: 37 additions & 0 deletions examples/python/hello_gpio_ble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python

# Author: Thomas Ingleby <[email protected]>
# 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")
x = mraa.Gpio(13 + 512)
x.dir(mraa.DIR_OUT)
print "Blinking"
for i in range(5):
x.write(1)
time.sleep(1)
x.write(0)
time.sleep(1)
41 changes: 41 additions & 0 deletions examples/python/rgblcd_ble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python

# Author: Brendan Le Foll <[email protected]>
# 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

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

# This example will change the LCD backlight on the Grove-LCD RGB backlight
# to a nice shade of purple
x = mraa.I2c(512+0)
x.address(0x62)

# initialise device
x.writeReg(0, 0)
x.writeReg(1, 0)

# sent RGB color data
x.writeReg(0x08, 0xAA)
x.writeReg(0x04, 255)
x.writeReg(0x02, 255)
9 changes: 9 additions & 0 deletions include/firmata/firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <pthread.h>

#include "uart.h"
#ifdef FIRMATABLE
#include "firmata_ble.h"
#endif

#define MODE_INPUT 0x00
#define MODE_OUTPUT 0x01
Expand Down Expand Up @@ -92,6 +95,9 @@ typedef struct s_pin {

typedef struct s_firmata {
mraa_uart_context uart;
#ifdef FIRMATABLE
lb_bl_device* bl_dev;
#endif
t_pin pins[128];
int i2cmsg[256][256];
int parse_command_len;
Expand All @@ -105,13 +111,16 @@ typedef struct s_firmata {
} t_firmata;

t_firmata* firmata_new(const char* name);
t_firmata* firmata_ble_new(const char* name, mraa_platform_t type);
void firmata_initPins(t_firmata* firmata);
int firmata_askFirmware(t_firmata* firmata);
int firmata_pinMode(t_firmata* firmata, int pin, int mode);
int firmata_digitalWrite(t_firmata* firmata, int pin, int value);
int firmata_analogWrite(t_firmata* firmata, int pin, int value);
int firmata_analogRead(t_firmata* firmata, int pin);
int firmata_pull(t_firmata* firmata);
int firmata_write_internal(t_firmata* firmata_dev, const char* buf, size_t len);
void firmata_parse(t_firmata* firmata, const uint8_t* buf, int len);
void firmata_endParse(t_firmata* firmata);
void firmata_close(t_firmata* firmata);
void firmata_ble_close(t_firmata* firmata);
63 changes: 63 additions & 0 deletions include/firmata/firmata_ble.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Author: Shiran Ben-Melech <[email protected]>
* Copyright (c) 2016 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.
*/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <dlfcn.h>
#include "mraa_internal.h"
#include "littleb.h"

void* liblittleb_lib;

lb_result_t (*dl_lb_init)();
lb_result_t (*dl_lb_destroy)();
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();


#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion include/firmata/firmata_mraa.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {

#include "mraa_internal.h"

mraa_platform_t mraa_firmata_platform(mraa_board_t* board, const char* uart_dev);
mraa_platform_t mraa_firmata_platform(mraa_board_t* board, const char* uart_dev, mraa_platform_t type);


#ifdef __cplusplus
Expand Down
6 changes: 5 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ set (mraa_LIB_INCLUDE_DIRS
if (FIRMATA)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFIRMATA=1")
add_subdirectory (firmata)

if (FIRMATABLE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFIRMATABLE=1")
endif()
endif ()

if (ONEWIRE)
Expand Down Expand Up @@ -255,7 +259,7 @@ if (BUILDSWIG)
endif ()

add_library (mraa ${mraa_LIB_SRCS})
target_link_libraries (mraa ${mraa_LIBS})
target_link_libraries (mraa ${mraa_LIBS} dl)
set_target_properties(
mraa
PROPERTIES
Expand Down
12 changes: 10 additions & 2 deletions src/firmata/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
if (FIRMATA)
message (STATUS "INFO - Adding firmata backend support")
set (mraa_LIB_SRCS_NOAUTO ${mraa_LIB_SRCS_NOAUTO}
set (mraa_firmata_LIBS
${PROJECT_SOURCE_DIR}/src/firmata/firmata.c
${PROJECT_SOURCE_DIR}/src/firmata/firmata_mraa.c
PARENT_SCOPE
)
if (FIRMATABLE)
message (STATUS "INFO - Adding firmata ble backend support")
set (mraa_firmata_LIBS ${mraa_firmata_LIBS}
${PROJECT_SOURCE_DIR}/src/firmata/firmata_ble.c
)
endif ()
set (mraa_LIB_SRCS_NOAUTO ${mraa_LIB_SRCS_NOAUTO} ${mraa_firmata_LIBS}
PARENT_SCOPE
)
endif ()
Loading