Compare commits

...

8 Commits

Author SHA1 Message Date
Shiran Ben-Melech
f00938670e changed firmata ble related code to match new littleb api
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
a8caf96fe3 Added back FIRMATABLE option to cmake and sources to avoid compilation errors when littleb is missing revert hello_gpio.py to original example
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
ca0ad5e0a7 Changed all fprintf to syslog fixed some small issues clang-format on changed files
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
224c7a9267 added firmata_ble close to mraa_firmata close
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
cd20de1b6a fixed mraa_firmata_plat_init
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
0b94bb73ee removed debug prints renamed callback removed firmata pull thread when using ble
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
a2a2d86ccd Added all basic functionality needed for connect write and read over ble
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:31:02 +01:00
Shiran Ben-Melech
119164ae6a Added initial changes needed for using littleb in mraa
Signed-off-by: Shiran Ben-Melech <shiran.ben-melech@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-08-02 14:28:51 +01:00
14 changed files with 598 additions and 122 deletions

View File

@@ -70,6 +70,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)

View File

@@ -37,30 +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_GT_TUCHUCK = 13, /**< The Intel GT Tuchuck 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_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_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;

View File

@@ -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 =

View File

@@ -0,0 +1,37 @@
#!/usr/bin/env python
# Author: Thomas Ingleby <thomas.c.ingleby@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")
x = mraa.Gpio(3 + 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)

View File

@@ -0,0 +1,41 @@
#!/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
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)

View File

@@ -25,6 +25,9 @@
#pragma once
#include "uart.h"
#ifdef FIRMATABLE
#include "firmata_ble.h"
#endif
#define MODE_INPUT 0x00
#define MODE_OUTPUT 0x01
@@ -90,6 +93,10 @@ 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];
int i2cmsg[256][256];
int parse_command_len;
@@ -102,6 +109,7 @@ 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);
@@ -109,6 +117,8 @@ 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);

View File

@@ -0,0 +1,63 @@
/*
* Author: Shiran Ben-Melech <shiran.ben-melech@intel.com>
* 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_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_parse_uart_service_message)(sd_bus_message*, const void**, size_t*);
mraa_result_t mraa_firmata_ble_init();
#ifdef __cplusplus
}
#endif

View File

@@ -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

View File

@@ -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)
@@ -251,7 +255,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

View File

@@ -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 ()

View File

@@ -23,12 +23,53 @@
*/
#include "firmata/firmata.h"
#include "firmata/firmata_ble.h"
#include "mraa_internal.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static int
firmata_uart_read_handler(sd_bus_message* message, void* userdata, sd_bus_error* error)
{
int r, i;
size_t size = 0;
uint8_t* result = NULL;
t_firmata* firmata = (t_firmata*) userdata;
r = dl_lb_parse_uart_service_message(message, (const void**) &result, &size);
if (r < 0) {
syslog(LOG_ERR, "ERROR: couldn't parse uart message\n");
return -MRAA_ERROR_UNSPECIFIED;
}
firmata_parse(firmata, result, size);
return LB_SUCCESS;
}
inline int
firmata_write_internal(t_firmata* firmata_dev, const char* buf, size_t len)
{
int r = MRAA_ERROR_UNSPECIFIED;
if (firmata_dev->uart != NULL) {
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,
"6e400002-b5a3-f393-e0a9-e50e24dcca9e", len, (uint8_t*) buf);
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_write_to_characteristic\n");
return -MRAA_ERROR_UNSPECIFIED;
}
}
#endif
return MRAA_SUCCESS;
}
t_firmata*
firmata_new(const char* name)
{
@@ -44,7 +85,7 @@ firmata_new(const char* name)
if (res->uart == NULL) {
syslog(LOG_ERR, "firmata: UART failed to setup");
free(res);
return NULL;
return NULL;
}
firmata_initPins(res);
@@ -59,6 +100,130 @@ firmata_new(const char* name)
return res;
}
#ifdef FIRMATABLE
t_firmata*
firmata_ble_new(const char* name, mraa_platform_t type)
{
t_firmata* res;
int ble_res = LB_ERROR_UNSPECIFIED;
res = calloc(1, sizeof(t_firmata));
if (!res) {
return NULL;
}
res->bl_dev = NULL;
ble_res = mraa_firmata_ble_init();
if (ble_res < 0) {
syslog(LOG_ERR, "firmata: littleb failed to setup");
free(res);
return NULL;
}
ble_res = dl_lb_init();
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_init\n");
free(res);
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);
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_get_bl_devices\n");
firmata_ble_close(res);
free(res);
return NULL;
}
if (type == MRAA_BLE_FIRMATA_BY_NAME) {
ble_res = dl_lb_get_device_by_device_name(res->lb_ctx, 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);
}
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: Device FIRMATA not found\n");
firmata_ble_close(res);
free(res);
return NULL;
}
ble_res = dl_lb_connect_device(res->lb_ctx, res->bl_dev);
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_connect_device\n");
firmata_ble_close(res);
free(res);
return NULL;
}
// ble_res = lb_pair_device(res->lb_ctx, res->bl_dev);
// if (ble_res < 0) {
// syslog(LOG_ERR, "ERROR: lb_pair_device\n");
// free(res);
// return NULL;
//}
ble_res = dl_lb_get_ble_device_services(res->lb_ctx, 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,
"6e400003-b5a3-f393-e0a9-e50e24dcca9e",
firmata_uart_read_handler, res);
if (ble_res < 0) {
syslog(LOG_ERR, "ERROR: lb_register_characteristic_read_event\n");
firmata_ble_close(res);
free(res);
return NULL;
}
firmata_initPins(res);
firmata_askFirmware(res);
syslog(LOG_INFO, "firmata: Device opened at: %s", name);
return res;
}
void
firmata_ble_close(t_firmata* firmata)
{
lb_result_t r = LB_ERROR_UNSPECIFIED;
// r = lb_unpair_device(lb_ctx, firmata);
// if (r < 0) {
// syslog(LOG_ERR, "ERROR: lb_unpair_device\n");
// exit(r);
//}
r = dl_lb_disconnect_device(firmata->lb_ctx, firmata->bl_dev);
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_disconnect_device\n");
}
r = dl_lb_context_free(firmata->lb_ctx);
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_context_free\n");
}
r = dl_lb_destroy();
if (r < 0) {
syslog(LOG_ERR, "ERROR: lb_destroy\n");
}
}
#endif
void
firmata_close(t_firmata* firmata)
{
@@ -189,7 +354,7 @@ firmata_endParse(t_firmata* firmata)
buf[len++] = 1;
}
firmata->isReady = 1;
mraa_uart_write(firmata->uart, buf, len);
firmata_write_internal(firmata, buf, len);
} else if (firmata->parse_buff[1] == FIRMATA_CAPABILITY_RESPONSE) {
int pin, i, n;
for (pin = 0; pin < 128; pin++) {
@@ -217,7 +382,7 @@ firmata_endParse(t_firmata* firmata)
buf[len++] = pin;
buf[len++] = FIRMATA_END_SYSEX;
}
mraa_uart_write(firmata->uart, buf, len);
firmata_write_internal(firmata, buf, len);
}
} else if (firmata->parse_buff[1] == FIRMATA_ANALOG_MAPPING_RESPONSE) {
int pin = 0;
@@ -235,15 +400,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;
firmata->i2cmsg[addr][reg + ii] =
(firmata->parse_buff[i] & 0x7f) | ((firmata->parse_buff[i + 1] & 0x7f) << 7);
i = i + 2;
}
} else {
if (firmata->devs != NULL) {
@@ -290,7 +456,7 @@ firmata_askFirmware(t_firmata* firmata)
buf[0] = FIRMATA_START_SYSEX;
buf[1] = FIRMATA_REPORT_FIRMWARE; // read firmata name & version
buf[2] = FIRMATA_END_SYSEX;
res = mraa_uart_write(firmata->uart, buf, 3);
res = firmata_write_internal(firmata, buf, 3);
return (res);
}
@@ -304,7 +470,7 @@ firmata_pinMode(t_firmata* firmata, int pin, int mode)
buff[0] = FIRMATA_SET_PIN_MODE;
buff[1] = pin;
buff[2] = mode;
res = mraa_uart_write(firmata->uart, buff, 3);
res = firmata_write_internal(firmata, buff, 3);
return (res);
}
@@ -317,19 +483,19 @@ firmata_analogWrite(t_firmata* firmata, int pin, int value)
buff[0] = 0xE0 | pin;
buff[1] = value & 0x7F;
buff[2] = (value >> 7) & 0x7F;
res = mraa_uart_write(firmata->uart, buff, 3);
res = firmata_write_internal(firmata, buff, 3);
return (res);
}
int
firmata_analogRead(t_firmata *firmata, int pin)
firmata_analogRead(t_firmata* firmata, int pin)
{
int res;
int value = 1;
uint8_t buff[2];
buff[0] = FIRMATA_REPORT_ANALOG | pin;
buff[1] = value;
res = mraa_uart_write(firmata->uart, buff, 2);
res = firmata_write_internal(firmata, buff, 2);
return res;
}
@@ -356,6 +522,6 @@ firmata_digitalWrite(t_firmata* firmata, int pin, int value)
buff[0] = FIRMATA_DIGITAL_MESSAGE | port_num;
buff[1] = port_val & 0x7F;
buff[2] = (port_val >> 7) & 0x7F;
res = mraa_uart_write(firmata->uart, buff, 3);
res = firmata_write_internal(firmata, buff, 3);
return (res);
}

93
src/firmata/firmata_ble.c Normal file
View File

@@ -0,0 +1,93 @@
/*
* Author: Shiran Ben-Melech <shiran.ben-melech@intel.com>
* 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.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "firmata.h"
#include "mraa_internal.h"
#include "firmata/firmata.h"
#include "firmata/firmata_ble.h"
#define PLATFORM_NAME "FIRMATA_BLE"
static mraa_boolean_t liblittleb_load_success = true;
void *
mraa_firmata_ble_dlsym(const char *symbol)
{
void *func = dlsym(liblittleb_lib, symbol);
if (func == NULL) {
syslog(LOG_ERR, "%s", dlerror());
liblittleb_load_success = false;
}
return func;
}
mraa_result_t
mraa_firmata_ble_init()
{
mraa_result_t mraaStatus = MRAA_ERROR_NO_RESOURCES;
liblittleb_lib = dlopen("liblittleb.so", RTLD_LAZY);
if (!liblittleb_lib) {
syslog(LOG_WARNING, "liblittleb.so not found, skipping");
return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
}
dl_lb_init = mraa_firmata_ble_dlsym("lb_init");
dl_lb_destroy = mraa_firmata_ble_dlsym("lb_destroy");
dl_lb_context_new = mraa_firmata_ble_dlsym("lb_context_new");
dl_lb_context_free = mraa_firmata_ble_dlsym("lb_context_free");
dl_lb_get_bl_devices = mraa_firmata_ble_dlsym("lb_get_bl_devices");
dl_lb_connect_device = mraa_firmata_ble_dlsym("lb_connect_device");
dl_lb_disconnect_device = mraa_firmata_ble_dlsym("lb_disconnect_device");
dl_lb_pair_device = mraa_firmata_ble_dlsym("lb_pair_device");
dl_lb_unpair_device = mraa_firmata_ble_dlsym("lb_unpair_device");
dl_lb_get_ble_characteristic_by_characteristic_path = mraa_firmata_ble_dlsym("lb_get_ble_characteristic_by_characteristic_path");
dl_lb_get_ble_characteristic_by_uuid = mraa_firmata_ble_dlsym("lb_get_ble_characteristic_by_uuid");
dl_lb_get_ble_service_by_service_path = mraa_firmata_ble_dlsym("lb_get_ble_service_by_service_path");
dl_lb_get_ble_service_by_uuid = mraa_firmata_ble_dlsym("lb_get_ble_service_by_uuid");
dl_lb_get_ble_device_services = mraa_firmata_ble_dlsym("lb_get_ble_device_services");
dl_lb_get_device_by_device_path = mraa_firmata_ble_dlsym("lb_get_device_by_device_path");
dl_lb_get_device_by_device_name = mraa_firmata_ble_dlsym("lb_get_device_by_device_name");
dl_lb_get_device_by_device_address = mraa_firmata_ble_dlsym("lb_get_device_by_device_address");
dl_lb_write_to_characteristic = mraa_firmata_ble_dlsym("lb_write_to_characteristic");
dl_lb_read_from_characteristic = mraa_firmata_ble_dlsym("lb_read_from_characteristic");
dl_lb_register_characteristic_read_event = mraa_firmata_ble_dlsym("lb_register_characteristic_read_event");
dl_lb_parse_uart_service_message = mraa_firmata_ble_dlsym("lb_parse_uart_service_message");
if (!liblittleb_load_success) {
syslog(LOG_ERR, "Failed to find all symbols for littleb support");
goto init_exit;
}
mraaStatus = MRAA_SUCCESS;
init_exit:
if (mraaStatus == MRAA_SUCCESS)
syslog(LOG_NOTICE, "mraa_firmata_ble_init completed successfully\n");
return mraaStatus;
}

View File

@@ -51,7 +51,7 @@ mraa_firmata_init(int feature)
mraa_result_t
mraa_firmata_write_sysex(mraa_firmata_context dev, char* msg, int length)
{
return mraa_uart_write(firmata_dev->uart, msg, length);
return firmata_write_internal(firmata_dev, msg, length);
}
mraa_result_t
@@ -59,7 +59,7 @@ mraa_firmata_response(mraa_firmata_context dev, void (*fptr)(uint8_t*, int))
{
if (dev->added == 0) {
struct _firmata** ptr;
ptr = realloc(firmata_dev->devs, (firmata_dev->dev_count+1) * sizeof(struct _firmata*));
ptr = realloc(firmata_dev->devs, (firmata_dev->dev_count + 1) * sizeof(struct _firmata*));
if (ptr == NULL) {
return MRAA_ERROR_NO_RESOURCES;
}
@@ -84,6 +84,7 @@ mraa_result_t
mraa_firmata_close(mraa_firmata_context dev)
{
mraa_firmata_response_stop(dev);
firmata_close(firmata_dev);
free(dev);
return MRAA_SUCCESS;
}
@@ -97,7 +98,7 @@ mraa_firmata_i2c_init_bus_replace(mraa_i2c_context dev)
buff[1] = FIRMATA_I2C_CONFIG;
buff[2] = delay & 0xFF, (delay >> 8) & 0xFF;
buff[3] = FIRMATA_END_SYSEX;
mraa_uart_write(firmata_dev->uart, buff, 4);
firmata_write_internal(firmata_dev, buff, 4);
return MRAA_SUCCESS;
}
@@ -106,7 +107,7 @@ static mraa_result_t
mraa_firmata_i2c_address(mraa_i2c_context dev, uint8_t addr)
{
// only thing needed and it's already done
//dev->addr = (int) addr;
// dev->addr = (int) addr;
return MRAA_SUCCESS;
}
@@ -134,13 +135,13 @@ mraa_firmata_send_i2c_read_req(mraa_i2c_context dev, int length)
buffer[5] = (length >> 7) & 0x7f;
buffer[6] = FIRMATA_END_SYSEX;
if (mraa_uart_write(firmata_dev->uart, buffer, 7) != 7) {
if (firmata_write_internal(firmata_dev, buffer, 7) != 7) {
free(buffer);
return MRAA_ERROR_UNSPECIFIED;
}
// this needs a lock :)
memset(&firmata_dev->i2cmsg[dev->addr][0], -1, sizeof(int)*length);
memset(&firmata_dev->i2cmsg[dev->addr][0], -1, sizeof(int) * length);
free(buffer);
return MRAA_SUCCESS;
@@ -166,13 +167,13 @@ mraa_firmata_send_i2c_read_reg_req(mraa_i2c_context dev, uint8_t command, int le
buffer[7] = (length >> 7) & 0x7f;
buffer[8] = FIRMATA_END_SYSEX;
if (mraa_uart_write(firmata_dev->uart, buffer, 9) != 9) {
if (firmata_write_internal(firmata_dev, buffer, 9) != 9) {
free(buffer);
return MRAA_ERROR_UNSPECIFIED;
}
// this needs a lock :)
memset(&firmata_dev->i2cmsg[dev->addr][command], -1, sizeof(int)*length);
memset(&firmata_dev->i2cmsg[dev->addr][command], -1, sizeof(int) * length);
free(buffer);
return MRAA_SUCCESS;
@@ -209,7 +210,7 @@ mraa_firmata_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
if (mraa_firmata_i2c_wait(dev->addr, command) == MRAA_SUCCESS) {
uint8_t rawdata[2];
rawdata[0] = firmata_dev->i2cmsg[dev->addr][command];
rawdata[1] = firmata_dev->i2cmsg[dev->addr][command+1];
rawdata[1] = firmata_dev->i2cmsg[dev->addr][command + 1];
uint16_t data = (uint16_t) rawdata;
uint8_t high = (data & 0xFF00) >> 8;
data = (data << 8) & 0xFF00;
@@ -226,7 +227,7 @@ mraa_firmata_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t*
{
if (mraa_firmata_send_i2c_read_reg_req(dev, command, length) == MRAA_SUCCESS) {
if (mraa_firmata_i2c_wait(dev->addr, command) == MRAA_SUCCESS) {
memcpy(data, &firmata_dev->i2cmsg[dev->addr][command], sizeof(int)*length);
memcpy(data, &firmata_dev->i2cmsg[dev->addr][command], sizeof(int) * length);
return length;
}
}
@@ -265,7 +266,7 @@ static mraa_result_t
mraa_firmata_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
{
// buffer needs 5 bytes for firmata, and 2 bytes for every byte of data
int buffer_size = (bytesToWrite*2) + 5;
int buffer_size = (bytesToWrite * 2) + 5;
uint8_t* buffer = calloc(buffer_size, 0);
if (buffer == NULL) {
return MRAA_ERROR_NO_RESOURCES;
@@ -277,13 +278,13 @@ mraa_firmata_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWri
buffer[2] = dev->addr;
buffer[3] = I2C_MODE_WRITE << 3;
// we need to write until FIRMATA_END_SYSEX
for (i; i < (buffer_size-1); i++) {
for (i; i < (buffer_size - 1); i++) {
buffer[ii] = data[i] & 0x7F;
buffer[ii+1] = (data[i] >> 7) & 0x7f;
ii = ii+2;
buffer[ii + 1] = (data[i] >> 7) & 0x7f;
ii = ii + 2;
}
buffer[buffer_size-1] = FIRMATA_END_SYSEX;
mraa_uart_write(firmata_dev->uart, buffer, buffer_size);
buffer[buffer_size - 1] = FIRMATA_END_SYSEX;
firmata_write_internal(firmata_dev, buffer, buffer_size);
free(buffer);
return MRAA_SUCCESS;
}
@@ -302,7 +303,7 @@ mraa_firmata_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
buffer[4] = data & 0x7F;
buffer[5] = (data >> 7) & 0x7F;
buffer[6] = FIRMATA_END_SYSEX;
mraa_uart_write(firmata_dev->uart, buffer, 7);
firmata_write_internal(firmata_dev, buffer, 7);
free(buffer);
return MRAA_SUCCESS;
}
@@ -323,7 +324,7 @@ mraa_firmata_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const
buffer[6] = data & 0x7F;
buffer[7] = (data >> 7) & 0x7F;
buffer[8] = FIRMATA_END_SYSEX;
mraa_uart_write(firmata_dev->uart, buffer, 9);
firmata_write_internal(firmata_dev, buffer, 9);
free(buffer);
return MRAA_SUCCESS;
}
@@ -472,7 +473,7 @@ mraa_firmata_pwm_init_internal_replace(void* func_table, int pin)
static mraa_result_t
mraa_firmata_pwm_write_replace(mraa_pwm_context dev, float percentage)
{
int value = (int)((percentage - 1) / 8000);
int value = (int) ((percentage - 1) / 8000);
firmata_analogWrite(firmata_dev, dev->pin, value);
firmata_dev->pins[dev->pin].value = value;
return MRAA_SUCCESS;
@@ -505,51 +506,67 @@ mraa_firmata_pull_handler(void* vp)
{
int i, isr_now, isr_prev;
isr_prev = 0;
while(1) {
while (1) {
isr_now = 0;
firmata_pull(firmata_dev);
// would prefer to send board pointer as argument
for(i = 0; i < 14; i++) {
for (i = 0; i < 14; i++) {
isr_now |= (firmata_dev->pins[i].value & 1) << i;
}
// might want to lock here?
isr_detected = isr_prev ^ isr_now; //both edges for now
isr_detected = isr_prev ^ isr_now; // both edges for now
isr_prev = isr_now;
usleep(100);
}
}
mraa_board_t*
mraa_firmata_plat_init(const char* uart_dev)
mraa_firmata_plat_init(const char* uart_dev, mraa_platform_t type)
{
mraa_board_t* b = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
if (b == NULL) {
return NULL;
}
firmata_dev = firmata_new(uart_dev);
if (firmata_dev == NULL) {
syslog(LOG_WARNING, "firmata: Failed to open uart to Firmata dev on %s", uart_dev);
fprintf(stderr, "Mraa expected to find a Firmata device on %s, is the port in use?\n", uart_dev);
free(b);
if (type == MRAA_GENERIC_FIRMATA) {
firmata_dev = firmata_new(uart_dev);
if (firmata_dev == NULL) {
syslog(LOG_WARNING, "firmata: Failed to open uart to Firmata dev on %s", uart_dev);
fprintf(stderr, "Mraa expected to find a Firmata device on %s, is the port in use?\n", uart_dev);
free(b);
return NULL;
}
// if this isn't working then we have an issue with our uart
int retry = 20;
while (!firmata_dev->isReady && retry--) {
firmata_pull(firmata_dev);
}
if (!retry) {
syslog(LOG_ERR, "firmata: Failed to find a valid Firmata board on %s", uart_dev);
firmata_close(firmata_dev);
free(b);
return NULL;
}
pthread_create(&thread_id, NULL, mraa_firmata_pull_handler, NULL);
}
else if (type == MRAA_BLE_FIRMATA_BY_NAME || type == MRAA_BLE_FIRMATA_BY_ADDRESS) {
#ifndef FIRMATABLE
return NULL;
#endif
firmata_dev = firmata_ble_new(uart_dev, type);
if (firmata_dev == NULL) {
syslog(LOG_WARNING, "firmata: Failed to open ble to Firmata dev on %s", uart_dev);
fprintf(stderr, "Mraa expected to find a Firmata ble device named %s, is the device "
"available?\n",
uart_dev);
free(b);
return NULL;
}
}
// if this isn't working then we have an issue with our uart
int retry = 20;
while (!firmata_dev->isReady && retry--) {
firmata_pull(firmata_dev);
}
if (!retry) {
syslog(LOG_ERR, "firmata: Failed to find a valid Firmata board on %s", uart_dev);
firmata_close(firmata_dev);
free(b);
return NULL;
}
pthread_create(&thread_id, NULL, mraa_firmata_pull_handler, NULL);
b->platform_name = "firmata";
// do we support 2.5? Or are we more 2.3?
// or should we return the flashed sketch name?
@@ -680,7 +697,7 @@ mraa_firmata_plat_init(const char* uart_dev)
}
mraa_platform_t
mraa_firmata_platform(mraa_board_t* board, const char* uart_dev)
mraa_firmata_platform(mraa_board_t* board, const char* uart_dev, mraa_platform_t type)
{
/**
* Firmata boards are not something we can detect so we just trust the user
@@ -689,12 +706,12 @@ mraa_firmata_platform(mraa_board_t* board, const char* uart_dev)
*/
mraa_board_t* sub_plat = NULL;
sub_plat = mraa_firmata_plat_init(uart_dev);
sub_plat = mraa_firmata_plat_init(uart_dev, type);
if (sub_plat != NULL) {
sub_plat->platform_type = MRAA_GENERIC_FIRMATA;
sub_plat->platform_type = type;
board->sub_platform = sub_plat;
return sub_plat->platform_type;
}
return MRAA_NULL_PLATFORM;
}
}

View File

@@ -205,7 +205,8 @@ imraa_init()
return MRAA_ERROR_NO_RESOURCES;
}
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(),
mraa_get_platform_type());
return MRAA_SUCCESS;
}
@@ -252,7 +253,6 @@ mraa_deinit()
}
#endif
free(plat);
}
if (plat_iio != NULL) {
free(plat_iio);
@@ -302,7 +302,7 @@ mraa_iio_detect()
plat_iio->iio_device_count = num_iio_devices;
plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
struct _iio* device;
for (i=0; i < num_iio_devices; i++) {
for (i = 0; i < num_iio_devices; i++) {
device = &plat_iio->iio_devices[i];
device->num = i;
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
@@ -315,7 +315,7 @@ mraa_iio_detect()
len = strlen(name);
// use strndup
device->name = malloc((sizeof(char) * len) + sizeof(char));
strncpy(device->name, name, len+1);
strncpy(device->name, name, len + 1);
}
close(fd);
}
@@ -334,22 +334,23 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
for (mi = 0; mi < meta.mux_total; mi++) {
switch(meta.mux[mi].pincmd) {
case PINCMD_UNDEFINED: // used for backward compatibility
if(meta.mux[mi].pin != last_pin) {
switch (meta.mux[mi].pincmd) {
case PINCMD_UNDEFINED: // used for backward compatibility
if (meta.mux[mi].pin != last_pin) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
}
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
if (mux_i == NULL)
return MRAA_ERROR_INVALID_HANDLE;
last_pin = meta.mux[mi].pin;
}
// this function will sometimes fail, however this is not critical as
// long as the write succeeds - Test case galileo gen2 pin2
mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
if(ret != MRAA_SUCCESS) {
if (ret != MRAA_SUCCESS) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
@@ -359,19 +360,20 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
break;
case PINCMD_SET_VALUE:
if(meta.mux[mi].pin != last_pin) {
if (meta.mux[mi].pin != last_pin) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
}
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
if (mux_i == NULL)
return MRAA_ERROR_INVALID_HANDLE;
last_pin = meta.mux[mi].pin;
}
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
if(ret != MRAA_SUCCESS) {
if (ret != MRAA_SUCCESS) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
@@ -381,19 +383,20 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
break;
case PINCMD_SET_DIRECTION:
if(meta.mux[mi].pin != last_pin) {
if (meta.mux[mi].pin != last_pin) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
}
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
if (mux_i == NULL)
return MRAA_ERROR_INVALID_HANDLE;
last_pin = meta.mux[mi].pin;
}
ret = mraa_gpio_dir(mux_i, meta.mux[mi].value);
if(ret != MRAA_SUCCESS) {
if (ret != MRAA_SUCCESS) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
@@ -403,22 +406,23 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
break;
case PINCMD_SET_IN_VALUE:
if(meta.mux[mi].pin != last_pin) {
if (meta.mux[mi].pin != last_pin) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
}
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
if (mux_i == NULL)
return MRAA_ERROR_INVALID_HANDLE;
last_pin = meta.mux[mi].pin;
}
ret = mraa_gpio_dir(mux_i, MRAA_GPIO_IN);
if(ret == MRAA_SUCCESS)
if (ret == MRAA_SUCCESS)
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
if(ret != MRAA_SUCCESS) {
if (ret != MRAA_SUCCESS) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
@@ -428,22 +432,23 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
break;
case PINCMD_SET_OUT_VALUE:
if(meta.mux[mi].pin != last_pin) {
if (meta.mux[mi].pin != last_pin) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
}
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
if (mux_i == NULL)
return MRAA_ERROR_INVALID_HANDLE;
last_pin = meta.mux[mi].pin;
}
ret = mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
if(ret == MRAA_SUCCESS)
if (ret == MRAA_SUCCESS)
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
if(ret != MRAA_SUCCESS) {
if (ret != MRAA_SUCCESS) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
@@ -453,19 +458,20 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
break;
case PINCMD_SET_MODE:
if(meta.mux[mi].pin != last_pin) {
if (meta.mux[mi].pin != last_pin) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
}
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
if (mux_i == NULL)
return MRAA_ERROR_INVALID_HANDLE;
last_pin = meta.mux[mi].pin;
}
ret = mraa_gpio_mode(mux_i, meta.mux[mi].value);
if(ret != MRAA_SUCCESS) {
if (ret != MRAA_SUCCESS) {
if (mux_i != NULL) {
mraa_gpio_owner(mux_i, 0);
mraa_gpio_close(mux_i);
@@ -478,7 +484,9 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
break;
default:
syslog(LOG_NOTICE, "mraa_setup_mux_mapped: wrong command %d on pin %d with value %d", meta.mux[mi].pincmd, meta.mux[mi].pin, meta.mux[mi].value);
syslog(LOG_NOTICE,
"mraa_setup_mux_mapped: wrong command %d on pin %d with value %d",
meta.mux[mi].pincmd, meta.mux[mi].pin, meta.mux[mi].value);
break;
}
}
@@ -572,7 +580,8 @@ mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
pin = mraa_get_sub_platform_index(pin);
}
if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM || current_plat->platform_type == MRAA_NULL_PLATFORM) {
if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM ||
current_plat->platform_type == MRAA_NULL_PLATFORM) {
return 0;
}
if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
@@ -749,9 +758,9 @@ mraa_get_platform_pin_count(uint8_t platform_offset)
return mraa_get_pin_count();
else {
if (mraa_has_sub_platform())
return plat->sub_platform->phy_pin_count;
return plat->sub_platform->phy_pin_count;
else
return 0;
return 0;
}
}
@@ -786,9 +795,9 @@ mraa_get_default_i2c_bus(uint8_t platform_offset)
return plat->def_i2c_bus;
} else {
if (mraa_has_sub_platform())
return plat->sub_platform->def_i2c_bus;
return plat->sub_platform->def_i2c_bus;
else
return -1;
return -1;
}
}
@@ -1023,18 +1032,36 @@ mraa_get_iio_device_count()
}
mraa_result_t
mraa_add_subplatform(mraa_platform_t subplatformtype, const char* uart_dev)
mraa_add_subplatform(mraa_platform_t subplatformtype, const char* dev)
{
#if defined(FIRMATA)
if (subplatformtype == MRAA_GENERIC_FIRMATA) {
if (plat->sub_platform != NULL) {
return MRAA_ERROR_INVALID_PARAMETER;
}
if (mraa_firmata_platform(plat, uart_dev) == MRAA_GENERIC_FIRMATA) {
if (mraa_firmata_platform(plat, dev, MRAA_GENERIC_FIRMATA) == MRAA_GENERIC_FIRMATA) {
syslog(LOG_NOTICE, "mraa: Added firmata subplatform");
return MRAA_SUCCESS;
}
syslog(LOG_NOTICE, "mraa: Failed to add firmata subplatform");
} else if (subplatformtype == MRAA_BLE_FIRMATA_BY_NAME) {
if (plat->sub_platform != NULL) {
return MRAA_ERROR_INVALID_PARAMETER;
}
if (mraa_firmata_platform(plat, dev, MRAA_BLE_FIRMATA_BY_NAME) == MRAA_BLE_FIRMATA_BY_NAME) {
syslog(LOG_NOTICE, "mraa: Added firmata ble subplatform by name");
return MRAA_SUCCESS;
}
syslog(LOG_NOTICE, "mraa: Failed to add firmata ble subplatform by name");
} else if (subplatformtype == MRAA_BLE_FIRMATA_BY_ADDRESS) {
if (plat->sub_platform != NULL) {
return MRAA_ERROR_INVALID_PARAMETER;
}
if (mraa_firmata_platform(plat, dev, MRAA_BLE_FIRMATA_BY_ADDRESS) == MRAA_BLE_FIRMATA_BY_ADDRESS) {
syslog(LOG_NOTICE, "mraa: Added firmata ble subplatform by address");
return MRAA_SUCCESS;
}
syslog(LOG_NOTICE, "mraa: Failed to add firmata ble subplatform by address");
}
#endif
@@ -1092,8 +1119,9 @@ mraa_add_from_lockfile(const char* imraa_lock_file)
int id = -1;
const char* uartdev = NULL;
for (i = 0; i < subplat_num; i++) {
struct json_object *ioobj = json_object_array_get_idx(ioarray, i);
json_object_object_foreach(ioobj, key, val) {
struct json_object* ioobj = json_object_array_get_idx(ioarray, i);
json_object_object_foreach(ioobj, key, val)
{
if (strcmp(key, "id") == 0) {
id = atoi(json_object_get_string(val));
} else if (strcmp(key, "uart") == 0) {
@@ -1237,11 +1265,13 @@ mraa_init_io(const char* desc)
} else if (strncmp(type, "PWM", 3) == 0) {
if (raw) {
if (mraa_init_io_helper(&str, &id, delim) != MRAA_SUCCESS) {
syslog(LOG_ERR, "mraa_init_io: Pwm, unable to convert the chip id string into a useable Int");
syslog(LOG_ERR, "mraa_init_io: Pwm, unable to convert the chip id string into a "
"useable Int");
return NULL;
}
if (mraa_init_io_helper(&str, &pin, delim) != MRAA_SUCCESS) {
syslog(LOG_ERR, "mraa_init_io: Pwm, unable to convert the pin string into a useable Int");
syslog(LOG_ERR,
"mraa_init_io: Pwm, unable to convert the pin string into a useable Int");
return NULL;
}
return (void*) mraa_pwm_init_raw(id, pin);
@@ -1250,11 +1280,13 @@ mraa_init_io(const char* desc)
} else if (strncmp(type, "SPI", 3) == 0) {
if (raw) {
if (mraa_init_io_helper(&str, &id, delim) != MRAA_SUCCESS) {
syslog(LOG_ERR, "mraa_init_io: Spi, unable to convert the bus string into a useable Int");
syslog(LOG_ERR,
"mraa_init_io: Spi, unable to convert the bus string into a useable Int");
return NULL;
}
if (mraa_init_io_helper(&str, &pin, delim) != MRAA_SUCCESS) {
syslog(LOG_ERR, "mraa_init_io: Spi, unable to convert the cs string into a useable Int");
syslog(LOG_ERR,
"mraa_init_io: Spi, unable to convert the cs string into a useable Int");
return NULL;
}
return (void*) mraa_spi_init_raw(id, pin);