diff --git a/inc/compat/DigitalIn.h b/inc/compat/DigitalIn.h new file mode 100644 index 00000000..063982db --- /dev/null +++ b/inc/compat/DigitalIn.h @@ -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 diff --git a/inc/compat/DigitalOut.h b/inc/compat/DigitalOut.h new file mode 100644 index 00000000..1b89176d --- /dev/null +++ b/inc/compat/DigitalOut.h @@ -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 diff --git a/inc/compat/InterruptIn.h b/inc/compat/InterruptIn.h new file mode 100644 index 00000000..43e2ce09 --- /dev/null +++ b/inc/compat/InterruptIn.h @@ -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 + 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 + 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 diff --git a/inc/compat/MbedMemberFunctionCallback.h b/inc/compat/MbedMemberFunctionCallback.h new file mode 100644 index 00000000..ebcd169f --- /dev/null +++ b/inc/compat/MbedMemberFunctionCallback.h @@ -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 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 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 + 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; + } + + /** + * 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 + 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 diff --git a/inc/compat/MbedTimer.h b/inc/compat/MbedTimer.h new file mode 100644 index 00000000..c70c2874 --- /dev/null +++ b/inc/compat/MbedTimer.h @@ -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 diff --git a/inc/compat/MicroBitCompat.h b/inc/compat/MicroBitCompat.h index 77f6af8c..3db37cac 100644 --- a/inc/compat/MicroBitCompat.h +++ b/inc/compat/MicroBitCompat.h @@ -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 diff --git a/inc/compat/PwmOut.h b/inc/compat/PwmOut.h new file mode 100644 index 00000000..96a9b570 --- /dev/null +++ b/inc/compat/PwmOut.h @@ -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 diff --git a/inc/compat/Ticker.h b/inc/compat/Ticker.h new file mode 100644 index 00000000..aebf2704 --- /dev/null +++ b/inc/compat/Ticker.h @@ -0,0 +1,55 @@ +#ifndef Ticker_h +#define Ticker_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 "MbedMemberFunctionCallback.h" +#include "Timer.h" +#include "MicroBitEvent.h" + +class Ticker { + private: + uint32_t interval; + MbedMemberFunctionCallback *func; + + public: + + Ticker() { + } + + void onTick(MicroBitEvent e) { + func->fire(); + } + + template + void attach(T* tptr, void (T::*mptr)(void), float s) { + this->func = new MbedMemberFunctionCallback(tptr, mptr); + this->interval = (s * 1000000.0f); + uBit.messageBus.listen(MICROBIT_ID_MBED_TICKER, 0x0, this, &Ticker::onTick); + system_timer_event_every_us(interval, MICROBIT_ID_MBED_TICKER, 0x0); + } + + template + void attach_us(T* tptr, void (T::*mptr)(void), int us) { + this->func = new MbedMemberFunctionCallback(tptr, mptr); + this->interval = (us * 1000); + + uBit.messageBus.listen(MICROBIT_ID_MBED_TICKER, 0x0, this, &Ticker::onTick); + system_timer_event_every_us(interval, MICROBIT_ID_MBED_TICKER, 0x0); + } + + void detach() { + system_timer_cancel_event(MICROBIT_ID_MBED_TICKER, 0x0); + } +}; + +#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 diff --git a/inc/compat/Timeout.h b/inc/compat/Timeout.h new file mode 100644 index 00000000..d030b1ad --- /dev/null +++ b/inc/compat/Timeout.h @@ -0,0 +1,58 @@ +#ifndef Timeout_h +#define Timeout_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 "MbedMemberFunctionCallback.h" +#include "Timer.h" +#include "MicroBitEvent.h" + +class Timeout { + + private: + uint32_t interval; + MbedMemberFunctionCallback *func; + + public: + + Timeout() { + } + + void onTimeout(MicroBitEvent e) { + func->fire(); + } + + template + void attach(T* tptr, void (T::*mptr)(void), float s) { + this->func = new MbedMemberFunctionCallback(tptr, mptr); + this->interval = (s * 1000000.0f); + + uBit.messageBus.listen(MICROBIT_ID_MBED_TIMEOUT, 0x0, this, &Timeout::onTimeout); + system_timer_event_after_us(interval, MICROBIT_ID_MBED_TIMEOUT, 0x0); + } + + template + void attach_us(T* tptr, void (T::*mptr)(void), int us) { + this->func = new MbedMemberFunctionCallback(tptr, mptr); + this->interval = (us * 1000); + + uBit.messageBus.listen(MICROBIT_ID_MBED_TIMEOUT, 0x0, this, &Timeout::onTimeout); + system_timer_event_after_us(interval, MICROBIT_ID_MBED_TIMEOUT, 0x0); + } + + void detach() { + system_timer_cancel_event(MICROBIT_ID_MBED_TIMEOUT, 0x0); + } + +}; + +#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 diff --git a/inc/compat/mbed.h b/inc/compat/mbed.h new file mode 100644 index 00000000..e3c40eb5 --- /dev/null +++ b/inc/compat/mbed.h @@ -0,0 +1,25 @@ +/* + * Polyfills and warnings for extensions that use mbed classes + */ + +#include +#include +#include +#include + +#include "MicroBitIO.h" +#include "MbedTimer.h" +#include "Ticker.h" +#include "Timeout.h" +#include "InterruptIn.h" +#include "PwmOut.h" +#include "DigitalIn.h" +#include "DigitalOut.h" + +using std::vector; +using std::map; + +// Missing in codal-core::Pin +#define PIN_CAPABILITY_STANDARD (codal::PinCapability)(0x4 | 0x2 | 0x1) + +#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." diff --git a/source/compat/MbedMemberFunctionCallback.cpp b/source/compat/MbedMemberFunctionCallback.cpp new file mode 100644 index 00000000..10f2ef63 --- /dev/null +++ b/source/compat/MbedMemberFunctionCallback.cpp @@ -0,0 +1,58 @@ +/* +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. +*/ + +/** + * 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. + */ + +#include "CodalConfig.h" +#include "MbedMemberFunctionCallback.h" + +using namespace codal; + +/** + * Calls the method reference held by this MbedMemberFunctionCallback. + * + */ +void MbedMemberFunctionCallback::fire() +{ + mbed_invoke(object, method); +} + +/** + * A comparison of two MbedMemberFunctionCallback objects. + * + * @return true if the given MbedMemberFunctionCallback is equivalent to this one, false otherwise. + */ +bool MbedMemberFunctionCallback::operator==(const MbedMemberFunctionCallback &mfc) +{ + return (object == mfc.object && (memcmp(method,mfc.method,sizeof(method))==0)); +}