Smartcar Shield
ArduinoRuntime.cpp
Go to the documentation of this file.
1 #include "ArduinoRuntime.hpp"
2 #include <Arduino.h>
3 #include <Wire.h>
4 #include <math.h>
5 #ifdef ESP_BOARD
6 #if defined(ESP32)
7 #include "analogWrite.h"
8 #endif
9 #if defined(ESP8266)
10 #define TEN_BIT_ANALOG
11 #include "../../utilities/Utilities.hpp"
12 #endif
13 #endif
14 
15 void ArduinoRuntime::setPinDirection(uint8_t pin, uint8_t direction)
16 {
17  pinMode(pin, direction);
18 }
19 
20 void STORED_IN_RAM ArduinoRuntime::setPinState(uint8_t pin, uint8_t state)
21 {
22  digitalWrite(pin, state);
23 }
24 
26 {
27  return digitalRead(pin);
28 }
29 
31 {
32  return analogRead(pin);
33 }
34 
35 void ArduinoRuntime::setPWM(uint8_t pin, int dutyCycle)
36 {
37 #ifdef TEN_BIT_ANALOG
38  dutyCycle = smartcarlib::utils::getMap(dutyCycle, 0, 255, 0, 1023);
39 #endif
40  analogWrite(pin, dutyCycle);
41 }
42 
44 {
45  Wire.begin();
46 }
47 
49 {
50  Wire.beginTransmission(address);
51 }
52 
53 size_t ArduinoRuntime::i2cWrite(uint8_t value)
54 {
55  return Wire.write(value);
56 }
57 
59 {
60  return Wire.endTransmission();
61 }
62 
63 uint8_t ArduinoRuntime::i2cRequestFrom(uint8_t address, uint8_t numberOfBytes)
64 {
65  return Wire.requestFrom(address, numberOfBytes);
66 }
67 
69 {
70  return Wire.available();
71 }
72 
74 {
75  return Wire.read();
76 }
77 
78 int8_t ArduinoRuntime::pinToInterrupt(uint8_t pin)
79 {
80  return digitalPinToInterrupt(pin);
81 }
82 
84 {
85  return millis();
86 }
87 
89 {
90  return micros();
91 }
92 
93 void ArduinoRuntime::delayMillis(unsigned long milliseconds)
94 {
95  delay(milliseconds);
96 }
97 
98 void ArduinoRuntime::delayMicros(unsigned int microseconds)
99 {
100  delayMicroseconds(microseconds);
101 }
102 
103 unsigned long ArduinoRuntime::getPulseDuration(uint8_t pin, uint8_t state, unsigned long timeout)
104 {
105  return pulseIn(pin, state, timeout);
106 }
107 
108 void ArduinoRuntime::setInterrupt(uint8_t pin, InterruptCallback callback, int mode)
109 {
110  attachInterrupt(pin, callback, mode);
111 }
112 
114 {
115  return LOW;
116 }
117 
119 {
120  return HIGH;
121 }
122 
124 {
125  return OUTPUT;
126 }
127 
129 {
130  return INPUT;
131 }
132 
134 {
135  return RISING;
136 }
ArduinoRuntime::pinToInterrupt
int8_t pinToInterrupt(uint8_t pin) override
Gets the interrupt number of the specified pin, equivalent of digitalPinToInterrupt in Arduino.
Definition: ArduinoRuntime.cpp:78
ArduinoRuntime::setPinState
void setPinState(uint8_t pin, uint8_t state) override
Set pin state, equivalent of digitalWrite in Arduino.
Definition: ArduinoRuntime.cpp:20
ArduinoRuntime::delayMicros
void delayMicros(unsigned int microseconds) override
Block the execution for the specified number of microseconds, equivalent of delayMicroseconds in Ardu...
Definition: ArduinoRuntime.cpp:98
ArduinoRuntime::getOutputState
uint8_t getOutputState() const override
Get the runtime-specific value representing an OUTPUT pin state.
Definition: ArduinoRuntime.cpp:123
ArduinoRuntime::setPWM
void setPWM(uint8_t pin, int dutyCycle) override
Set PWM state, equivalent of analogWrite in Arduino.
Definition: ArduinoRuntime.cpp:35
InterruptCallback
A callback to be invoked. Depending on the platform different callback types may be necessary.
ArduinoRuntime::i2cRequestFrom
uint8_t i2cRequestFrom(uint8_t address, uint8_t numberOfBytes) override
Request a number of bytes from the specified I2C slave, equivalent of Wire.requestFrom in Arduino.
Definition: ArduinoRuntime.cpp:63
ArduinoRuntime::getHighState
uint8_t getHighState() const override
Get the runtime-specific value representing a logical HIGH voltage state.
Definition: ArduinoRuntime.cpp:118
ArduinoRuntime.hpp
ArduinoRuntime::i2cBeginTransmission
void i2cBeginTransmission(uint8_t address) override
Initiate a transmission to the specified I2C slave device, equivalent of Wire.beginTransmission in Ar...
Definition: ArduinoRuntime.cpp:48
ArduinoRuntime::setPinDirection
void setPinDirection(uint8_t pin, uint8_t direction) override
Set pin direction, equivalent of pinMode in Arduino.
Definition: ArduinoRuntime.cpp:15
ArduinoRuntime::getPulseDuration
unsigned long getPulseDuration(uint8_t pin, uint8_t state, unsigned long timeout) override
Gets the incomming pulse length in microseconds starting from the nearest state, equivalent to pulseI...
Definition: ArduinoRuntime.cpp:103
ArduinoRuntime::i2cAvailable
int i2cAvailable() override
Gets the number of bytes available to be retrieved, equivalent of Wire.availableO in Arduino.
Definition: ArduinoRuntime.cpp:68
ArduinoRuntime::currentTimeMillis
unsigned long currentTimeMillis() override
Gets the amount of milliseconds since the microcontroller started running, equivalent of millis in Ar...
Definition: ArduinoRuntime.cpp:83
ArduinoRuntime::getAnalogPinState
int getAnalogPinState(uint8_t pin) override
Get pin's analog (ADC) reading, equivalent of analogRead in Arduino.
Definition: ArduinoRuntime.cpp:30
ArduinoRuntime::i2cEndTransmission
uint8_t i2cEndTransmission() override
Ends a transmission to an I2C device equivalent of Wire.endTransmission in Arduino.
Definition: ArduinoRuntime.cpp:58
smartcarlib::utils::getMap
constexpr AnyNumber getMap(AnyNumber valueToMap, AnyNumber fromLow, AnyNumber fromHigh, AnyNumber toLow, AnyNumber toHigh)
Maps a value from a range to another.
Definition: Utilities.hpp:104
ArduinoRuntime::i2cWrite
size_t i2cWrite(uint8_t value) override
Send the specified byte via i2c, equivalent of Wire.write in Arduino.
Definition: ArduinoRuntime.cpp:53
ArduinoRuntime::i2cRead
int i2cRead() override
Reads a byte from I2C bus, equivalent of Wire.read in Arduino.
Definition: ArduinoRuntime.cpp:73
ArduinoRuntime::i2cInit
void i2cInit() override
Initialize I2C bus as master, equivalent of Wire.begin in Arduino.
Definition: ArduinoRuntime.cpp:43
ArduinoRuntime::getInputState
uint8_t getInputState() const override
Get the runtime-specific value representing an INPUT pin state.
Definition: ArduinoRuntime.cpp:128
ArduinoRuntime::currentTimeMicros
unsigned long currentTimeMicros() override
Gets the amount of microseconds since the microcontroller started running, equivalent of micros in Ar...
Definition: ArduinoRuntime.cpp:88
ArduinoRuntime::getLowState
uint8_t getLowState() const override
Get the runtime-specific value representing a logical LOW voltage state.
Definition: ArduinoRuntime.cpp:113
ArduinoRuntime::getRisingEdgeMode
int getRisingEdgeMode() const override
Get the rising edge constant for setting an interrupt.
Definition: ArduinoRuntime.cpp:133
ArduinoRuntime::delayMillis
void delayMillis(unsigned long milliseconds) override
Block the execution for the specified number of milliseconds, equivalent of delay in Arduino.
Definition: ArduinoRuntime.cpp:93
STORED_IN_RAM
#define STORED_IN_RAM
Definition: Runtime.hpp:32
ArduinoRuntime::setInterrupt
void setInterrupt(uint8_t pin, InterruptCallback callback, int mode) override
Enables an external hardware interrupt and provides a callback, equivalent to attachInterrupt in Ardu...
Definition: ArduinoRuntime.cpp:108
ArduinoRuntime::getPinState
int getPinState(uint8_t pin) override
Get the pin state, equivalent of digitalRead in Arduino.
Definition: ArduinoRuntime.cpp:25