Skip to content

Commit 78638d2

Browse files
author
Owen
authored
Merge pull request #1 from sparkfun/implement-Common
2 parents d80f6e5 + 606ce76 commit 78638d2

12 files changed

+972
-6
lines changed

Arduino.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ SOFTWARE.
2222

2323
// The "Arduino.h" header file is intended to only be included by C++ sources.
2424

25-
#ifndef _ARDUINO_H_
26-
#define _ARDUINO_H_
25+
#ifndef _ARDUINO_MBED_BRIDGE_ARDUINO_H_
26+
#define _ARDUINO_MBED_BRIDGE_ARDUINO_H_
2727

2828
#include "mbed.h"
2929

30-
#endif // _ARDUINO_H_
30+
#define PinMode Arduino_PinMode // note: this changes the Arduino API for mbed compatibility - use Arduino_PinMode where PinMode was specified in the Arduino API
31+
#include "core-api/api/ArduinoAPI.h"
32+
#undef PinMode
33+
34+
#include "core-extend/ArduinoAPI.h"
35+
36+
#include "bridge/pins.h"
37+
38+
#include "variant.h" // user-supplied
39+
40+
#endif // _ARDUINO_MBED_BRIDGE_ARDUINO_H_

bridge/pins.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "bridge/pins.h"
24+
25+
pin_size_t pinIndexByName(PinName name){
26+
pin_size_t index = 0;
27+
while(index < variantPinCount){
28+
if(variantPinStates[index].name == name){ return index; }
29+
index++;
30+
}
31+
return variantPinCount;
32+
}
33+
34+
pin_size_t pinIndexByNumber(pin_size_t number){
35+
pin_size_t index = 0;
36+
while(index < variantPinCount){
37+
if(variantPinStates[index].number == number){ return index; }
38+
index++;
39+
}
40+
return variantPinCount;
41+
}
42+
43+
pin_size_t pinNumberByIndex(pin_size_t index){
44+
if(index >= variantPinCount){ return (pin_size_t)NC; }
45+
return variantPinStates[index].number;
46+
}
47+
48+
pin_size_t pinNumberByName(PinName name){
49+
pin_size_t index = pinIndexByName(name);
50+
return pinNumberByIndex(index);
51+
}
52+
53+
PinName pinNameByIndex(pin_size_t index){
54+
if(index >= variantPinCount){ return NC; }
55+
return variantPinStates[index].name;
56+
}
57+
58+
PinName pinNameByNumber(pin_size_t number){
59+
pin_size_t index = pinIndexByNumber(number);
60+
return pinNameByIndex(index);
61+
}

bridge/pins.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#ifndef _ARDUINO_MBED_BRIDGE_BRIDGE_PINS_H_
24+
#define _ARDUINO_MBED_BRIDGE_BRIDGE_PINS_H_
25+
26+
#include "Arduino.h"
27+
28+
typedef struct _PinState {
29+
PinName name;
30+
pin_size_t number;
31+
arduino::InterruptInParam* irq;
32+
// PwmOut* pwm; // todo: implement this
33+
// AnalogOut* dac; // todo: implement this
34+
// AnalogIn* adc; // todo: implement this
35+
DigitalInOut* gpio;
36+
} PinState;
37+
38+
pin_size_t pinIndexByName(PinName name);
39+
pin_size_t pinIndexByNumber(pin_size_t number);
40+
41+
pin_size_t pinNumberByIndex(pin_size_t index);
42+
pin_size_t pinNumberByName(PinName name);
43+
44+
PinName pinNameByIndex(pin_size_t index);
45+
PinName pinNameByNumber(pin_size_t number);
46+
47+
#define pinIRQByIndex(I) variantPinStates[I].irq
48+
#define pinPWMByIndex(I) variantPinStates[I].pwm
49+
#define pinDACByIndex(I) variantPinStates[I].dac
50+
#define pinADCByIndex(I) variantPinStates[I].adc
51+
#define pinGPIOByIndex(I) variantPinStates[I].gpio
52+
53+
extern const pin_size_t variantPinCount;
54+
extern PinState variantPinStates[];
55+
56+
#endif // _ARDUINO_MBED_BRIDGE_BRIDGE_PINS_H_

core-extend/ArduinoAPI.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#ifndef _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_
24+
#define _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_
25+
26+
#include "core-extend/Common.h"
27+
28+
#endif // _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_

core-extend/Common.h

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#ifndef _ARDUINO_MBED_BRIDGE_CORE_EXTEND_COMMON_H_
24+
#define _ARDUINO_MBED_BRIDGE_CORE_EXTEND_COMMON_H_
25+
26+
#include "mbed.h"
27+
28+
#define PinMode Arduino_PinMode // note: this changes the Arduino API for mbed compatibility - use Arduino_PinMode where PinMode was specified in the Arduino API
29+
#include "core-api/api/Common.h"
30+
#undef PinMode
31+
32+
void indexPinMode(pin_size_t index, Arduino_PinMode pinMode);
33+
void pinMode(PinName pinName, Arduino_PinMode pinMode);
34+
35+
void indexDigitalWrite(pin_size_t index, PinStatus val);
36+
void digitalWrite(PinName pinName, PinStatus val);
37+
38+
PinStatus indexDigitalRead(pin_size_t index);
39+
PinStatus digitalRead(PinName pinName);
40+
41+
int indexAnalogRead(pin_size_t index);
42+
int analogRead(PinName pinName);
43+
44+
void indexAnalogWriteDAC(pin_size_t index, int val);
45+
void analogWriteDAC(PinName pinName, int val);
46+
void analogWriteDAC(pin_size_t pinNumber, int val);
47+
48+
void indexAnalogWrite(pin_size_t index, int val);
49+
void analogWrite(PinName pinName, int val);
50+
51+
void indexShiftOut(pin_size_t data_index, pin_size_t clock_index, BitOrder bitOrder, uint8_t val);
52+
void shiftOut(PinName dataPinName, PinName clockPinName, BitOrder bitOrder, uint8_t val);
53+
54+
pin_size_t indexShiftIn(pin_size_t data_index, pin_size_t clock_index, BitOrder bitOrder);
55+
pin_size_t shiftIn(PinName dataPinName, PinName clockPinName, BitOrder bitOrder);
56+
57+
void indexAttachInterrupt(pin_size_t index, voidFuncPtr callback, PinStatus mode);
58+
void attachInterrupt(PinName pinName, voidFuncPtr callback, PinStatus mode);
59+
60+
void indexAttachInterruptParam(pin_size_t index, voidFuncPtrParam callback, PinStatus mode, void* param);
61+
void attachInterruptParam(PinName pinName, voidFuncPtrParam callback, PinStatus mode, void* param);
62+
63+
void indexDetachInterrupt(pin_size_t index);
64+
void detachInterrupt(PinName pinName);
65+
66+
#ifdef __cplusplus
67+
68+
void indexTone(pin_size_t index, unsigned int frequency, unsigned long duration = 0);
69+
unsigned long indexPulseIn(pin_size_t index, uint8_t state, unsigned long timeout = 1000000L);
70+
unsigned long pulseIn(PinName pinName, uint8_t state, unsigned long timeout = 1000000L);
71+
unsigned long indexPulseInLong(pin_size_t index, uint8_t state, unsigned long timeout = 1000000L);
72+
unsigned long pulseInLong(PinName pinName, uint8_t state, unsigned long timeout = 1000000L);
73+
74+
#if DEVICE_INTERRUPTIN
75+
76+
namespace arduino {
77+
78+
class InterruptInParam : public mbed::InterruptIn {
79+
private:
80+
protected:
81+
public:
82+
/** Create an InterruptIn connected to the specified pin
83+
*
84+
* @param pin InterruptIn pin to connect to
85+
*/
86+
InterruptInParam(PinName pin);
87+
88+
/** Create an InterruptIn connected to the specified pin,
89+
* and the pin configured to the specified mode.
90+
*
91+
* @param pin InterruptIn pin to connect to
92+
* @param mode Desired Pin mode configuration.
93+
* (Valid values could be PullNone, PullDown, PullUp and PullDefault.
94+
* See PinNames.h for your target for definitions)
95+
*
96+
*/
97+
InterruptInParam(PinName pin, PinMode mode);
98+
99+
virtual ~InterruptInParam();
100+
101+
/** Attach a function to call when a rising edge occurs on the input
102+
*
103+
* @param func A pointer to a void function with argument, or 0 to set as none
104+
*/
105+
void rise(Callback<void(void*)> func, void* param);
106+
107+
/** Attach a function to call when a rising edge occurs on the input
108+
*
109+
* @param func A pointer to a void function, or 0 to set as none
110+
*/
111+
void rise(Callback<void()> func); // shadows InterruptIn::rise
112+
113+
/** Attach a function to call when a falling edge occurs on the input
114+
*
115+
* @param func A pointer to a void function with argument, or 0 to set as none
116+
*/
117+
void fall(Callback<void(void*)> func, void* param);
118+
119+
/** Attach a function to call when a falling edge occurs on the input
120+
*
121+
* @param func A pointer to a void function, or 0 to set as none
122+
*/
123+
void fall(Callback<void()> func); // shadows InterruptIn::fall
124+
125+
static void _irq_handler(uint32_t id, gpio_irq_event event);
126+
127+
protected:
128+
Callback<void(void*)> _rise; // shadows InterruptIn::_rise
129+
Callback<void(void*)> _fall; // shadows InterruptIn::_fall
130+
131+
void* _rise_param = nullptr;
132+
void* _fall_param = nullptr;
133+
134+
void irq_init(PinName pin); // shadows InterruptIn::irq_init
135+
};
136+
137+
} // namespace arduio
138+
139+
#endif //DEVICE_INTERRUPTIN
140+
141+
#endif // __cplusplus
142+
143+
#endif // _ARDUINO_MBED_BRIDGE_CORE_EXTEND_COMMON_H_

0 commit comments

Comments
 (0)