arduino-emulator
Loading...
Searching...
No Matches
I2CWrapper.h
1/*
2 I2CWrapper.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21#include "Sources.h"
22#include "api/HardwareI2C.h"
23
24namespace arduino {
25
51class I2CWrapper : public HardwareI2C {
52 public:
53 I2CWrapper() = default;
55 I2CWrapper(HardwareI2C& i2c) { setI2C(&i2c); }
56 ~I2CWrapper() = default;
57 void begin();
58 void begin(uint8_t address);
59 void end();
60 void setClock(uint32_t freq);
61 void beginTransmission(uint8_t address);
62 uint8_t endTransmission(bool stopBit);
63 uint8_t endTransmission(void);
64 size_t requestFrom(uint8_t address, size_t len, bool stopBit);
65 size_t requestFrom(uint8_t address, size_t len);
66 void onReceive(void (*)(int));
67 void onRequest(void (*)(void));
68 size_t write(uint8_t);
69 int available();
70 int read();
71 int peek();
72
74 void setI2C(HardwareI2C* i2c) {
75 p_i2c = i2c;
76 p_source = nullptr;
77 }
80 p_source = source;
81 p_i2c = nullptr;
82 }
83
84 protected:
85 HardwareI2C* p_i2c = nullptr;
86 I2CSource* p_source = nullptr;
87
88 HardwareI2C* getI2C() {
89 HardwareI2C* result = p_i2c;
90 if (result == nullptr && p_source != nullptr) {
91 result = p_source->getI2C();
92 }
93 return result;
94 }
95};
96
98extern I2CWrapper Wire;
99
102
103} // namespace arduino
Definition DMAPool.h:103
Definition HardwareI2C.h:28
Abstract interface for providing I2C hardware implementations.
Definition Sources.h:36
I2C wrapper class that provides flexible hardware abstraction.
Definition I2CWrapper.h:51
void setSource(I2CSource *source)
alternatively defines a class that provides the I2C implementation
Definition I2CWrapper.h:79
void setI2C(HardwareI2C *i2c)
defines the i2c implementation: use nullptr to reset.
Definition I2CWrapper.h:74
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
I2CWrapper Wire
Global Wire instance used by Arduino API functions and direct access.
Definition I2CWrapper.cpp:24