Today I had a look under the hood of the Standard Arduino implementation for the Raspberry Pico. I was surprised that they did not base their work on the Pico API but they based their work on ARM Mbed.
Here is a blink example using the Ticker from the Mbed API:
Mbed Ticker
#include "mbed.h"
using namespace mbed;
Ticker ticker;
DigitalOut led1(LED1);
void blink(){
led1 = !led1;
}
void setup() {
ticker.attach(&blink, 1);
}
void loop() {
}
Mbed Ticker – with Arduino Pin Names
In Arduino we do not want to introduce another set of pin names! Fortunately we can use the digitalPinToPinName() method to convert an Arduino PIN into a Mbed pin. So we can use the Arduino LED_BUILTIN like this:
#include "mbed.h"
#include "pinDefinitions.h"
using namespace mbed;
Ticker ticker;
DigitalOut led1(digitalPinToPinName(LED_BUILTIN));
void blink(){
led1 = !led1;
}
void setup() {
ticker.attach(&blink, 1);
}
void loop() {
}
Mbed PWM
Arduino has just a limited PWM functionality usually at just one predefined frequency. In Mbed we have the power to flexibly define the frequency as well:
#include "mbed.h"
#include "pinDefinitions.h"
using namespace mbed;
Ticker ticker;
PwmOut led(digitalPinToPinName(LED_BUILTIN));
float value = 0;
void setup() {
//led.period(1.0f); // 1 second period
//led.period_ms(1); // 1 millisecond period
led.period_us(1000); // 1000 microseconds
}
void loop() {
value += 0.00001;
led.write(value); // between 0.0 and 1.0
if (value>=1.0) {
value = 0;
}
}
Mbed Threads
It is also possible to start new threads:
#include "mbed.h"
using namespace mbed;
rtos::Thread thread;
rtos::Semaphore semaphore(1);
void runInThread() {
while(true){
semaphore.acquire();
Serial.println("hallo from thread");
semaphore.release();
delay(1000);
}
}
void setup() {
Serial.begin(115200);
thread.start(runInThread);
}
void loop() {
semaphore.acquire();
Serial.println("hallo from thread");
semaphore.release();
delay(1000);
}
Conclusion
It is good to know that if something is missing in the Arduino API, we have access to a richer API – that we can use if we are ready to sacrifice portability to other architectures….
0 Comments