Reformat sources to fit the new namespace formatting rules

pull/13/head
D.R.racer 2021-05-25 12:24:19 +02:00
parent acc33bfacb
commit 9226230fd5
18 changed files with 728 additions and 728 deletions

View File

@ -3,7 +3,7 @@
namespace hal {
namespace adc {
uint16_t ReadADC(uint8_t adc) { return 0; }
uint16_t ReadADC(uint8_t adc) { return 0; }
} // namespace adc
} // namespace hal

View File

@ -6,8 +6,8 @@
namespace hal {
namespace adc {
/// ADC access routines
uint16_t ReadADC(uint8_t adc);
/// ADC access routines
uint16_t ReadADC(uint8_t adc);
} // namespace adc
} // namespace hal

View File

@ -3,8 +3,8 @@
namespace hal {
namespace cpu {
void Init() {
}
void Init() {
}
} // namespace CPU
} // namespace hal

View File

@ -4,15 +4,15 @@
namespace hal {
namespace usart {
USART usart1;
USART usart1;
uint8_t USART::Read() {
uint8_t USART::Read() {
uint8_t c = 0;
rx_buf.ConsumeFirst(c);
return c;
}
}
void USART::Write(uint8_t c) {
void USART::Write(uint8_t c) {
_written = true;
// If the buffer and the data register is empty, just write the byte
// to the data register and be done. This shortcut helps
@ -41,9 +41,9 @@ namespace usart {
}
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
// case is needed since there is no way to force the TXC (transmit
// complete) bit to 1 during initialization
@ -62,13 +62,13 @@ namespace usart {
}
// If we get here, nothing is queued anymore (DRIE is disabled) and
// the hardware finished tranmission (TXC is set).
}
}
void USART::puts(const char *str) {
void USART::puts(const char *str) {
while (*str) {
Write(*str++);
}
}
}
} // namespace usart
} // namespace hal

View File

@ -5,8 +5,8 @@
namespace hal {
namespace cpu {
/// CPU init routines (not really necessary for the AVR)
void Init();
/// CPU init routines (not really necessary for the AVR)
void Init();
} // namespace cpu
} // namespace hal

View File

@ -3,10 +3,10 @@
namespace hal {
namespace EEPROM {
/// EEPROM interface
void WriteByte(uint16_t addr, uint8_t value);
void UpdateByte(uint16_t addr, uint8_t value);
uint8_t ReadByte(uint16_t addr);
/// EEPROM interface
void WriteByte(uint16_t addr, uint8_t value);
void UpdateByte(uint16_t addr, uint8_t value);
uint8_t ReadByte(uint16_t addr);
} // namespace EEPROM
} // namespace hal

View File

@ -5,29 +5,29 @@
namespace hal {
namespace gpio {
struct GPIO_TypeDef {
struct GPIO_TypeDef {
volatile uint8_t PINx;
volatile uint8_t DDRx;
volatile uint8_t PORTx;
};
};
enum class Mode : uint8_t {
enum class Mode : uint8_t {
input = 0,
output,
};
};
enum class Pull : uint8_t {
enum class Pull : uint8_t {
none = 0,
up,
down, //not available on the AVR
};
};
enum class Level : uint8_t {
enum class Level : uint8_t {
low = 0,
high,
};
};
struct GPIO_InitTypeDef {
struct GPIO_InitTypeDef {
Mode mode;
Pull pull;
Level level;
@ -37,32 +37,32 @@ namespace gpio {
inline GPIO_InitTypeDef(Mode mode, Level level)
: mode(mode)
, level(level) {};
};
};
struct GPIO_pin {
struct GPIO_pin {
GPIO_TypeDef *const port;
const uint8_t pin;
inline GPIO_pin(GPIO_TypeDef *const port, const uint8_t pin)
: port(port)
, 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)
portPin.port->PORTx |= (1 << portPin.pin);
else
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));
}
}
__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);
}
}
__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) {
WritePin(portPin, GPIO_Init.level);
portPin.port->DDRx |= (1 << portPin.pin);
@ -70,7 +70,7 @@ namespace gpio {
portPin.port->DDRx &= ~(1 << portPin.pin);
WritePin(portPin, (Level)GPIO_Init.pull);
}
}
}
}
}

View File

@ -6,13 +6,13 @@
namespace hal {
namespace spi {
struct SPI_TypeDef {
struct SPI_TypeDef {
volatile uint8_t SPCRx;
volatile uint8_t SPSRx;
volatile uint8_t SPDRx;
};
};
struct SPI_InitTypeDef {
struct SPI_InitTypeDef {
hal::gpio::GPIO_pin miso_pin;
hal::gpio::GPIO_pin mosi_pin;
hal::gpio::GPIO_pin sck_pin;
@ -20,9 +20,9 @@ namespace spi {
uint8_t prescaler;
uint8_t cpha;
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;
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));
@ -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->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;
while (!(hspi->SPSRx & (1 << SPIF)))
;
return hspi->SPDRx;
}
}
}
}

View File

@ -5,10 +5,10 @@
namespace hal {
namespace timers {
/// timers
void ConfigureTimer(uint8_t timer /* some config struct */);
void StartTimer(uint8_t timer);
void StopTimer(uint8_t timer);
/// timers
void ConfigureTimer(uint8_t timer /* some config struct */);
void StartTimer(uint8_t timer);
void StopTimer(uint8_t timer);
} // namespace cpu
} // namespace hal

View File

@ -10,8 +10,8 @@
namespace hal {
namespace usart {
class USART {
public:
class USART {
public:
struct USART_TypeDef {
volatile uint8_t UCSRxA;
volatile uint8_t UCSRxB;
@ -88,17 +88,17 @@ namespace usart {
husart = conf;
}
private:
private:
// IO base address
USART_TypeDef *husart;
bool _written;
CircleBuffer<uint8_t, 32> tx_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
extern USART usart1;
/// 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;
} // namespace usart
} // namespace hal

View File

@ -5,9 +5,9 @@
namespace hal {
namespace watchdog {
/// watchdog interface
void ConfigureWatchDog(uint16_t period);
void ResetWatchDog();
/// watchdog interface
void ConfigureWatchDog(uint16_t period);
void ResetWatchDog();
} // namespace watchdog
} // namespace hal

View File

@ -3,10 +3,10 @@
namespace modules {
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
void Button::Step(uint16_t time, bool press) {
// original idea from: https://www.eeweb.com/debouncing-push-buttons-using-a-state-machine-approach
void Button::Step(uint16_t time, bool press) {
switch (f.state) {
case State::Waiting:
if (press) {
@ -39,9 +39,9 @@ namespace buttons {
timeLastChange = time;
f.tmp = false;
}
}
}
int8_t Buttons::Sample(uint16_t rawADC) {
int8_t Buttons::Sample(uint16_t rawADC) {
// decode 3 buttons' levels from one ADC
// Button 1 - 0
// Button 2 - 344
@ -55,9 +55,9 @@ namespace buttons {
else if (rawADC > 500 && rawADC < 530)
return 2;
return -1;
}
}
void Buttons::Step(uint16_t rawADC) {
void Buttons::Step(uint16_t rawADC) {
// @@TODO temporary timing
++tmpTiming;
int8_t currentState = Sample(rawADC);
@ -65,7 +65,7 @@ namespace buttons {
// this button was pressed if b == currentState, released otherwise
buttons[b].Step(tmpTiming, b == currentState);
}
}
}
} // namespace buttons
} // namespace modules

View File

@ -9,7 +9,7 @@
namespace modules {
namespace buttons {
struct Button {
struct Button {
inline constexpr Button()
: timeLastChange(0) {}
@ -19,7 +19,7 @@ namespace buttons {
/// State machine stepping routine
void Step(uint16_t time, bool press);
private:
private:
/// time interval for debouncing @@TODO specify units
constexpr static const uint16_t debounce = 100;
@ -46,14 +46,14 @@ namespace buttons {
/// Timestamp of the last change of ADC state for this button
uint16_t timeLastChange;
};
};
class Buttons {
class Buttons {
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
static uint16_t tmpTiming; ///< subject to removal when we have timers implemented - now used for the unit tests
public:
public:
inline constexpr Buttons() = default;
/// 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
inline bool ButtonPressed(uint8_t index) const { return buttons[index].Pressed(); }
private:
private:
Button buttons[N];
/// 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
static int8_t Sample(uint16_t rawADC);
};
};
} // namespace buttons
} // namespace modules

View File

@ -3,7 +3,7 @@
namespace modules {
namespace leds {
void LED::SetMode(Mode mode) {
void LED::SetMode(Mode mode) {
state.mode = mode;
// set initial state of LEDs correctly - transition from one mode to another
switch (state.mode) {
@ -19,9 +19,9 @@ namespace leds {
default:
break;
}
}
}
bool LED::Step(bool oddPeriod) {
bool LED::Step(bool oddPeriod) {
switch (state.mode) {
// on and off don't change while stepping
case Mode::blink0:
@ -33,9 +33,9 @@ namespace leds {
default: // do nothing
break;
}
}
}
uint16_t LEDs::Step(uint8_t delta_ms) {
uint16_t LEDs::Step(uint8_t delta_ms) {
ms += delta_ms;
bool oddPeriod = ((ms / 1000U) & 0x01U) != 0;
uint16_t result = 0;
@ -44,7 +44,7 @@ namespace leds {
result |= leds[i].Step(oddPeriod);
}
return result;
}
}
} // namespace leds
} // namespace modules

View File

@ -13,19 +13,19 @@
namespace modules {
namespace protocol {
// decoding automaton
// states: input -> transition into state
// Code QTLMUXPSBEWK -> msgcode
// \n ->start
// * ->error
// error \n ->start
// * ->error
// msgcode 0-9 ->msgvalue
// * ->error
// msgvalue 0-9 ->msgvalue
// \n ->start successfully accepted command
// decoding automaton
// states: input -> transition into state
// Code QTLMUXPSBEWK -> msgcode
// \n ->start
// * ->error
// error \n ->start
// * ->error
// msgcode 0-9 ->msgvalue
// * ->error
// msgvalue 0-9 ->msgvalue
// \n ->start successfully accepted command
DecodeStatus Protocol::DecodeRequest(uint8_t c) {
DecodeStatus Protocol::DecodeRequest(uint8_t c) {
switch (rqState) {
case RequestStates::Code:
switch (c) {
@ -73,16 +73,16 @@ namespace protocol {
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[1] = msg.value + '0';
txbuff[2] = '\n';
return 3;
}
}
DecodeStatus Protocol::DecodeResponse(uint8_t c) {
DecodeStatus Protocol::DecodeResponse(uint8_t c) {
switch (rspState) {
case ResponseStates::RequestCode:
switch (c) {
@ -156,18 +156,18 @@ namespace protocol {
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[1] = msg.value + '0';
txbuff[2] = ' ';
txbuff[3] = (uint8_t)ar;
txbuff[4] = '\n';
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[1] = msg.value + '0';
txbuff[2] = ' ';
@ -175,9 +175,9 @@ namespace protocol {
txbuff[4] = findaValue + '0';
txbuff[5] = '\n';
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[1] = msg.value + '0';
txbuff[2] = ' ';
@ -195,9 +195,9 @@ namespace protocol {
}
*dst = '\n';
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[1] = msg.value + '0';
txbuff[2] = ' ';
@ -217,7 +217,7 @@ namespace protocol {
}
*dst = '\n';
return dst - txbuff + 1;
}
}
} // namespace protocol
} // namespace modules

View File

@ -9,7 +9,7 @@
namespace modules {
namespace protocol {
enum class RequestMsgCodes : uint8_t {
enum class RequestMsgCodes : uint8_t {
unknown = 0,
Query = 'Q',
Tool = 'T',
@ -23,31 +23,31 @@ namespace protocol {
Eject = 'E',
Wait = 'W',
Cut = 'K'
};
};
enum class ResponseMsgParamCodes : uint8_t {
enum class ResponseMsgParamCodes : uint8_t {
unknown = 0,
Processing = 'P',
Error = 'E',
Finished = 'F',
Accepted = 'A',
Rejected = 'R'
};
};
/// A request message
/// Requests are being sent by the printer into the MMU
/// It is the same structure as the generic Msg
struct RequestMsg {
/// A request message
/// Requests are being sent by the printer into the MMU
/// It is the same structure as the generic Msg
struct RequestMsg {
RequestMsgCodes code;
uint8_t value;
inline RequestMsg(RequestMsgCodes code, uint8_t value)
: code(code)
, value(value) {}
};
};
/// A response message
/// Responses are being sent from the MMU into the printer as a response to a request message
struct ResponseMsg {
/// A response message
/// Responses are being sent from the MMU into the printer as a response to a request message
struct ResponseMsg {
RequestMsg request; ///< response is always preceeded by the request message
ResponseMsgParamCodes paramCode; ///< parameters of reply
uint8_t paramValue; ///< parameters of reply
@ -55,20 +55,20 @@ namespace protocol {
: request(request)
, paramCode(paramCode)
, paramValue(paramValue) {}
};
};
/// Message decoding return value
enum class DecodeStatus : uint_fast8_t {
/// Message decoding return value
enum class DecodeStatus : uint_fast8_t {
MessageCompleted, ///< message completed and successfully lexed
NeedMoreData, ///< message incomplete yet, waiting for another byte to come
Error, ///< input character broke message decoding
};
};
/// 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
/// processing one input byte per call
class Protocol {
public:
/// 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
/// processing one input byte per call
class Protocol {
public:
inline Protocol()
: rqState(RequestStates::Code)
, requestMsg(RequestMsgCodes::unknown, 0)
@ -119,7 +119,7 @@ namespace protocol {
/// @returns the most recently lexed response message
inline const ResponseMsg GetResponseMsg() const { return responseMsg; }
private:
private:
enum class RequestStates : uint8_t {
Code, ///< starting state - expects message code
Value, ///< expecting code value
@ -139,7 +139,7 @@ namespace protocol {
ResponseStates rspState;
ResponseMsg responseMsg;
};
};
} // namespace protocol
} // namespace modules

View File

@ -5,20 +5,20 @@
namespace hal {
namespace adc {
static TADCData values2Return;
static TADCData::const_iterator rdptr = values2Return.cbegin();
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 TADCData values2Return;
static TADCData::const_iterator rdptr = values2Return.cbegin();
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
void ReinitADC(TADCData &&d, uint8_t ovsmpl) {
void ReinitADC(TADCData &&d, uint8_t ovsmpl) {
values2Return = std::move(d);
oversampleFactor = ovsmpl;
oversample = ovsmpl;
rdptr = values2Return.cbegin();
}
}
/// ADC access routines
uint16_t ReadADC(uint8_t /*adc*/) {
/// ADC access routines
uint16_t ReadADC(uint8_t /*adc*/) {
if (!oversample) {
++rdptr;
oversample = oversampleFactor;
@ -26,7 +26,7 @@ namespace adc {
--oversample;
}
return rdptr != values2Return.end() ? *rdptr : 1023;
}
}
} // namespace adc
} // namespace hal

View File

@ -6,9 +6,9 @@
namespace hal {
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 hal