ADD mail box for gear device
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include <chrono>
|
||||
|
||||
#include "joystick.hpp"
|
||||
#include "mbed_atomic.h"
|
||||
#include "mbed_trace.h"
|
||||
|
||||
#if MBED_CONF_MBED_TRACE_ENABLE
|
||||
@@ -38,9 +39,33 @@
|
||||
|
||||
namespace multi_tasking {
|
||||
|
||||
GearDevice::GearDevice(Callback<void()> cbOnUp, Callback<void()> cbOnDown) {
|
||||
disco::Joystick::getInstance().setUpCallback(callback(cbOnUp));
|
||||
disco::Joystick::getInstance().setDownCallback(callback(cbOnDown));
|
||||
GearDevice::GearDevice(Mail<uint8_t, 16>* mailBox) {
|
||||
disco::Joystick::getInstance().setUpCallback(callback(this, &GearDevice::onUp));
|
||||
disco::Joystick::getInstance().setDownCallback(callback(this, &GearDevice::onDown));
|
||||
|
||||
_mailBox = mailBox;
|
||||
}
|
||||
|
||||
void GearDevice::onUp() {
|
||||
if (_currentGear < bike_computer::kMaxGear) {
|
||||
core_util_atomic_incr_u8(&_currentGear, 1);
|
||||
sendMail(core_util_atomic_load_u8(&_currentGear));
|
||||
}
|
||||
}
|
||||
|
||||
void GearDevice::onDown() {
|
||||
if (_currentGear > bike_computer::kMinGear) {
|
||||
core_util_atomic_decr_u8(&_currentGear, 1);
|
||||
sendMail(core_util_atomic_load_u8(&_currentGear));
|
||||
}
|
||||
}
|
||||
|
||||
void GearDevice::sendMail(uint32_t data) {
|
||||
uint8_t* currentGear = _mailBox->try_alloc();
|
||||
if (currentGear != nullptr) {
|
||||
*currentGear = data;
|
||||
_mailBox->put(currentGear);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace multi_tasking
|
||||
|
||||
Reference in New Issue
Block a user