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

👷 (PlatformIO): Build examples #11

Merged
merged 3 commits into from
Jul 19, 2024
Merged
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
68 changes: 68 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: PlatformIO CI

on:
pull_request:
branches:
- main
- master
- develop
paths-ignore:
- "**/*.md"
push:
branches:
- main
- master
- develop
paths-ignore:
- "**/*.md"

jobs:
platformio:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ ubuntu-latest ]
example:
- "examples/PCA9685/Servo/Servo.ino"
- "examples/PCA9685/VibroPulse/VibroPulse.ino"
boards: [ [ uno, esp32dev ] ]

steps:
- uses: actions/checkout@v4

- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Cache PlatformIO
uses: actions/cache@v4
with:
path: |
~/.platformio/.cache
./.pio
key: ${{ runner.os }}-pio-${{ hashFiles('**/*.ini') }}
restore-keys: |
${{ runner.os }}-pio-

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.9"

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio
pio upgrade --dev
pio pkg update --global

- name: Build example
run: |
pio ci --lib="." --board=${{ join(matrix.boards, ' --board=') }}
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
27 changes: 8 additions & 19 deletions examples/PCA9685/Servo/Servo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ void setup() {
Serial.begin(115200);
Serial.println("I2CDevLib Example - PCA9685 Test");

if (!Wire.begin()) {
Serial.println("[E] Failed to init Wire!");
while(true) {};
}
Wire.begin();

// auto result = pca9685.setPrescale(100);
result = pca9685.setFrequency(SERVO_FREQ);
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed to set PCA9685 prescale: %i\n", result);
Serial.println("[E] Failed to set PCA9685 prescale");
while(true) {};
}
result = pca9685.wakeup();
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed wakeup PCA9685: %i\n", result);
Serial.println("[E] Failed to wake up PCA9685");
while(true) {};
}

Expand All @@ -46,16 +43,14 @@ void setup() {
uint8_t currentServo = 0;

void loop() {
Serial.printf("Current servo: %i\n", currentServo);
Serial.print("Current servo: "); Serial.println(currentServo);

Serial.println("\t > SERVO_PULSE_LEN_MIN => SERVO_PULSE_LEN_MAX");
for (uint16_t pulseLen = SERVO_PULSE_LEN_MIN; pulseLen < SERVO_PULSE_LEN_MAX; pulseLen++) {
result = pca9685.setChannel(currentServo, pulseLen);
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed to set PCA9685 pin: %i\n", result);
Serial.println("[E] Failed to set PCA9685 pin");
}
// delay(5);
// Serial.printf("\t > SERVO_PULSE_LEN_MIN => SERVO_PULSE_LEN_MAX: %i\n", pulseLen);
}

delay(500);
Expand All @@ -64,10 +59,8 @@ void loop() {
for (uint16_t pulseLen = SERVO_PULSE_LEN_MAX; pulseLen > SERVO_PULSE_LEN_MIN; pulseLen--) {
result = pca9685.setChannel(currentServo, pulseLen);
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed to set PCA9685 pin: %i\n", result);
Serial.println("[E] Failed to set PCA9685 pin");
}
// delay(5);
// Serial.printf("\t > SERVO_PULSE_LEN_MAX => SERVO_PULSE_LEN_MIN: %i\n", pulseLen);
}

delay(500);
Expand All @@ -76,10 +69,8 @@ void loop() {
for (uint16_t uSec = SERVO_US_MIN; uSec < SERVO_US_MAX; uSec++) {
result = pca9685.writeMicroseconds(currentServo, uSec);
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed to set PCA9685 microseconds: %i\n", result);
Serial.println("[E] Failed to set PCA9685 microseconds");
}
// delay(5);
// Serial.printf("\t > SERVO_US_MIN => SERVO_US_MAX: %i\n", uSec);
}

delay(500);
Expand All @@ -88,10 +79,8 @@ void loop() {
for (uint16_t uSec = SERVO_US_MAX; uSec > SERVO_US_MIN; uSec--) {
result = pca9685.writeMicroseconds(currentServo, uSec);
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed to set PCA9685 microseconds: %i\n", result);
Serial.println("[E] Failed to set PCA9685 microseconds");
}
// delay(5);
// Serial.printf("\t > SERVO_US_MAX => SERVO_US_MIN: %i\n", uSec);
}

delay(500);
Expand Down
11 changes: 4 additions & 7 deletions examples/PCA9685/VibroPulse/VibroPulse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ void setup() {
Serial.begin(115200);
Serial.println("I2CDevLib Example - PCA9685 Test");

if (!Wire.begin()) {
Serial.println("[E] Failed to init Wire!");
while(true) {};
}
Wire.begin();

// auto result = pca9685.setPrescale(100);
result = pca9685.setFrequency(PWM_FREQ);
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed to set PCA9685 prescale: %i\n", result);
Serial.println("[E] Failed to set PCA9685 prescale");
while(true) {};
}
result = pca9685.wakeup();
if (result != I2CDEV_RESULT_OK) {
Serial.printf("[E] Failed wakeup PCA9685: %i\n", result);
Serial.println("[E] Failed wakeup PCA9685");
while(true) {};
}

Expand All @@ -46,7 +43,7 @@ void pulse(uint8_t pin) {
uint8_t currentVibro = 0;

void loop() {
Serial.printf("Current vibro: %i\n", currentVibro);
Serial.print("Current vibro: "); Serial.println(currentVibro);

pulse(currentVibro);
delay(100);
Expand Down
50 changes: 29 additions & 21 deletions src/hal/I2CDevBus_ArduinoWire.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
#include <Wire.h>

class ArduinoI2CDevBus : public I2CDevBus {
public:
public:
explicit ArduinoI2CDevBus(TwoWire* wire = &Wire) : wire_(wire) {}

private:
private:
TwoWire* wire_;

auto readReg8(
std::uint8_t devAddr,
std::uint8_t regAddr,
std::size_t length,
std::uint8_t* data,
std::uint16_t timeout = DEFAULT_READ_TIMEOUT_MS
uint8_t devAddr,
uint8_t regAddr,
size_t length,
uint8_t* data,
uint16_t timeout = DEFAULT_READ_TIMEOUT_MS
) -> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("readReg8: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());
Expand All @@ -25,9 +25,13 @@ class ArduinoI2CDevBus : public I2CDevBus {
this->wire_->write(regAddr);
this->wire_->endTransmission();

#if defined(ARDUINO_ARCH_AVR)
this->wire_->requestFrom(devAddr, static_cast<uint8_t>(length), 1);
#else
this->wire_->requestFrom(devAddr, length, true);
#endif

std::uint8_t received = 0;
uint8_t received = 0;
const auto start = millis();

while (this->wire_->available() && (timeout == 0 || millis() - start < timeout)) {
Expand All @@ -47,11 +51,11 @@ class ArduinoI2CDevBus : public I2CDevBus {
}

auto readReg16(
std::uint8_t devAddr,
std::uint8_t regAddr,
std::size_t length,
std::uint16_t* data,
std::uint16_t timeout = DEFAULT_READ_TIMEOUT_MS
uint8_t devAddr,
uint8_t regAddr,
size_t length,
uint16_t* data,
uint16_t timeout = DEFAULT_READ_TIMEOUT_MS
) -> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("readReg16: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());
Expand All @@ -60,9 +64,13 @@ class ArduinoI2CDevBus : public I2CDevBus {
this->wire_->write(regAddr);
this->wire_->endTransmission();

#if defined(ARDUINO_ARCH_AVR)
this->wire_->requestFrom(devAddr, static_cast<uint8_t>(length * 2), 1);
#else
this->wire_->requestFrom(devAddr, length * 2, true);
#endif

std::uint8_t received = 0;
uint8_t received = 0;
const auto start = millis();

while (this->wire_->available() >= 2 && (timeout == 0 || millis() - start < timeout)) {
Expand All @@ -81,8 +89,8 @@ class ArduinoI2CDevBus : public I2CDevBus {
return I2CDEV_RESULT_OK;
}

auto writeReg8(std::uint8_t devAddr, std::uint8_t regAddr, std::size_t length, const std::uint8_t* data)
-> i2cdev_result_t override
auto writeReg8(uint8_t devAddr, uint8_t regAddr, size_t length, const uint8_t* data)
-> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("writeReg8: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());

Expand All @@ -105,8 +113,8 @@ class ArduinoI2CDevBus : public I2CDevBus {
return I2CDEV_RESULT_OK;
}

auto writeReg16(std::uint8_t devAddr, std::uint8_t regAddr, std::size_t length, const std::uint16_t* data)
-> i2cdev_result_t override
auto writeReg16(uint8_t devAddr, uint8_t regAddr, size_t length, const uint16_t* data)
-> i2cdev_result_t override
{
I2CDEVLIB_LOG_D("writeReg16: devAddr=0x%02X, regAddr=0x%02X, data=%s", devAddr, regAddr, i2cdevlib::hexdump(data, length).c_str());

Expand All @@ -118,14 +126,14 @@ class ArduinoI2CDevBus : public I2CDevBus {
}

// Send data
for (std::size_t i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
// Send MSB
if (this->wire_->write(static_cast<std::uint8_t>(data[i] >> 8)) != 1) {
if (this->wire_->write(static_cast<uint8_t>(data[i] >> 8)) != 1) {
return I2CDEV_RESULT_ERROR;
}

// Send LSB
if (this->wire_->write(static_cast<std::uint8_t>(data[i] & 0xFF)) != 1) {
if (this->wire_->write(static_cast<uint8_t>(data[i] & 0xFF)) != 1) {
return I2CDEV_RESULT_ERROR;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/i2cdevbus.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef __I2CDEVBUS_H__
#define __I2CDEVBUS_H__

#ifdef ARDUINO
#include "Arduino.h"
#else // !ARDUINO
#include "stdint.h"
#endif // ARDUINO

/// 1000ms default read timeout (modify with `I2CDevBus::DEFAULT_READ_TIMEOUT_MS = [ms];`)
#ifndef I2CDEV_DEFAULT_READ_TIMEOUT_MS
Expand Down
Loading