Skip to content

Commit bde1be9

Browse files
authored
Wrapper classes for GPIO, SPI and I2C (#12)
* Clean Wrapper implementations for SPI, I2C and GPIO * RPI: compile errors * RPI: error corrections: extern SPI, Wire, GPIO * RPI: expect SPI, I2C begin in sketch * Remove RPI examples * remove rasperry-pi examples
1 parent 6fafeb9 commit bde1be9

File tree

78 files changed

+1730
-1554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1730
-1554
lines changed
Lines changed: 64 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,113 @@
1+
#include "Arduino.h"
2+
3+
#include <stdio.h>
4+
5+
#include <chrono>
16
#include <cstring>
2-
#include <chrono>
37
#include <ctime>
4-
#include <stdio.h>
58
#include <thread>
6-
#include <chrono>
7-
#include "Arduino.h"
9+
10+
#include "GPIOWrapper.h"
11+
#include "HardwareGPIO.h"
12+
#include "RemoteSerial.h"
813
#include "api/Common.h"
9-
#include "api/Stream.h"
1014
#include "api/PluggableUSB.h"
11-
#include "RemoteSerial.h"
12-
#include "Hardware.h"
15+
#include "api/Stream.h"
1316
#if !defined(SKIP_HARDWARE_SETUP)
14-
# include "HardwareSetupRemote.h"
17+
#include "HardwareSetupRemote.h"
1518
#endif
1619
#if !defined(SKIP_HARDWARE_WIFI)
17-
# include "WiFi.h"
18-
# include "WiFiClient.h"
20+
#include "WiFi.h"
21+
#include "WiFiClient.h"
1922
#endif
2023
#if defined(USE_RPI)
21-
# include "HardwareGPIO_RPI.h"
22-
# include "HardwareI2C_RPI.h"
24+
#include "HardwareGPIO_RPI.h"
25+
#include "HardwareI2C_RPI.h"
2326
#endif
24-
#include "api/PluggableUSB.h"
25-
#include "api/deprecated-avr-comp/avr/dtostrf.h"
2627
#include "ArduinoLogger.h"
27-
#include "StdioDevice.h"
2828
#include "Serial.h"
29+
#include "StdioDevice.h"
30+
#include "api/PluggableUSB.h"
31+
#include "api/deprecated-avr-comp/avr/dtostrf.h"
2932

3033
namespace arduino {
3134

3235
#if !defined(SKIP_HARDWARE_WIFI)
33-
WifiMock WiFi; // So that we can use the WiFi
36+
WifiMock WiFi; // So that we can use the WiFi
3437
#endif
3538

39+
// static PluggableUSB_ obj;
40+
PluggableUSB_::PluggableUSB_() {}
3641

37-
//static PluggableUSB_ obj;
38-
PluggableUSB_::PluggableUSB_(){}
42+
} // namespace arduino
3943

40-
}
41-
42-
4344
// sleep ms milliseconds
44-
void delay(unsigned long ms){
45-
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
45+
void delay(unsigned long ms) {
46+
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
4647
}
4748

4849
// sleep us milliseconds
49-
void delayMicroseconds(unsigned int us){
50-
std::this_thread::sleep_for(std::chrono::microseconds(us));
50+
void delayMicroseconds(unsigned int us) {
51+
std::this_thread::sleep_for(std::chrono::microseconds(us));
5152
}
5253

5354
// double to string conversion -> we can use sprintf which is complete in linux
54-
char *dtostrf(double val, signed char width, unsigned char prec, char *sout){
55-
sprintf(sout,"%*.*lf",width,prec, val);
56-
return sout;
55+
char* dtostrf(double val, signed char width, unsigned char prec, char* sout) {
56+
sprintf(sout, "%*.*lf", width, prec, val);
57+
return sout;
5758
}
5859

5960
// Returns the number of milliseconds passed since epich
6061
unsigned long millis() {
61-
static uint64_t start = 0;
62-
using namespace std::chrono;
63-
// Get current time with precision of milliseconds
64-
auto now = time_point_cast<milliseconds>(system_clock::now());
65-
// sys_milliseconds is type time_point<system_clock, milliseconds>
66-
using sys_milliseconds = decltype(now);
67-
// Convert time_point to signed integral type
68-
auto result = now.time_since_epoch().count();
69-
if (start == 0) {
70-
start = result;
71-
}
72-
return result - start;
73-
62+
static uint64_t start = 0;
63+
using namespace std::chrono;
64+
// Get current time with precision of milliseconds
65+
auto now = time_point_cast<milliseconds>(system_clock::now());
66+
// sys_milliseconds is type time_point<system_clock, milliseconds>
67+
using sys_milliseconds = decltype(now);
68+
// Convert time_point to signed integral type
69+
auto result = now.time_since_epoch().count();
70+
if (start == 0) {
71+
start = result;
72+
}
73+
return result - start;
7474
}
7575

7676
// Returns the micros of milliseconds passed since epich
77-
unsigned long micros(void){
78-
using namespace std::chrono;
79-
// Get current time with precision of milliseconds
80-
auto now = time_point_cast<microseconds>(system_clock::now());
81-
// sys_milliseconds is type time_point<system_clock, milliseconds>
82-
using sys_milliseconds = decltype(now);
83-
// Convert time_point to signed integral type
84-
return now.time_since_epoch().count();
77+
unsigned long micros(void) {
78+
using namespace std::chrono;
79+
// Get current time with precision of milliseconds
80+
auto now = time_point_cast<microseconds>(system_clock::now());
81+
// sys_milliseconds is type time_point<system_clock, milliseconds>
82+
using sys_milliseconds = decltype(now);
83+
// Convert time_point to signed integral type
84+
return now.time_since_epoch().count();
8585
}
8686

87-
void pinMode(pin_size_t pinNumber, PinMode pinMode){
88-
if (Hardware.gpio != nullptr) {
89-
Hardware.gpio->pinMode(pinNumber,pinMode);
90-
}
87+
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
88+
GPIO.pinMode(pinNumber, pinMode);
9189
}
9290
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
93-
if (Hardware.gpio != nullptr) {
94-
Hardware.gpio->digitalWrite(pinNumber,status);
95-
}
91+
GPIO.digitalWrite(pinNumber, status);
9692
}
9793
PinStatus digitalRead(pin_size_t pinNumber) {
98-
if (Hardware.gpio != nullptr) {
99-
return Hardware.gpio->digitalRead(pinNumber);
100-
} else {
101-
return HIGH; //sumulate input pullup
102-
}
103-
}
104-
int analogRead(pin_size_t pinNumber){
105-
if (Hardware.gpio != nullptr) {
106-
return Hardware.gpio->analogRead(pinNumber);
107-
} else {
108-
return 0;
109-
}
110-
111-
}
112-
void analogReference(uint8_t mode){
113-
if (Hardware.gpio != nullptr) {
114-
Hardware.gpio->analogReference(mode);
115-
}
94+
return GPIO.digitalRead(pinNumber);
11695
}
96+
int analogRead(pin_size_t pinNumber) { return GPIO.analogRead(pinNumber); }
97+
void analogReference(uint8_t mode) { GPIO.analogReference(mode); }
11798
void analogWrite(pin_size_t pinNumber, int value) {
118-
if (Hardware.gpio != nullptr) {
119-
Hardware.gpio->analogWrite(pinNumber,value);
120-
}
99+
GPIO.analogWrite(pinNumber, value);
121100
}
122101
void tone(uint8_t pinNumber, unsigned int frequency, unsigned long duration) {
123-
if (Hardware.gpio != nullptr) {
124-
Hardware.gpio->tone(pinNumber,frequency,duration);
125-
}
126-
}
127-
void noTone(uint8_t pinNumber) {
128-
if (Hardware.gpio != nullptr) {
129-
Hardware.gpio->noTone(pinNumber);
130-
}
102+
GPIO.tone(pinNumber, frequency, duration);
131103
}
132-
unsigned long pulseIn(uint8_t pinNumber, uint8_t state, unsigned long timeout){
133-
if (Hardware.gpio != nullptr) {
134-
return Hardware.gpio->pulseIn(pinNumber, state, timeout);
135-
} else {
136-
return 0;
137-
}
104+
void noTone(uint8_t pinNumber) { GPIO.noTone(pinNumber); }
105+
unsigned long pulseIn(uint8_t pinNumber, uint8_t state, unsigned long timeout) {
106+
return GPIO.pulseIn(pinNumber, state, timeout);
138107
}
139-
unsigned long pulseInLong(uint8_t pinNumber, uint8_t state, unsigned long timeout){
140-
if (Hardware.gpio != nullptr) {
141-
return Hardware.gpio->pulseInLong(pinNumber, state, timeout);
142-
} else {
143-
return 0;
144-
}
108+
unsigned long pulseInLong(uint8_t pinNumber, uint8_t state,
109+
unsigned long timeout) {
110+
return GPIO.pulseInLong(pinNumber, state, timeout);
145111
}
146112

147-
void yield(){
148-
}
149-
150-
113+
void yield() {}

ArduinoCore-Linux/cores/arduino/Ethernet.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include <unistd.h> // for close
77

88
#include "ArduinoLogger.h"
9+
#include "RingBufferExt.h"
10+
#include "SocketImpl.h"
911
#include "api/Client.h"
1012
#include "api/Common.h"
1113
#include "api/IPAddress.h"
12-
#include "RingBufferExt.h"
13-
#include "SocketImpl.h"
1414

1515
namespace arduino {
1616

@@ -120,7 +120,6 @@ class EthernetClient : public Client {
120120
return connect(str.c_str(), port);
121121
}
122122

123-
124123
// opens a connection
125124
virtual int connect(const char* address, uint16_t port) override {
126125
Logger.info(WIFICLIENT, "connect");
@@ -213,7 +212,7 @@ class EthernetClient : public Client {
213212
return result;
214213
}
215214

216-
virtual size_t readBytes(char* buffer, size_t len) {
215+
virtual size_t readBytes(char* buffer, size_t len) {
217216
return read((uint8_t*)buffer, len);
218217
}
219218

0 commit comments

Comments
 (0)