arduino-emulator
Loading...
Searching...
No Matches
HardwareI2C_RPI.h
1#pragma once
2/*
3 HardwareI2C_RPI.h
4 Copyright (c) 2025 Phil Schatzmann. All right reserved.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20#ifdef USE_RPI
21
22#include <fcntl.h> // for O_RDWR
23#include <unistd.h> // for open(), close(), etc.
24#include <vector>
25#include "api/HardwareI2C.h"
26#include "ArduinoLogger.h"
27
28namespace arduino {
29
41 public:
42 HardwareI2C_RPI(const char* device = "/dev/i2c-1") {
43 i2c_device = device;
44 }
46 end();
47 }
48 void begin() override;
49 void begin(uint8_t address) override;
50 void end() override;
51 void setClock(uint32_t freq) override;
52 void beginTransmission(uint8_t address) override;
53 uint8_t endTransmission(bool stopBit) override;
54 uint8_t endTransmission(void) { return endTransmission(true);};
55 size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
56 size_t requestFrom(uint8_t address, size_t len) override;
57 void onReceive(void (*)(int)) override;
58 void onRequest(void (*)(void)) override;
59 size_t write(uint8_t) override;
60 size_t write(const uint8_t*, size_t) override;
61 int available() override;
62 int read() override;
63 int peek() override;
64 void flush() override { fsync(i2c_fd);}
65
66 operator bool() { return is_open; }
67
68 private:
69 int i2c_fd = -1;
70 uint8_t current_address = 0;
71 uint32_t i2c_clock = 100000; // default 100kHz
72 std::vector<uint8_t> i2c_rx_buffer;
73 std::vector<uint8_t> i2c_tx_buffer;
74 int i2c_rx_pos = 0;
75 const char* i2c_device;
76 bool is_open = false;
77};
78
79} // namespace arduino
80
81#endif
Definition DMAPool.h:103
Implementation of I2C communication for Raspberry Pi using Linux I2C device interface.
Definition HardwareI2C_RPI.h:40
Definition HardwareI2C.h:28
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31