Skip to content

Commit

Permalink
Polyfilling mbed for extensions (#13)
Browse files Browse the repository at this point in the history
Introduce minimal mbed API compatibility 

  - introduce to support backward compatibility for v1 C/C++ based vendor extensions that used mbed APIs
  • Loading branch information
microbit-sam committed Nov 6, 2020
1 parent bea9f66 commit cfc40a6
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 0 deletions.
44 changes: 44 additions & 0 deletions inc/compat/DigitalIn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef DigitalIn_h
#define DigitalIn_h

#include "Pin.h"

class DigitalIn: public codal::NRF52Pin {
public:
DigitalIn(PinName pin) : codal::NRF52Pin(pin, pin, PIN_CAPABILITY_DIGITAL)
{
}

void mode(PullMode pull) {
this->setPull(pull);
}

void mode(int pull) {
switch(pull) {
case PullNone:
this->setPull(PullMode::None);
break;
case PullDown:
this->setPull(PullMode::Down);
break;
case PullUp:
this->setPull(PullMode::Up);
break;
default:
this->setPull(PullMode::Up);
break;
}
}

int read() {
return this->getDigitalValue();
}

operator int() {
return this->getDigitalValue();
}
};

#warning "Use of mbed with CODAL is not recommended! These classes will not always behave as expected and are provided to attempt to support existing extensions. Please write your extension using CODAL."

#endif
56 changes: 56 additions & 0 deletions inc/compat/DigitalOut.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef DigitalOut_h
#define DigitalOut_h

#include "Pin.h"

class DigitalOut: public codal::NRF52Pin {
public:
DigitalOut(PinName pin) : codal::NRF52Pin(pin, pin, PIN_CAPABILITY_DIGITAL)
{
}

void mode(PullMode pull) {
this->setPull(pull);
}

void mode(int pull) {
switch(pull) {
case PullNone:
this->setPull(PullMode::None);
break;
case PullDown:
this->setPull(PullMode::Down);
break;
case PullUp:
this->setPull(PullMode::Up);
break;
default:
this->setPull(PullMode::Up);
break;
}
}

int read() {
return this->getDigitalValue();
}

void write(int value) {
this->setDigitalValue(value);
return;
}

DigitalOut &operator= (int value) {
this->write(value);
return *this;
}

DigitalOut &operator= (DigitalOut &rhs);

operator int() {
return this->getDigitalValue();
}
};

#warning "Use of mbed with CODAL is not recommended! These classes will not always behave as expected and are provided to attempt to support existing extensions. Please write your extension using CODAL."

#endif
71 changes: 71 additions & 0 deletions inc/compat/InterruptIn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef InterruptIn_h
#define InterruptIn_h

/* uBit object from PXT or CODAL
* TODO: MICROBIT_DAL_PXT will be added to PXT to replace this define */
#ifdef MICROBIT_DAL_FIBER_USER_DATA
#include "pxt.h"
#else
extern MicroBit uBit;
#endif

#include "MicroBitCompat.h"
#include "Pin.h"
#include "MbedMemberFunctionCallback.h"
#include "MicroBitEvent.h"

class InterruptIn {

MbedMemberFunctionCallback *_rise;
MbedMemberFunctionCallback *_fall;
NRF52Pin *p;

public:
InterruptIn(PinName pin) {
p = new NRF52Pin(MICROBIT_ID_MBED_INTERRUPT_IN, pin, PIN_CAPABILITY_DIGITAL);
p->eventOn(MICROBIT_PIN_EVENT_ON_EDGE);
return;
}

void mode(int pull) {
switch(pull) {
case PullNone:
p->setPull(PullMode::None);
break;
case PullDown:
p->setPull(PullMode::Down);
break;
case PullUp:
p->setPull(PullMode::Up);
break;
default:
p->setPull(PullMode::Up);
break;
}
}

void onFall(MicroBitEvent e) {
_fall->fire();
}

template<typename T>
void fall(T* tptr, void (T::*mptr)(void)) {
this->_fall = new MbedMemberFunctionCallback(tptr, mptr);
uBit.messageBus.listen(MICROBIT_ID_MBED_INTERRUPT_IN, MICROBIT_PIN_EVT_FALL, this, &InterruptIn::onFall);
}

void onRise(MicroBitEvent e) {
_rise->fire();
}

template<typename T>
void rise(T* tptr, void (T::*mptr)(void)) {
this->_rise = new MbedMemberFunctionCallback(tptr, mptr);
uBit.messageBus.listen(MICROBIT_ID_MBED_INTERRUPT_IN, MICROBIT_PIN_EVT_RISE, this, &InterruptIn::onRise);
}

};

#warning "Use of mbed with CODAL is not recommended! These classes will not always behave as expected and are provided to attempt to support existing extensions. Please write your extension using CODAL."

#endif
114 changes: 114 additions & 0 deletions inc/compat/MbedMemberFunctionCallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
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 MBEDMEMBERFUNCTION_CALLBACK_H
#define MBEDMEMBERFUNCTION_CALLBACK_H

#include "CodalConfig.h"
#include "CodalCompat.h"

/**
* Class definition for a MbedMemberFunctionCallback.
*
* C++ member functions (also known as methods) have a more complex
* representation than normal C functions. This class allows a reference to
* a C++ member function to be stored then called at a later date.
*
* This class is used extensively by the DeviceMessageBus to deliver
* events to C++ methods.
*/
namespace codal
{
class MbedMemberFunctionCallback
{
private:
void* object;
uint32_t method[4];
void (*mbed_invoke)(void *object, uint32_t *method);
template <typename T> static void methodCall(void* object, uint32_t*method);

public:

/**
* Constructor. Creates a MbedMemberFunctionCallback based on a pointer to given method.
*
* @param object The object the callback method should be invooked on.
*
* @param method The method to invoke.
*/
template <typename T> MbedMemberFunctionCallback(T* object, void (T::*method)());

/**
* A comparison of two MbedMemberFunctionCallback objects.
*
* @return true if the given MbedMemberFunctionCallback is equivalent to this one, false otherwise.
*/
bool operator==(const MbedMemberFunctionCallback &mfc);

/**
* Calls the method reference held by this MbedMemberFunctionCallback.
*
*/
void fire();
};

/**
* Constructor. Creates a MbedMemberFunctionCallback based on a pointer to given method.
*
* @param object The object the callback method should be invooked on.
*
* @param method The method to invoke.
*/
template <typename T>
MbedMemberFunctionCallback::MbedMemberFunctionCallback(T* object, void (T::*method)())
{
this->object = object;
memclr(this->method, sizeof(this->method));
memcpy(this->method, &method, sizeof(method));
mbed_invoke = &MbedMemberFunctionCallback::methodCall<T>;
}

/**
* A template used to create a static method capable of invoking a C++ member function (method)
* based on the given parameters.
*
* @param object The object the callback method should be invooked on.
*
* @param method The method to invoke.
*
*/
template <typename T>
void MbedMemberFunctionCallback::methodCall(void *object, uint32_t *method)
{
T* o = (T*)object;
void (T::*m)();
memcpy(&m, method, sizeof(m));

(o->*m)();
}
}

#warning "Use of mbed with CODAL is not recommended! These classes will not always behave as expected and are provided to attempt to support existing extensions. Please write your extension using CODAL."

#endif
62 changes: 62 additions & 0 deletions inc/compat/MbedTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef MbedTimer_h
#define MbedTimer_h

class MbedTimer {
private:
uint32_t _start;
uint32_t _stop = 0;
public:
void start() {
_start = system_timer_current_time_us();
}

void stop() {
_stop = system_timer_current_time_us();
}

void reset() {
_start = system_timer_current_time_us();
}

float read() {
if(_stop > 0) {
return (_stop - _start) / 1000000.0;
} else {
return (system_timer_current_time_us() - _start) / 1000000.0;
}

}

int read_ms() {
if(_stop > 0) {
return (_stop - _start) / 1000;
} else {
return (system_timer_current_time_us() - _start) / 1000;
}
}

int read_us() {
if(_stop > 0) {
return system_timer_current_time_us() - _start;
} else {
return system_timer_current_time_us() - _start;
}
}

operator float() {
return read();
}

int read_high_resolution_us() {
return read_us();
}

int elapsed_time() {
return read_ms();
}

};

#warning "Use of mbed with CODAL is not recommended! These classes will not always behave as expected and are provided to attempt to support existing extensions. Please write your extension using CODAL."

#endif
5 changes: 5 additions & 0 deletions inc/compat/MicroBitCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ typedef codal::EventLaunchMode MicroBitEventLaunchMode;

#define MICROBIT_ID_VIRTUAL_SPEAKER_PIN 39

#define MICROBIT_ID_MBED_INTERRUPT_IN 40
#define MICROBIT_ID_MBED_PWM 41
#define MICROBIT_ID_MBED_TIMEOUT 42
#define MICROBIT_ID_MBED_TICKER 43

#define MICROBIT_MAXIMUM_HEAPS DEVICE_MAXIMUM_HEAPS
#define MICROBIT_NESTED_HEAP_SIZE 0
#define MICROBIT_PANIC_HEAP_FULL DEVICE_PANIC_HEAP_FULL
Expand Down
29 changes: 29 additions & 0 deletions inc/compat/PwmOut.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef PwmOut_h
#define PwmOut_h

#include "MicroBitCompat.h"
#include "MicroBitIO.h"
#include "NRF52Pin.h"

class PwmOut {

private:
NRF52Pin p;

public:
PwmOut(PinName pin) : p(MICROBIT_ID_MBED_PWM, pin, PIN_CAPABILITY_ANALOG) {
return;
}

void write(float width) {
p.setAnalogValue(width * 1024.0);
}

void period_us(float period) {
p.setAnalogPeriod(period);
}
};

#warning "Use of mbed with CODAL is not recommended! These classes will not always behave as expected and are provided to attempt to support existing extensions. Please write your extension using CODAL."

#endif
Loading

0 comments on commit cfc40a6

Please sign in to comment.