Reworking the AVR specific stuff into an AVRInterface class.

This commit is contained in:
2026-01-24 16:43:07 -06:00
parent 2ba02318db
commit 30f4047b7c
8 changed files with 298 additions and 20 deletions

View File

@@ -0,0 +1,111 @@
#pragma once
#include <stdint.h>
class AVRInterface {
public:
/// @brief
/// @param SendMessage is the function this AVRInterface calls to send a message to the device
///
AVRInterface(int (*SendMessage)(const uint8_t *buffer, uint16_t len))
: SendMethod(SendMessage) {};
~AVRInterface();
/// @brief When the system receives something from the device, give it to this function to handle it
/// @param buffer
/// @param len
/// @return true if it was handled
///
bool HandleMessage(const uint8_t *buffer, uint16_t len);
/// @brief Call this periodically so timed activities can be handled.
///
/// Every 1 or even 50 to 100 msec is ok.
///
/// @param millisec
///
void Tick(uint32_t millisec);
/// @brief Initialize the AVR interface and issue the ready command.
///
/// This is temporarily blocking since nothing else should run until ready
///
/// @return true if initialized
///
bool Initialize();
/// @brief determine if the device is ready
///
/// @return true if ready
///
bool IsOnline();
/// @brief Power on and off commands
///
typedef enum {
avrPowerOff,
avrPowerOn,
} AVRPower_T;
/// @brief Send the power command
/// @param cmd
/// @return true if sent
///
bool Power(AVRPower_T cmd);
// MasterVolumeButton
// Issues volume up and down commands to the AVR.
//
typedef enum {
avrVolumeUp,
avrVolumeDown,
} AVRVolumeButton_T;
bool MasterVolumeButton(AVRVolumeButton_T cmd);
// Mute, UnMute
typedef enum {
avrMute,
avrUnMute,
} AVRMute_T;
bool Mute(AVRMute_T cmd);
private:
uint32_t sentAtTime_ms;
typedef enum {
stPoweringUp,
stAwaitingReadyResponse,
stInitializing,
stRetryInitializing,
stReady,
stAwaitingResponse,
} AVRState_T;
AVRState_T state = stPoweringUp;
AVRState_T GetState() {
return state;
}
bool readyResponsReceived;
int readyTries;
#define RETRY_INTERVAL_ms 500
#define MAXTRIES 5
#define SERIALQUEUESIZE 5
typedef struct {
uint8_t *messageToSend;
uint16_t len;
} SerialQueue_T;
SerialQueue_T serialQueue[SERIALQUEUESIZE];
int serialQueueCount = 0;
bool ProcessSerialQueue(const uint8_t *p = NULL, uint16_t len = NULL);
int (*SendMethod)(const uint8_t *buffer, uint16_t bufferSize);
bool CheckTheChecksum(const uint8_t *szBuffer, uint32_t num);
unsigned long Hex2Dec(const uint8_t *p, int dig);
void FreeMemory();
};