投稿済みの下記ライブラリを使いArduinoのライブラリをRaspberry Pi 5 で動作させるためのヘルパーライブラリを作ってみた。
Raspberry Pi 5 のRP1-Chipを使い倒す(GPIO)
Raspberry Pi 5 のSoftware-I2C libraryを作ってみた。
Raspberry Pi 5 のSoftware-SPI libraryを作ってみた。
Arduinoのハードウェアに依存するものは対応できないが標準のGPIO/I2C/SPIのみを使うライブラリなど若干の修正のみで動作するものもあるのでそのさいに必要となる基本機能のみを実装。
【使い方】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <arduino.h> #include <xxxxxxx.h> /* arduino library */ int main(int argc, char **argv) { /* 利用開始のための初期化 */ arduino(); /* arduino helper class */ Wire.begin(); SPI.begin(); while (1) { /* arduino helper functions */ pinMode(...) digitalWrite(...) digitalRead(...) analogWrite(...) micros(); millis(); delayMicroseconds(...); delay(...); yield(); } return 0; } |
【ライブラリ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
/* arduino.h - Helper library for using Arduino libraries on Raspberry Pi 5 Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include "rp1-gpio.h" #include "i2c-gpio.h" #include "spi-gpio.h" #define _BV(b) (1 << (b)) inline RP1_GPIO GPIO; inline TwoWire Wire(GPIO, 3, 2); /* for standard I2C pins */ inline SPIClass SPI(GPIO, 11, 10, 9, 1, 8); /* for standard SPI pins */ typedef RP1_GPIO::pin_size_t pin_size_t; typedef enum { INPUT = RP1_GPIO::INPUT, OUTPUT = RP1_GPIO::OUTPUT, OPEN_DRAIN = RP1_GPIO::OPEN_DRAIN, OPEN_SOURCE = RP1_GPIO::OPEN_SOURCE, DISABLE = RP1_GPIO::DISABLE, INPUT_PULLUP, INPUT_PULLDOWN, } PinMode; typedef enum { LOW = RP1_GPIO::LOW, HIGH = RP1_GPIO::HIGH, TOGGLE = RP1_GPIO::TOGGLE, IGNORE = RP1_GPIO::IGNORE, } PinStatus; inline bool arduino(void) { return GPIO.begin() != GPIO.CHIP_UNKNOWN; } inline void pinMode(pin_size_t pin, PinMode mode, PinStatus value = IGNORE) { switch (mode) { case INPUT: GPIO.setPullUpDown(GPIO.PULL_DISABLE); GPIO.pinMode(pin, GPIO.INPUT, (RP1_GPIO::PinStatus)value); break; case INPUT_PULLUP: GPIO.setPullUpDown(GPIO.PULL_UP); GPIO.pinMode(pin, GPIO.INPUT, (RP1_GPIO::PinStatus)value); break; case INPUT_PULLDOWN: GPIO.setPullUpDown(GPIO.PULL_DOWN); GPIO.pinMode(pin, GPIO.INPUT, (RP1_GPIO::PinStatus)value); break; case OUTPUT: GPIO.pinMode(pin, GPIO.OUTPUT, (RP1_GPIO::PinStatus)value); break; case OPEN_DRAIN: GPIO.setPullUpDown(GPIO.PULL_UP); GPIO.pinMode(pin, GPIO.OPEN_DRAIN, (RP1_GPIO::PinStatus)value); break; case OPEN_SOURCE: GPIO.setPullUpDown(GPIO.PULL_DOWN); GPIO.pinMode(pin, GPIO.OPEN_SOURCE, (RP1_GPIO::PinStatus)value); break; case DISABLE: GPIO.pinMode(pin, GPIO.DISABLE, (RP1_GPIO::PinStatus)value); break; } } inline void digitalWrite(pin_size_t pin, int value) { GPIO.digitalWrite(pin, (RP1_GPIO::PinStatus)value); } inline int digitalRead(pin_size_t pin, bool sync = false) { return GPIO.digitalRead(pin, sync); } inline void analogWrite(pin_size_t pin, unsigned long value, unsigned long maxres = 255, unsigned long freq = 1000) { unsigned long range = GPIO.PWM0_FREQ / freq; GPIO.analogWrite(pin, range, (unsigned long long)range * (value > maxres ? maxres : value) / maxres); } inline unsigned long nanos(void) { return GPIO.nanos(); } inline unsigned long micros(void) { return GPIO.micros(); } inline unsigned long millis(void) { return GPIO.millis(); } inline void delayNanoseconds(unsigned long ns) { GPIO.delayNanoseconds(ns); } inline void delayMicroseconds(unsigned long us) { GPIO.delayMicroseconds(us); } inline void delay(unsigned long ms) { GPIO.delay(ms); } inline void yield(void) { GPIO.yield(); } |