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

Initial support for Bluetooth #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "vendor/serial"]
path = vendor/serial
url = https://github.com/wjwwood/serial.git
[submodule "vendor/libblepp"]
path = vendor/libblepp
url = https://github.com/edrosten/libblepp.git
37 changes: 35 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ project(firmatacpp)
set (CMAKE_CXX_STANDARD 11)

option(FIRMATA_BUILD_EXAMPLES "Build firmata example programs" YES)
option(FIRMATA_WITH_BLUETOOTH "Enable Bluetooth support" YES)

if (FIRMATA_WITH_BLUETOOTH)
include (ExternalProject)

ExternalProject_Add(libblepp
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/libblepp
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/vendor/libblepp/configure --prefix=<INSTALL_DIR>
BUILD_COMMAND ${MAKE})
ExternalProject_Get_Property(libblepp install_dir)
set(FIRMATA_BLESRC src/firmble.cpp)
set(FIRMATA_BLEHDR include/firmble.h)
else()
set(FIRMATA_BLESRC)
set(FIRMATA_BLEHDR)
endif()


include (GenerateExportHeader)

Expand All @@ -16,6 +33,10 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/vendor/serial/include
)

if (FIRMATA_WITH_BLUETOOTH)
include_directories( ${install_dir}/include)
endif()

set(FIRMATACPP_SOURCES
src/firmbase.cpp
src/firmi2c.cpp
Expand All @@ -27,19 +48,31 @@ set(FIRMATACPP_INCLUDES
include/firmata.h
include/firmbase.h
include/firmi2c.h
include/firmio.h
include/firmio.h
include/firmserial.h
${CMAKE_CURRENT_BINARY_DIR}/firmatacpp_export.h
)

add_library(firmatacpp ${FIRMATACPP_SOURCES} ${FIRMATACPP_INCLUDES})
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3" )

add_library(firmatacpp ${FIRMATACPP_SOURCES} ${FIRMATA_BLESRC} ${FIRMATACPP_INCLUDES} ${FIRMATA_BLEHDR})
add_dependencies(firmatacpp libblepp)
generate_export_header(firmatacpp)
set_target_properties(firmatacpp PROPERTIES
COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)

link_directories(
${install_dir}/lib
)

target_link_libraries(firmatacpp serial)

if (FIRMATA_BUILD_EXAMPLES)
add_executable(simple_example examples/simple.cpp)
target_link_libraries(simple_example firmatacpp)

if (FIRMATA_WITH_BLUETOOTH)
add_executable(simple_exampleble examples/simpleble.cpp)
target_link_libraries(simple_exampleble firmatacpp ble++)
endif()
endif()
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#firmatacpp [![Donate](https://nourish.je/assets/images/donate.svg)](http://ko-fi.com/A250KJT)

A C++ firmata client library. Currently implements the basic protocol and I2C extension, version 2.5.

If you are having problems with messages like this:

```
CMake Error at vendor/serial/CMakeLists.txt:30 (add_library):
Cannot find source file:

src/serial.cc
```

then the magic commands are: `git submodule init && git submodule update`

Packages required to build on Ubuntu are: bluetooth bluez libbluetooth-dev libboost-all-dev
119 changes: 119 additions & 0 deletions examples/simpleble.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <iostream>
#include <signal.h>

#include "firmata.h"
#include "firmble.h"

#ifndef WIN32
#include "unistd.h"
#endif

bool stopping = false;

void do_stop(int sig)
{
std::cout << "Shutting down..." << std::endl;
stopping = true;
}

/*
* Detect first bluetooth device with a StandardFirmata interface
* Read analog inputs A0 and A1 and digital pin 2 (eg, a Playstation analog stick + button)
* as well as I2C address 8 (eg, the slave_sender example that comes with Arduino IDE)
* and print to stdout
*/

int main(int argc, const char* argv[])
{
signal(SIGINT, do_stop);
signal(SIGTERM, do_stop);

std::vector<firmata::BlePortInfo> ports = firmata::FirmBle::listPorts(3);
firmata::Firmata<firmata::Base, firmata::I2C>* f = NULL;
firmata::FirmBle* bleio;

for (auto port : ports) {
std::cout << port.port << std::endl;

if (f != NULL) {
delete f;
f = NULL;
}

try {
bleio = new firmata::FirmBle(port.port.c_str());
#ifndef WIN32
//if (bleio->available()) {
f = new firmata::Firmata<firmata::Base, firmata::I2C>(bleio);
//}
#else
f = new firmata::Firmata<firmata::Base, firmata::I2C>(bleio);
#endif
}
catch(firmata::IOException e) {
std::cout << e.what() << std::endl;
}
catch(firmata::NotOpenException e) {
std::cout << e.what() << std::endl;
}
if (f != NULL && f->ready()) {
break;
}
}
if (f == NULL || !f->ready()) {
std::cout << "nothing there" <<std::endl;
return 1;
}

int sleep_usecs = 100000;
try {
f->setSamplingInterval(100);

std::cout << f->name << std::endl;
std::cout << f->major_version << std::endl;
std::cout << f->minor_version << std::endl;

f->pinMode(2, MODE_INPUT);

f->reportAnalog(0, 1);
f->reportAnalog(1, 1);

f->reportDigital(0, 1);
//f->configI2C(0);
//f->reportI2C(8, FIRMATA_I2C_REGISTER_NOT_SPECIFIED, 6);

while (true) {
if (stopping)
{
break;
}
f->parse();
int a0 = f->analogRead("A0");
int a1 = f->analogRead("A1");
int pin2 = f->digitalRead(2);
//std::vector<uint8_t> i2c = f->readI2C(8);
//std::string s = "";
//for (auto byte = i2c.begin(); byte < i2c.end(); ++byte) {
//s += (char)*byte;
//}

std::cout << a0 << ", " << a1 << ", " << pin2 << std::endl;
f->digitalWrite(13,1);
usleep(sleep_usecs);
f->digitalWrite(13,0);
usleep(sleep_usecs);
};

std::vector<unsigned char> r;
r.push_back(FIRMATA_SYSTEM_RESET);
f->standardCommand(r);
delete f;
}
catch (firmata::IOException e) {
std::cout << e.what() << std::endl;
}
catch (firmata::NotOpenException e) {
std::cout << e.what() << std::endl;
}
}

7 changes: 6 additions & 1 deletion include/firmata_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#define MODE_SERVO 0x04
#define MODE_SHIFT 0x05
#define MODE_I2C 0x06
#define MODE_ONEWIRE 0x07
#define MODE_STEPPER 0x08
#define MODE_ENCODER 0x09
#define MODE_SERIAL 0x0a
#define MODE_PULLUP 0x0b

#define LOW 0
#define HIGH 1
Expand Down Expand Up @@ -69,4 +74,4 @@ typedef struct s_pin
std::vector<uint8_t> resolutions;
} t_pin;

#endif
#endif
23 changes: 17 additions & 6 deletions include/firmbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace firmata {

void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t value);
void analogWrite(uint8_t pin, uint32_t value);
void analogWrite(const std::string& channel, uint32_t value);
void analogWrite(uint8_t pin, uint32_t value); // pin = digital pin
void analogWrite(const std::string& channel, uint32_t value); // pin = "AN" where N is analog pin

uint8_t digitalRead(uint8_t pin);
uint32_t analogRead(uint8_t pin);
Expand All @@ -38,16 +38,25 @@ namespace firmata {
void sysexCommand(uint8_t sysex_command);
void sysexCommand(std::vector<uint8_t> sysex_command);

void reportAnalog(uint8_t channel, uint8_t enable = 1);
void reportDigital(uint8_t port, uint8_t enable = 1);
void reportAnalog(uint8_t channel, uint8_t enable = 1); // pin = analog pin
void reportDigital(uint8_t port, uint8_t enable = 1); // port = port group
void reportDigitalPin(uint8_t pin, uint8_t enable = 1);
void setSamplingInterval(uint32_t intervalms);

const uint8_t getNumPins() const { return m_numPins; }
const std::vector<uint8_t> & getPinCaps(uint8_t pin) const { return pins[pin].supported_modes; }
const std::vector<uint8_t> & getPinResolutions(uint8_t pin) const { return pins[pin].resolutions; }
uint8_t getPinCapResolution(uint8_t pin, uint8_t mode) const;
uint8_t getPinMode(uint8_t pin) { return pins[pin].mode; }
uint8_t getPinAnalogChannel(uint8_t pin) const { return pins[pin].analog_channel; } // Dpin -> Apin
uint8_t getPinFromAnalogChannel(uint8_t apin) const { return apins[apin]; } // Apin -> Dpin

protected:
virtual bool handleSysex(uint8_t command, std::vector<uint8_t> data);
virtual bool handleString(std::string data);

bool awaitResponse(uint8_t command, uint32_t timeout = 1000);
bool awaitSysexResponse(uint8_t sysexCommand, uint32_t timeout = 1000);
bool awaitResponse(uint8_t command, uint32_t timeout = 1000 /* ms */ );
bool awaitSysexResponse(uint8_t sysexCommand, uint32_t timeout = 1000 /* ms */ );

private:
void initPins();
Expand All @@ -64,7 +73,9 @@ namespace firmata {


FirmIO* m_firmIO;
uint8_t m_numPins;
t_pin pins[128];
uint8_t apins[128];
};

}
Expand Down
Loading