Skip to content

Implement Common #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ SOFTWARE.

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

#ifndef _ARDUINO_H_
#define _ARDUINO_H_
#ifndef _ARDUINO_MBED_BRIDGE_ARDUINO_H_
#define _ARDUINO_MBED_BRIDGE_ARDUINO_H_

#include "mbed.h"

#endif // _ARDUINO_H_
#define PinMode Arduino_PinMode // note: this changes the Arduino API for mbed compatibility - use Arduino_PinMode where PinMode was specified in the Arduino API
#include "core-api/api/ArduinoAPI.h"
#undef PinMode

#include "core-extend/ArduinoAPI.h"

#include "bridge/pins.h"

#include "variant.h" // user-supplied

#endif // _ARDUINO_MBED_BRIDGE_ARDUINO_H_
61 changes: 61 additions & 0 deletions bridge/pins.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (c) 2020 SparkFun Electronics

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "bridge/pins.h"

pin_size_t pinIndexByName(PinName name){
pin_size_t index = 0;
while(index < variantPinCount){
if(variantPinStates[index].name == name){ return index; }
index++;
}
return variantPinCount;
}

pin_size_t pinIndexByNumber(pin_size_t number){
pin_size_t index = 0;
while(index < variantPinCount){
if(variantPinStates[index].number == number){ return index; }
index++;
}
return variantPinCount;
}

pin_size_t pinNumberByIndex(pin_size_t index){
if(index >= variantPinCount){ return (pin_size_t)NC; }
return variantPinStates[index].number;
}

pin_size_t pinNumberByName(PinName name){
pin_size_t index = pinIndexByName(name);
return pinNumberByIndex(index);
}

PinName pinNameByIndex(pin_size_t index){
if(index >= variantPinCount){ return NC; }
return variantPinStates[index].name;
}

PinName pinNameByNumber(pin_size_t number){
pin_size_t index = pinIndexByNumber(number);
return pinNameByIndex(index);
}
56 changes: 56 additions & 0 deletions bridge/pins.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright (c) 2020 SparkFun Electronics

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef _ARDUINO_MBED_BRIDGE_BRIDGE_PINS_H_
#define _ARDUINO_MBED_BRIDGE_BRIDGE_PINS_H_

#include "Arduino.h"

typedef struct _PinState {
PinName name;
pin_size_t number;
arduino::InterruptInParam* irq;
// PwmOut* pwm; // todo: implement this
// AnalogOut* dac; // todo: implement this
// AnalogIn* adc; // todo: implement this
DigitalInOut* gpio;
} PinState;

pin_size_t pinIndexByName(PinName name);
pin_size_t pinIndexByNumber(pin_size_t number);

pin_size_t pinNumberByIndex(pin_size_t index);
pin_size_t pinNumberByName(PinName name);

PinName pinNameByIndex(pin_size_t index);
PinName pinNameByNumber(pin_size_t number);

#define pinIRQByIndex(I) variantPinStates[I].irq
#define pinPWMByIndex(I) variantPinStates[I].pwm
#define pinDACByIndex(I) variantPinStates[I].dac
#define pinADCByIndex(I) variantPinStates[I].adc
#define pinGPIOByIndex(I) variantPinStates[I].gpio

extern const pin_size_t variantPinCount;
extern PinState variantPinStates[];

#endif // _ARDUINO_MBED_BRIDGE_BRIDGE_PINS_H_
28 changes: 28 additions & 0 deletions core-extend/ArduinoAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright (c) 2020 SparkFun Electronics

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_
#define _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_

#include "core-extend/Common.h"

#endif // _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_
143 changes: 143 additions & 0 deletions core-extend/Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright (c) 2020 SparkFun Electronics

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef _ARDUINO_MBED_BRIDGE_CORE_EXTEND_COMMON_H_
#define _ARDUINO_MBED_BRIDGE_CORE_EXTEND_COMMON_H_

#include "mbed.h"

#define PinMode Arduino_PinMode // note: this changes the Arduino API for mbed compatibility - use Arduino_PinMode where PinMode was specified in the Arduino API
#include "core-api/api/Common.h"
#undef PinMode

void indexPinMode(pin_size_t index, Arduino_PinMode pinMode);
void pinMode(PinName pinName, Arduino_PinMode pinMode);

void indexDigitalWrite(pin_size_t index, PinStatus val);
void digitalWrite(PinName pinName, PinStatus val);

PinStatus indexDigitalRead(pin_size_t index);
PinStatus digitalRead(PinName pinName);

int indexAnalogRead(pin_size_t index);
int analogRead(PinName pinName);

void indexAnalogWriteDAC(pin_size_t index, int val);
void analogWriteDAC(PinName pinName, int val);
void analogWriteDAC(pin_size_t pinNumber, int val);

void indexAnalogWrite(pin_size_t index, int val);
void analogWrite(PinName pinName, int val);

void indexShiftOut(pin_size_t data_index, pin_size_t clock_index, BitOrder bitOrder, uint8_t val);
void shiftOut(PinName dataPinName, PinName clockPinName, BitOrder bitOrder, uint8_t val);

pin_size_t indexShiftIn(pin_size_t data_index, pin_size_t clock_index, BitOrder bitOrder);
pin_size_t shiftIn(PinName dataPinName, PinName clockPinName, BitOrder bitOrder);

void indexAttachInterrupt(pin_size_t index, voidFuncPtr callback, PinStatus mode);
void attachInterrupt(PinName pinName, voidFuncPtr callback, PinStatus mode);

void indexAttachInterruptParam(pin_size_t index, voidFuncPtrParam callback, PinStatus mode, void* param);
void attachInterruptParam(PinName pinName, voidFuncPtrParam callback, PinStatus mode, void* param);

void indexDetachInterrupt(pin_size_t index);
void detachInterrupt(PinName pinName);

#ifdef __cplusplus

void indexTone(pin_size_t index, unsigned int frequency, unsigned long duration = 0);
unsigned long indexPulseIn(pin_size_t index, uint8_t state, unsigned long timeout = 1000000L);
unsigned long pulseIn(PinName pinName, uint8_t state, unsigned long timeout = 1000000L);
unsigned long indexPulseInLong(pin_size_t index, uint8_t state, unsigned long timeout = 1000000L);
unsigned long pulseInLong(PinName pinName, uint8_t state, unsigned long timeout = 1000000L);

#if DEVICE_INTERRUPTIN

namespace arduino {

class InterruptInParam : public mbed::InterruptIn {
private:
protected:
public:
/** Create an InterruptIn connected to the specified pin
*
* @param pin InterruptIn pin to connect to
*/
InterruptInParam(PinName pin);

/** Create an InterruptIn connected to the specified pin,
* and the pin configured to the specified mode.
*
* @param pin InterruptIn pin to connect to
* @param mode Desired Pin mode configuration.
* (Valid values could be PullNone, PullDown, PullUp and PullDefault.
* See PinNames.h for your target for definitions)
*
*/
InterruptInParam(PinName pin, PinMode mode);

virtual ~InterruptInParam();

/** Attach a function to call when a rising edge occurs on the input
*
* @param func A pointer to a void function with argument, or 0 to set as none
*/
void rise(Callback<void(void*)> func, void* param);

/** Attach a function to call when a rising edge occurs on the input
*
* @param func A pointer to a void function, or 0 to set as none
*/
void rise(Callback<void()> func); // shadows InterruptIn::rise

/** Attach a function to call when a falling edge occurs on the input
*
* @param func A pointer to a void function with argument, or 0 to set as none
*/
void fall(Callback<void(void*)> func, void* param);

/** Attach a function to call when a falling edge occurs on the input
*
* @param func A pointer to a void function, or 0 to set as none
*/
void fall(Callback<void()> func); // shadows InterruptIn::fall

static void _irq_handler(uint32_t id, gpio_irq_event event);

protected:
Callback<void(void*)> _rise; // shadows InterruptIn::_rise
Callback<void(void*)> _fall; // shadows InterruptIn::_fall

void* _rise_param = nullptr;
void* _fall_param = nullptr;

void irq_init(PinName pin); // shadows InterruptIn::irq_init
};

} // namespace arduio

#endif //DEVICE_INTERRUPTIN

#endif // __cplusplus

#endif // _ARDUINO_MBED_BRIDGE_CORE_EXTEND_COMMON_H_
Loading