Reformat sources to fit the new namespace formatting rules
parent
acc33bfacb
commit
9226230fd5
|
|
@ -3,7 +3,7 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace adc {
|
namespace adc {
|
||||||
|
|
||||||
uint16_t ReadADC(uint8_t adc) { return 0; }
|
uint16_t ReadADC(uint8_t adc) { return 0; }
|
||||||
|
|
||||||
} // namespace adc
|
} // namespace adc
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace adc {
|
namespace adc {
|
||||||
|
|
||||||
/// ADC access routines
|
/// ADC access routines
|
||||||
uint16_t ReadADC(uint8_t adc);
|
uint16_t ReadADC(uint8_t adc);
|
||||||
|
|
||||||
} // namespace adc
|
} // namespace adc
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace cpu {
|
namespace cpu {
|
||||||
|
|
||||||
void Init() {
|
void Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CPU
|
} // namespace CPU
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,15 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace usart {
|
namespace usart {
|
||||||
|
|
||||||
USART usart1;
|
USART usart1;
|
||||||
|
|
||||||
uint8_t USART::Read() {
|
uint8_t USART::Read() {
|
||||||
uint8_t c = 0;
|
uint8_t c = 0;
|
||||||
rx_buf.ConsumeFirst(c);
|
rx_buf.ConsumeFirst(c);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void USART::Write(uint8_t c) {
|
void USART::Write(uint8_t c) {
|
||||||
_written = true;
|
_written = true;
|
||||||
// If the buffer and the data register is empty, just write the byte
|
// If the buffer and the data register is empty, just write the byte
|
||||||
// to the data register and be done. This shortcut helps
|
// to the data register and be done. This shortcut helps
|
||||||
|
|
@ -41,9 +41,9 @@ namespace usart {
|
||||||
}
|
}
|
||||||
|
|
||||||
husart->UCSRxB |= (1 << 5); //enable UDRE interrupt
|
husart->UCSRxB |= (1 << 5); //enable UDRE interrupt
|
||||||
}
|
}
|
||||||
|
|
||||||
void USART::Flush() {
|
void USART::Flush() {
|
||||||
// If we have never written a byte, no need to flush. This special
|
// If we have never written a byte, no need to flush. This special
|
||||||
// case is needed since there is no way to force the TXC (transmit
|
// case is needed since there is no way to force the TXC (transmit
|
||||||
// complete) bit to 1 during initialization
|
// complete) bit to 1 during initialization
|
||||||
|
|
@ -62,13 +62,13 @@ namespace usart {
|
||||||
}
|
}
|
||||||
// If we get here, nothing is queued anymore (DRIE is disabled) and
|
// If we get here, nothing is queued anymore (DRIE is disabled) and
|
||||||
// the hardware finished tranmission (TXC is set).
|
// the hardware finished tranmission (TXC is set).
|
||||||
}
|
}
|
||||||
|
|
||||||
void USART::puts(const char *str) {
|
void USART::puts(const char *str) {
|
||||||
while (*str) {
|
while (*str) {
|
||||||
Write(*str++);
|
Write(*str++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace usart
|
} // namespace usart
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace cpu {
|
namespace cpu {
|
||||||
|
|
||||||
/// CPU init routines (not really necessary for the AVR)
|
/// CPU init routines (not really necessary for the AVR)
|
||||||
void Init();
|
void Init();
|
||||||
|
|
||||||
} // namespace cpu
|
} // namespace cpu
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace EEPROM {
|
namespace EEPROM {
|
||||||
|
|
||||||
/// EEPROM interface
|
/// EEPROM interface
|
||||||
void WriteByte(uint16_t addr, uint8_t value);
|
void WriteByte(uint16_t addr, uint8_t value);
|
||||||
void UpdateByte(uint16_t addr, uint8_t value);
|
void UpdateByte(uint16_t addr, uint8_t value);
|
||||||
uint8_t ReadByte(uint16_t addr);
|
uint8_t ReadByte(uint16_t addr);
|
||||||
|
|
||||||
} // namespace EEPROM
|
} // namespace EEPROM
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -5,29 +5,29 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace gpio {
|
namespace gpio {
|
||||||
|
|
||||||
struct GPIO_TypeDef {
|
struct GPIO_TypeDef {
|
||||||
volatile uint8_t PINx;
|
volatile uint8_t PINx;
|
||||||
volatile uint8_t DDRx;
|
volatile uint8_t DDRx;
|
||||||
volatile uint8_t PORTx;
|
volatile uint8_t PORTx;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Mode : uint8_t {
|
enum class Mode : uint8_t {
|
||||||
input = 0,
|
input = 0,
|
||||||
output,
|
output,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Pull : uint8_t {
|
enum class Pull : uint8_t {
|
||||||
none = 0,
|
none = 0,
|
||||||
up,
|
up,
|
||||||
down, //not available on the AVR
|
down, //not available on the AVR
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Level : uint8_t {
|
enum class Level : uint8_t {
|
||||||
low = 0,
|
low = 0,
|
||||||
high,
|
high,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GPIO_InitTypeDef {
|
struct GPIO_InitTypeDef {
|
||||||
Mode mode;
|
Mode mode;
|
||||||
Pull pull;
|
Pull pull;
|
||||||
Level level;
|
Level level;
|
||||||
|
|
@ -37,32 +37,32 @@ namespace gpio {
|
||||||
inline GPIO_InitTypeDef(Mode mode, Level level)
|
inline GPIO_InitTypeDef(Mode mode, Level level)
|
||||||
: mode(mode)
|
: mode(mode)
|
||||||
, level(level) {};
|
, level(level) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GPIO_pin {
|
struct GPIO_pin {
|
||||||
GPIO_TypeDef *const port;
|
GPIO_TypeDef *const port;
|
||||||
const uint8_t pin;
|
const uint8_t pin;
|
||||||
inline GPIO_pin(GPIO_TypeDef *const port, const uint8_t pin)
|
inline GPIO_pin(GPIO_TypeDef *const port, const uint8_t pin)
|
||||||
: port(port)
|
: port(port)
|
||||||
, pin(pin) {};
|
, pin(pin) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
__attribute__((always_inline)) inline void WritePin(const GPIO_pin portPin, Level level) {
|
__attribute__((always_inline)) inline void WritePin(const GPIO_pin portPin, Level level) {
|
||||||
if (level == Level::high)
|
if (level == Level::high)
|
||||||
portPin.port->PORTx |= (1 << portPin.pin);
|
portPin.port->PORTx |= (1 << portPin.pin);
|
||||||
else
|
else
|
||||||
portPin.port->PORTx &= ~(1 << portPin.pin);
|
portPin.port->PORTx &= ~(1 << portPin.pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((always_inline)) inline Level ReadPin(const GPIO_pin portPin) {
|
__attribute__((always_inline)) inline Level ReadPin(const GPIO_pin portPin) {
|
||||||
return (Level)(portPin.port->PINx & (1 << portPin.pin));
|
return (Level)(portPin.port->PINx & (1 << portPin.pin));
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((always_inline)) inline void TogglePin(const GPIO_pin portPin) {
|
__attribute__((always_inline)) inline void TogglePin(const GPIO_pin portPin) {
|
||||||
portPin.port->PINx |= (1 << portPin.pin);
|
portPin.port->PINx |= (1 << portPin.pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((always_inline)) inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) {
|
__attribute__((always_inline)) inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) {
|
||||||
if (GPIO_Init.mode == Mode::output) {
|
if (GPIO_Init.mode == Mode::output) {
|
||||||
WritePin(portPin, GPIO_Init.level);
|
WritePin(portPin, GPIO_Init.level);
|
||||||
portPin.port->DDRx |= (1 << portPin.pin);
|
portPin.port->DDRx |= (1 << portPin.pin);
|
||||||
|
|
@ -70,7 +70,7 @@ namespace gpio {
|
||||||
portPin.port->DDRx &= ~(1 << portPin.pin);
|
portPin.port->DDRx &= ~(1 << portPin.pin);
|
||||||
WritePin(portPin, (Level)GPIO_Init.pull);
|
WritePin(portPin, (Level)GPIO_Init.pull);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
|
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace spi {
|
namespace spi {
|
||||||
struct SPI_TypeDef {
|
struct SPI_TypeDef {
|
||||||
volatile uint8_t SPCRx;
|
volatile uint8_t SPCRx;
|
||||||
volatile uint8_t SPSRx;
|
volatile uint8_t SPSRx;
|
||||||
volatile uint8_t SPDRx;
|
volatile uint8_t SPDRx;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SPI_InitTypeDef {
|
struct SPI_InitTypeDef {
|
||||||
hal::gpio::GPIO_pin miso_pin;
|
hal::gpio::GPIO_pin miso_pin;
|
||||||
hal::gpio::GPIO_pin mosi_pin;
|
hal::gpio::GPIO_pin mosi_pin;
|
||||||
hal::gpio::GPIO_pin sck_pin;
|
hal::gpio::GPIO_pin sck_pin;
|
||||||
|
|
@ -20,9 +20,9 @@ namespace spi {
|
||||||
uint8_t prescaler;
|
uint8_t prescaler;
|
||||||
uint8_t cpha;
|
uint8_t cpha;
|
||||||
uint8_t cpol;
|
uint8_t cpol;
|
||||||
};
|
};
|
||||||
|
|
||||||
__attribute__((always_inline)) inline void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf) {
|
__attribute__((always_inline)) inline void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf) {
|
||||||
using namespace hal;
|
using namespace hal;
|
||||||
gpio::Init(conf->miso_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none));
|
gpio::Init(conf->miso_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none));
|
||||||
gpio::Init(conf->mosi_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
|
gpio::Init(conf->mosi_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
|
||||||
|
|
@ -34,14 +34,14 @@ namespace spi {
|
||||||
|
|
||||||
hspi->SPCRx = (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | ((conf->cpol & 0x01) << CPOL) | ((conf->cpha & 0x01) << CPHA) | (spr << SPR0);
|
hspi->SPCRx = (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | ((conf->cpol & 0x01) << CPOL) | ((conf->cpha & 0x01) << CPHA) | (spr << SPR0);
|
||||||
hspi->SPSRx = (spi2x << SPI2X);
|
hspi->SPSRx = (spi2x << SPI2X);
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((always_inline)) inline uint8_t TxRx(SPI_TypeDef *const hspi, uint8_t val) {
|
__attribute__((always_inline)) inline uint8_t TxRx(SPI_TypeDef *const hspi, uint8_t val) {
|
||||||
hspi->SPDRx = val;
|
hspi->SPDRx = val;
|
||||||
while (!(hspi->SPSRx & (1 << SPIF)))
|
while (!(hspi->SPSRx & (1 << SPIF)))
|
||||||
;
|
;
|
||||||
return hspi->SPDRx;
|
return hspi->SPDRx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace timers {
|
namespace timers {
|
||||||
|
|
||||||
/// timers
|
/// timers
|
||||||
void ConfigureTimer(uint8_t timer /* some config struct */);
|
void ConfigureTimer(uint8_t timer /* some config struct */);
|
||||||
void StartTimer(uint8_t timer);
|
void StartTimer(uint8_t timer);
|
||||||
void StopTimer(uint8_t timer);
|
void StopTimer(uint8_t timer);
|
||||||
|
|
||||||
} // namespace cpu
|
} // namespace cpu
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace usart {
|
namespace usart {
|
||||||
|
|
||||||
class USART {
|
class USART {
|
||||||
public:
|
public:
|
||||||
struct USART_TypeDef {
|
struct USART_TypeDef {
|
||||||
volatile uint8_t UCSRxA;
|
volatile uint8_t UCSRxA;
|
||||||
volatile uint8_t UCSRxB;
|
volatile uint8_t UCSRxB;
|
||||||
|
|
@ -88,17 +88,17 @@ namespace usart {
|
||||||
husart = conf;
|
husart = conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// IO base address
|
// IO base address
|
||||||
USART_TypeDef *husart;
|
USART_TypeDef *husart;
|
||||||
bool _written;
|
bool _written;
|
||||||
|
|
||||||
CircleBuffer<uint8_t, 32> tx_buf;
|
CircleBuffer<uint8_t, 32> tx_buf;
|
||||||
CircleBuffer<uint8_t, 32> rx_buf;
|
CircleBuffer<uint8_t, 32> rx_buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// beware - normally we'd make a singleton, but avr-gcc generates suboptimal code for them, therefore we only keep this extern variable
|
/// beware - normally we'd make a singleton, but avr-gcc generates suboptimal code for them, therefore we only keep this extern variable
|
||||||
extern USART usart1;
|
extern USART usart1;
|
||||||
|
|
||||||
} // namespace usart
|
} // namespace usart
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace watchdog {
|
namespace watchdog {
|
||||||
|
|
||||||
/// watchdog interface
|
/// watchdog interface
|
||||||
void ConfigureWatchDog(uint16_t period);
|
void ConfigureWatchDog(uint16_t period);
|
||||||
void ResetWatchDog();
|
void ResetWatchDog();
|
||||||
|
|
||||||
} // namespace watchdog
|
} // namespace watchdog
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
namespace modules {
|
namespace modules {
|
||||||
namespace buttons {
|
namespace buttons {
|
||||||
|
|
||||||
uint16_t Buttons::tmpTiming = 0;
|
uint16_t Buttons::tmpTiming = 0;
|
||||||
|
|
||||||
// original idea from: https://www.eeweb.com/debouncing-push-buttons-using-a-state-machine-approach
|
// original idea from: https://www.eeweb.com/debouncing-push-buttons-using-a-state-machine-approach
|
||||||
void Button::Step(uint16_t time, bool press) {
|
void Button::Step(uint16_t time, bool press) {
|
||||||
switch (f.state) {
|
switch (f.state) {
|
||||||
case State::Waiting:
|
case State::Waiting:
|
||||||
if (press) {
|
if (press) {
|
||||||
|
|
@ -39,9 +39,9 @@ namespace buttons {
|
||||||
timeLastChange = time;
|
timeLastChange = time;
|
||||||
f.tmp = false;
|
f.tmp = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t Buttons::Sample(uint16_t rawADC) {
|
int8_t Buttons::Sample(uint16_t rawADC) {
|
||||||
// decode 3 buttons' levels from one ADC
|
// decode 3 buttons' levels from one ADC
|
||||||
// Button 1 - 0
|
// Button 1 - 0
|
||||||
// Button 2 - 344
|
// Button 2 - 344
|
||||||
|
|
@ -55,9 +55,9 @@ namespace buttons {
|
||||||
else if (rawADC > 500 && rawADC < 530)
|
else if (rawADC > 500 && rawADC < 530)
|
||||||
return 2;
|
return 2;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buttons::Step(uint16_t rawADC) {
|
void Buttons::Step(uint16_t rawADC) {
|
||||||
// @@TODO temporary timing
|
// @@TODO temporary timing
|
||||||
++tmpTiming;
|
++tmpTiming;
|
||||||
int8_t currentState = Sample(rawADC);
|
int8_t currentState = Sample(rawADC);
|
||||||
|
|
@ -65,7 +65,7 @@ namespace buttons {
|
||||||
// this button was pressed if b == currentState, released otherwise
|
// this button was pressed if b == currentState, released otherwise
|
||||||
buttons[b].Step(tmpTiming, b == currentState);
|
buttons[b].Step(tmpTiming, b == currentState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace buttons
|
} // namespace buttons
|
||||||
} // namespace modules
|
} // namespace modules
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
namespace modules {
|
namespace modules {
|
||||||
namespace buttons {
|
namespace buttons {
|
||||||
|
|
||||||
struct Button {
|
struct Button {
|
||||||
inline constexpr Button()
|
inline constexpr Button()
|
||||||
: timeLastChange(0) {}
|
: timeLastChange(0) {}
|
||||||
|
|
||||||
|
|
@ -19,7 +19,7 @@ namespace buttons {
|
||||||
/// State machine stepping routine
|
/// State machine stepping routine
|
||||||
void Step(uint16_t time, bool press);
|
void Step(uint16_t time, bool press);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// time interval for debouncing @@TODO specify units
|
/// time interval for debouncing @@TODO specify units
|
||||||
constexpr static const uint16_t debounce = 100;
|
constexpr static const uint16_t debounce = 100;
|
||||||
|
|
||||||
|
|
@ -46,14 +46,14 @@ namespace buttons {
|
||||||
|
|
||||||
/// Timestamp of the last change of ADC state for this button
|
/// Timestamp of the last change of ADC state for this button
|
||||||
uint16_t timeLastChange;
|
uint16_t timeLastChange;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Buttons {
|
class Buttons {
|
||||||
constexpr static const uint8_t N = 3; ///< number of buttons currently supported
|
constexpr static const uint8_t N = 3; ///< number of buttons currently supported
|
||||||
constexpr static const uint8_t adc = 1; ///< ADC index - will be some define or other constant later on
|
constexpr static const uint8_t adc = 1; ///< ADC index - will be some define or other constant later on
|
||||||
static uint16_t tmpTiming; ///< subject to removal when we have timers implemented - now used for the unit tests
|
static uint16_t tmpTiming; ///< subject to removal when we have timers implemented - now used for the unit tests
|
||||||
|
|
||||||
public:
|
public:
|
||||||
inline constexpr Buttons() = default;
|
inline constexpr Buttons() = default;
|
||||||
|
|
||||||
/// State machine step - reads the ADC, processes debouncing, updates states of individual buttons
|
/// State machine step - reads the ADC, processes debouncing, updates states of individual buttons
|
||||||
|
|
@ -63,13 +63,13 @@ namespace buttons {
|
||||||
/// @@TODO add range checking if necessary
|
/// @@TODO add range checking if necessary
|
||||||
inline bool ButtonPressed(uint8_t index) const { return buttons[index].Pressed(); }
|
inline bool ButtonPressed(uint8_t index) const { return buttons[index].Pressed(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Button buttons[N];
|
Button buttons[N];
|
||||||
|
|
||||||
/// Call to the ADC and decode its output into a button index
|
/// Call to the ADC and decode its output into a button index
|
||||||
/// @returns index of the button pressed or -1 in case no button is pressed
|
/// @returns index of the button pressed or -1 in case no button is pressed
|
||||||
static int8_t Sample(uint16_t rawADC);
|
static int8_t Sample(uint16_t rawADC);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace buttons
|
} // namespace buttons
|
||||||
} // namespace modules
|
} // namespace modules
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
namespace modules {
|
namespace modules {
|
||||||
namespace leds {
|
namespace leds {
|
||||||
|
|
||||||
void LED::SetMode(Mode mode) {
|
void LED::SetMode(Mode mode) {
|
||||||
state.mode = mode;
|
state.mode = mode;
|
||||||
// set initial state of LEDs correctly - transition from one mode to another
|
// set initial state of LEDs correctly - transition from one mode to another
|
||||||
switch (state.mode) {
|
switch (state.mode) {
|
||||||
|
|
@ -19,9 +19,9 @@ namespace leds {
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LED::Step(bool oddPeriod) {
|
bool LED::Step(bool oddPeriod) {
|
||||||
switch (state.mode) {
|
switch (state.mode) {
|
||||||
// on and off don't change while stepping
|
// on and off don't change while stepping
|
||||||
case Mode::blink0:
|
case Mode::blink0:
|
||||||
|
|
@ -33,9 +33,9 @@ namespace leds {
|
||||||
default: // do nothing
|
default: // do nothing
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t LEDs::Step(uint8_t delta_ms) {
|
uint16_t LEDs::Step(uint8_t delta_ms) {
|
||||||
ms += delta_ms;
|
ms += delta_ms;
|
||||||
bool oddPeriod = ((ms / 1000U) & 0x01U) != 0;
|
bool oddPeriod = ((ms / 1000U) & 0x01U) != 0;
|
||||||
uint16_t result = 0;
|
uint16_t result = 0;
|
||||||
|
|
@ -44,7 +44,7 @@ namespace leds {
|
||||||
result |= leds[i].Step(oddPeriod);
|
result |= leds[i].Step(oddPeriod);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace leds
|
} // namespace leds
|
||||||
} // namespace modules
|
} // namespace modules
|
||||||
|
|
|
||||||
|
|
@ -13,19 +13,19 @@
|
||||||
namespace modules {
|
namespace modules {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
// decoding automaton
|
// decoding automaton
|
||||||
// states: input -> transition into state
|
// states: input -> transition into state
|
||||||
// Code QTLMUXPSBEWK -> msgcode
|
// Code QTLMUXPSBEWK -> msgcode
|
||||||
// \n ->start
|
// \n ->start
|
||||||
// * ->error
|
// * ->error
|
||||||
// error \n ->start
|
// error \n ->start
|
||||||
// * ->error
|
// * ->error
|
||||||
// msgcode 0-9 ->msgvalue
|
// msgcode 0-9 ->msgvalue
|
||||||
// * ->error
|
// * ->error
|
||||||
// msgvalue 0-9 ->msgvalue
|
// msgvalue 0-9 ->msgvalue
|
||||||
// \n ->start successfully accepted command
|
// \n ->start successfully accepted command
|
||||||
|
|
||||||
DecodeStatus Protocol::DecodeRequest(uint8_t c) {
|
DecodeStatus Protocol::DecodeRequest(uint8_t c) {
|
||||||
switch (rqState) {
|
switch (rqState) {
|
||||||
case RequestStates::Code:
|
case RequestStates::Code:
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
|
@ -73,16 +73,16 @@ namespace protocol {
|
||||||
return DecodeStatus::Error;
|
return DecodeStatus::Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) {
|
uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) {
|
||||||
txbuff[0] = (uint8_t)msg.code;
|
txbuff[0] = (uint8_t)msg.code;
|
||||||
txbuff[1] = msg.value + '0';
|
txbuff[1] = msg.value + '0';
|
||||||
txbuff[2] = '\n';
|
txbuff[2] = '\n';
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
DecodeStatus Protocol::DecodeResponse(uint8_t c) {
|
DecodeStatus Protocol::DecodeResponse(uint8_t c) {
|
||||||
switch (rspState) {
|
switch (rspState) {
|
||||||
case ResponseStates::RequestCode:
|
case ResponseStates::RequestCode:
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
|
@ -156,18 +156,18 @@ namespace protocol {
|
||||||
return DecodeStatus::Error;
|
return DecodeStatus::Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) {
|
uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) {
|
||||||
txbuff[0] = (uint8_t)msg.code;
|
txbuff[0] = (uint8_t)msg.code;
|
||||||
txbuff[1] = msg.value + '0';
|
txbuff[1] = msg.value + '0';
|
||||||
txbuff[2] = ' ';
|
txbuff[2] = ' ';
|
||||||
txbuff[3] = (uint8_t)ar;
|
txbuff[3] = (uint8_t)ar;
|
||||||
txbuff[4] = '\n';
|
txbuff[4] = '\n';
|
||||||
return 5;
|
return 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) {
|
uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) {
|
||||||
txbuff[0] = (uint8_t)msg.code;
|
txbuff[0] = (uint8_t)msg.code;
|
||||||
txbuff[1] = msg.value + '0';
|
txbuff[1] = msg.value + '0';
|
||||||
txbuff[2] = ' ';
|
txbuff[2] = ' ';
|
||||||
|
|
@ -175,9 +175,9 @@ namespace protocol {
|
||||||
txbuff[4] = findaValue + '0';
|
txbuff[4] = findaValue + '0';
|
||||||
txbuff[5] = '\n';
|
txbuff[5] = '\n';
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Protocol::EncodeResponseVersion(const RequestMsg &msg, uint8_t value, uint8_t *txbuff) {
|
uint8_t Protocol::EncodeResponseVersion(const RequestMsg &msg, uint8_t value, uint8_t *txbuff) {
|
||||||
txbuff[0] = (uint8_t)msg.code;
|
txbuff[0] = (uint8_t)msg.code;
|
||||||
txbuff[1] = msg.value + '0';
|
txbuff[1] = msg.value + '0';
|
||||||
txbuff[2] = ' ';
|
txbuff[2] = ' ';
|
||||||
|
|
@ -195,9 +195,9 @@ namespace protocol {
|
||||||
}
|
}
|
||||||
*dst = '\n';
|
*dst = '\n';
|
||||||
return dst - txbuff + 1;
|
return dst - txbuff + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseMsgParamCodes code, uint8_t value, uint8_t *txbuff) {
|
uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseMsgParamCodes code, uint8_t value, uint8_t *txbuff) {
|
||||||
txbuff[0] = (uint8_t)msg.code;
|
txbuff[0] = (uint8_t)msg.code;
|
||||||
txbuff[1] = msg.value + '0';
|
txbuff[1] = msg.value + '0';
|
||||||
txbuff[2] = ' ';
|
txbuff[2] = ' ';
|
||||||
|
|
@ -217,7 +217,7 @@ namespace protocol {
|
||||||
}
|
}
|
||||||
*dst = '\n';
|
*dst = '\n';
|
||||||
return dst - txbuff + 1;
|
return dst - txbuff + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace protocol
|
} // namespace protocol
|
||||||
} // namespace modules
|
} // namespace modules
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
namespace modules {
|
namespace modules {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
enum class RequestMsgCodes : uint8_t {
|
enum class RequestMsgCodes : uint8_t {
|
||||||
unknown = 0,
|
unknown = 0,
|
||||||
Query = 'Q',
|
Query = 'Q',
|
||||||
Tool = 'T',
|
Tool = 'T',
|
||||||
|
|
@ -23,31 +23,31 @@ namespace protocol {
|
||||||
Eject = 'E',
|
Eject = 'E',
|
||||||
Wait = 'W',
|
Wait = 'W',
|
||||||
Cut = 'K'
|
Cut = 'K'
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class ResponseMsgParamCodes : uint8_t {
|
enum class ResponseMsgParamCodes : uint8_t {
|
||||||
unknown = 0,
|
unknown = 0,
|
||||||
Processing = 'P',
|
Processing = 'P',
|
||||||
Error = 'E',
|
Error = 'E',
|
||||||
Finished = 'F',
|
Finished = 'F',
|
||||||
Accepted = 'A',
|
Accepted = 'A',
|
||||||
Rejected = 'R'
|
Rejected = 'R'
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A request message
|
/// A request message
|
||||||
/// Requests are being sent by the printer into the MMU
|
/// Requests are being sent by the printer into the MMU
|
||||||
/// It is the same structure as the generic Msg
|
/// It is the same structure as the generic Msg
|
||||||
struct RequestMsg {
|
struct RequestMsg {
|
||||||
RequestMsgCodes code;
|
RequestMsgCodes code;
|
||||||
uint8_t value;
|
uint8_t value;
|
||||||
inline RequestMsg(RequestMsgCodes code, uint8_t value)
|
inline RequestMsg(RequestMsgCodes code, uint8_t value)
|
||||||
: code(code)
|
: code(code)
|
||||||
, value(value) {}
|
, value(value) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A response message
|
/// A response message
|
||||||
/// Responses are being sent from the MMU into the printer as a response to a request message
|
/// Responses are being sent from the MMU into the printer as a response to a request message
|
||||||
struct ResponseMsg {
|
struct ResponseMsg {
|
||||||
RequestMsg request; ///< response is always preceeded by the request message
|
RequestMsg request; ///< response is always preceeded by the request message
|
||||||
ResponseMsgParamCodes paramCode; ///< parameters of reply
|
ResponseMsgParamCodes paramCode; ///< parameters of reply
|
||||||
uint8_t paramValue; ///< parameters of reply
|
uint8_t paramValue; ///< parameters of reply
|
||||||
|
|
@ -55,20 +55,20 @@ namespace protocol {
|
||||||
: request(request)
|
: request(request)
|
||||||
, paramCode(paramCode)
|
, paramCode(paramCode)
|
||||||
, paramValue(paramValue) {}
|
, paramValue(paramValue) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Message decoding return value
|
/// Message decoding return value
|
||||||
enum class DecodeStatus : uint_fast8_t {
|
enum class DecodeStatus : uint_fast8_t {
|
||||||
MessageCompleted, ///< message completed and successfully lexed
|
MessageCompleted, ///< message completed and successfully lexed
|
||||||
NeedMoreData, ///< message incomplete yet, waiting for another byte to come
|
NeedMoreData, ///< message incomplete yet, waiting for another byte to come
|
||||||
Error, ///< input character broke message decoding
|
Error, ///< input character broke message decoding
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Protocol class is responsible for creating/decoding messages in Rx/Tx buffer
|
/// Protocol class is responsible for creating/decoding messages in Rx/Tx buffer
|
||||||
/// Beware - in the decoding more, it is meant to be a statefull instance which works through public methods
|
/// Beware - in the decoding more, it is meant to be a statefull instance which works through public methods
|
||||||
/// processing one input byte per call
|
/// processing one input byte per call
|
||||||
class Protocol {
|
class Protocol {
|
||||||
public:
|
public:
|
||||||
inline Protocol()
|
inline Protocol()
|
||||||
: rqState(RequestStates::Code)
|
: rqState(RequestStates::Code)
|
||||||
, requestMsg(RequestMsgCodes::unknown, 0)
|
, requestMsg(RequestMsgCodes::unknown, 0)
|
||||||
|
|
@ -119,7 +119,7 @@ namespace protocol {
|
||||||
/// @returns the most recently lexed response message
|
/// @returns the most recently lexed response message
|
||||||
inline const ResponseMsg GetResponseMsg() const { return responseMsg; }
|
inline const ResponseMsg GetResponseMsg() const { return responseMsg; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum class RequestStates : uint8_t {
|
enum class RequestStates : uint8_t {
|
||||||
Code, ///< starting state - expects message code
|
Code, ///< starting state - expects message code
|
||||||
Value, ///< expecting code value
|
Value, ///< expecting code value
|
||||||
|
|
@ -139,7 +139,7 @@ namespace protocol {
|
||||||
|
|
||||||
ResponseStates rspState;
|
ResponseStates rspState;
|
||||||
ResponseMsg responseMsg;
|
ResponseMsg responseMsg;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace protocol
|
} // namespace protocol
|
||||||
} // namespace modules
|
} // namespace modules
|
||||||
|
|
|
||||||
|
|
@ -5,20 +5,20 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace adc {
|
namespace adc {
|
||||||
|
|
||||||
static TADCData values2Return;
|
static TADCData values2Return;
|
||||||
static TADCData::const_iterator rdptr = values2Return.cbegin();
|
static TADCData::const_iterator rdptr = values2Return.cbegin();
|
||||||
static uint8_t oversampleFactor = 1;
|
static uint8_t oversampleFactor = 1;
|
||||||
static uint8_t oversample = 1; ///< current count of oversampled values returned from the ADC - will get filled with oversampleFactor once it reaches zero
|
static uint8_t oversample = 1; ///< current count of oversampled values returned from the ADC - will get filled with oversampleFactor once it reaches zero
|
||||||
|
|
||||||
void ReinitADC(TADCData &&d, uint8_t ovsmpl) {
|
void ReinitADC(TADCData &&d, uint8_t ovsmpl) {
|
||||||
values2Return = std::move(d);
|
values2Return = std::move(d);
|
||||||
oversampleFactor = ovsmpl;
|
oversampleFactor = ovsmpl;
|
||||||
oversample = ovsmpl;
|
oversample = ovsmpl;
|
||||||
rdptr = values2Return.cbegin();
|
rdptr = values2Return.cbegin();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ADC access routines
|
/// ADC access routines
|
||||||
uint16_t ReadADC(uint8_t /*adc*/) {
|
uint16_t ReadADC(uint8_t /*adc*/) {
|
||||||
if (!oversample) {
|
if (!oversample) {
|
||||||
++rdptr;
|
++rdptr;
|
||||||
oversample = oversampleFactor;
|
oversample = oversampleFactor;
|
||||||
|
|
@ -26,7 +26,7 @@ namespace adc {
|
||||||
--oversample;
|
--oversample;
|
||||||
}
|
}
|
||||||
return rdptr != values2Return.end() ? *rdptr : 1023;
|
return rdptr != values2Return.end() ? *rdptr : 1023;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace adc
|
} // namespace adc
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,9 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace adc {
|
namespace adc {
|
||||||
|
|
||||||
using TADCData = std::vector<uint16_t>;
|
using TADCData = std::vector<uint16_t>;
|
||||||
|
|
||||||
void ReinitADC(TADCData &&d, uint8_t ovsmpl);
|
void ReinitADC(TADCData &&d, uint8_t ovsmpl);
|
||||||
|
|
||||||
} // namespace adc
|
} // namespace adc
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue