Files
WII5Firmware/WII5SerialManager.h
scottp 295abb37ee Initial public release of WII5 Buoy firmware
Firmware for an autonomous wave-measurement buoy (ATmega2560-based
WII5 v2 board). Reads wave motion from a Sparton AHRS-M1/M2 IMU,
samples GPS and battery state, and reports back over Iridium SBD
satellite telemetry. Originally developed 2012-2024.

This is the first public release. Code, documentation, and field-tested
operating modes (Capture, Sleep, Position, ManualTest, SelfTest,
LowBattery) are licensed under Apache 2.0 — see LICENSE and NOTICE.

See README.md for an overview and build instructions, CONTRIBUTING.md
for how to contribute, and DEPLOYMENTS.md for the field-deployment log.
2026-05-07 16:27:18 +10:00

163 lines
3.9 KiB
C++

// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2012-2024 Scott Penrose <scottp@dd.com.au> and WII5 Buoy contributors
//
// This file is part of WII5 Buoy firmware.
// See LICENSE for full terms.
/**
* @file WII5SerialManager.h
* @brief Serial-port helper: AT-response parsing, command queue.
*/
#ifndef WII5SerialManager_h
#define WII5SerialManager_h
#include <Arduino.h>
#include <WII5_board.h>
#include <WII5Data.h>
#include <WII5Sh3dConsole.h>
#include <elapsedMillis.h>
#include <TimeLib.h>
#include <TinyGPS++.h>
// TODO Really?
#define WII5SERIALMAANGER_FIELD_SIZE 25
/*
WII5SerialManager - Base class for managed serial ports
Special and Shared Types
WII5SERIAL_LAST
What was the last message we received
WII5SERIAL_PARSERS
Which parser we currently using?
*/
enum WII5SERIAL_PARSERS {
WII5SERIALPARSER_NONE,
WII5SERIALPARSER_LINE,
WII5SERIALPARSER_BINARY1, // likely to have multiple binary fomrats, this one, has the length in the first 2 bytes
// and last 2 bytes are checksum
WII5SERIALPARSER_RECORD,
WII5SERIALPARSER_OKHUH,
WII5SERIALPARSER_AT
};
enum WII5SERIAL_LAST {
WII5SERIALLAST_NONE,
WII5SERIALLAST_LINE,
WII5SERIALLAST_RECORD,
WII5SERIALLAST_OK,
WII5SERIALLAST_READY,
WII5SERIALLAST_ERROR,
WII5SERIALLAST_ATOK, // OK
WII5SERIALLAST_ATERR,
WII5SERIALLAST_ATSBDI, // including +SBDI and +SBDIX
WII5SERIALLAST_BOOTVERSION,
WII5SERIALLAST_NUMBER,
WII5SERIALLAST_ATCSQ // including +CSQ
};
class WII5SerialManager {
public:
WII5SerialManager() {
gps = NULL;
}
virtual WII5_DRIVERS driverId();
virtual void loop();
void beginSerialManager();
void endSerialManager();
void setPassthrough(bool in);
bool getPassthrough();
void setDebug(bool in);
bool getDebug();
void setCapture(bool in);
bool getCapture();
void repl(uint32_t baud = 0);
virtual void sendNewLine();
WII5_SERIALCMDS sendAll(WII5SERIAL_LAST lastUntil = WII5SERIALLAST_OK);
WII5_SERIALCMDS sendAndWait(WII5SERIAL_LAST lastUntil = WII5SERIALLAST_OK);
virtual void setBaudrate(uint32_t baud);
virtual void start(bool force = false);
virtual void stop(bool force = false);
virtual void setRecords(uint32_t in);
virtual uint32_t getRecords();
virtual void writePassthrough();
virtual void writeCapture();
// Public to allow easy access???
TinyGPSPlus *gps;
void enableNmea();
// virtual void displaySTATS();
virtual void sendAtDataLine();
void setTimeout(uint32_t in);
uint32_t getTimeout();
char* buffer;
char* binBuffer;
uint16_t binBufferCount;
uint16_t binBufferExpect;
protected:
// Serial stream
// TODO Stream or HardwareSerial
HardwareSerial *stream;
// Must be implemented in inherited class !
bool capture; // Capture to file - see openFile and closeFile
bool passthrough; // Passthrough to Serial console
bool debug; // Extra debugging to Serial Console
uint32_t sendCount;
virtual bool processSerial(char in);
virtual void processBufferField(char *buf, uint8_t len);
virtual void processBufferOK();
virtual void processBufferLine();
virtual void processRecord();
elapsedMillis processWait;
uint32_t processTimeout;
virtual void programLine(uint16_t l);
virtual void sendLine(uint16_t l);
virtual void consolePrefix(bool input = false);
virtual void consoleBuffer(bool input = false, bool useLog = false);
virtual void setBuffer(char* buf, uint8_t sz);
uint8_t bufMax;
char fieldBuffer[WII5SERIALMAANGER_FIELD_SIZE]; // TODO Configuration etc
uint8_t bufCount;
uint8_t fieldLoc;
uint8_t fieldCount;
WII5SERIAL_PARSERS processMode;
uint8_t programCount;
uint8_t programTotal;
uint32_t recordCount;
uint32_t recordTotal;
WII5SERIAL_LAST last;
uint32_t lastVal;
uint32_t recordVals[WII5_FIELD_MAX];
char *p;
};
#endif