Skip to content

Commit

Permalink
non-blocking beep
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Sep 14, 2023
1 parent 427fec2 commit 5f5fbed
Show file tree
Hide file tree
Showing 23 changed files with 408 additions and 322 deletions.
68 changes: 55 additions & 13 deletions src/ayab/beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
#include "beeper.h"
#include "board.h"

/*!
* Initialize beeper
*/
void Beeper::init() {
m_currentState = BeepState::Idle;
}

/*!
* Get beeper state
*/
BeepState Beeper::getState() {
return m_currentState;
}

/*!
* Beep to indicate readiness
*/
Expand All @@ -49,23 +63,51 @@ void Beeper::endWork() {
beep(BEEP_NUM_ENDWORK);
}

/* Private Methods */

/*!
* Generic beep function.
*
* /param length number of beeps
* Beep handler scheduled from main loop
*/
void Beeper::beep(uint8_t length) const {

for (uint8_t i = 0U; i < length; ++i) {

void Beeper::schedule() {
long unsigned int now = millis();
switch (m_currentState) {
case BeepState::On:
analogWrite(PIEZO_PIN, BEEP_ON_DUTY);
delay(BEEP_DELAY);

m_currentState = BeepState::Wait;
m_nextState = BeepState::Off;
m_nextTime = now + BEEP_DELAY;
break;
case BeepState::Off:
analogWrite(PIEZO_PIN, BEEP_OFF_DUTY);
delay(BEEP_DELAY);
m_currentState = BeepState::Wait;
m_nextState = BeepState::On;
m_nextTime = now + BEEP_DELAY;
m_repeat--;
break;
case BeepState::Wait:
if (now >= m_nextTime) {
if (m_repeat == 0) {
analogWrite(PIEZO_PIN, BEEP_NO_DUTY);
m_currentState = BeepState::Idle;
} else {
m_currentState = m_nextState;
}
}
break;
case BeepState::Idle:
default:
break;
}
}

analogWrite(PIEZO_PIN, BEEP_NO_DUTY);
/* Private Methods */

/*!
* Generic beep function.
*
* /param repeats number of beeps
*/
void Beeper::beep(uint8_t repeats) {
m_repeat = repeats;
m_currentState = BeepState::Wait;
m_nextState = BeepState::On;
m_nextTime = millis();
}
20 changes: 18 additions & 2 deletions src/ayab/beeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

#include <Arduino.h>

constexpr uint8_t BEEP_DELAY = 50U; // ms
enum class BeepState : unsigned char {Idle, Wait, On, Off};

constexpr unsigned int BEEP_DELAY = 0U; // ms

constexpr uint8_t BEEP_NUM_READY = 5U;
constexpr uint8_t BEEP_NUM_FINISHEDLINE = 3U;
Expand All @@ -41,9 +43,12 @@ class BeeperInterface {
virtual ~BeeperInterface() = default;

// any methods that need to be mocked should go here
virtual void init() = 0;
virtual BeepState getState() = 0;
virtual void ready() = 0;
virtual void finishedLine() = 0;
virtual void endWork() = 0;
virtual void schedule() = 0;
};

// Container class for the static methods that control the beeper.
Expand All @@ -60,22 +65,33 @@ class GlobalBeeper final {
// pointer to global instance whose methods are implemented
static BeeperInterface *m_instance;

static void init();
static BeepState getState();
static void ready();
static void finishedLine();
static void endWork();
static void schedule();
};

/*!
* \brief Class to actuate a beeper connected to PIEZO_PIN
*/
class Beeper : public BeeperInterface {
public:
void init() final;
BeepState getState() final;
void ready() final;
void finishedLine() final;
void endWork() final;
void schedule() final;

private:
void beep(uint8_t length) const;
void beep(uint8_t repeats);

BeepState m_currentState;
BeepState m_nextState;
unsigned long m_nextTime;
uint8_t m_repeat;
};

#endif // BEEPER_H_
2 changes: 1 addition & 1 deletion src/ayab/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ constexpr uint8_t I2Caddr_sol1_8 = 0x0U; ///< I2C Address of solenoids 1 - 8
constexpr uint8_t I2Caddr_sol9_16 = 0x1U; ///< I2C Address of solenoids 9 - 16

// TODO(Who?): Optimize Delay for various Arduino Models
constexpr uint16_t START_KNITTING_DELAY = 2000U;
constexpr uint16_t START_KNITTING_DELAY = 2000U; // ms

// Determine board type
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
Expand Down
4 changes: 2 additions & 2 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void Com::send_reqLine(const uint8_t lineNumber, Err_t error) const {
*/
void Com::send_indState(Carriage_t carriage, uint8_t position,
Err_t error) const {
uint16_t leftHallValue = GlobalEncoders::getHallValue(Left);
uint16_t rightHallValue = GlobalEncoders::getHallValue(Right);
uint16_t leftHallValue = GlobalEncoders::getHallValue(Direction_t::Left);
uint16_t rightHallValue = GlobalEncoders::getHallValue(Direction_t::Right);
uint8_t payload[INDSTATE_LEN] = {
indState_msgid,
static_cast<uint8_t>(error),
Expand Down
62 changes: 31 additions & 31 deletions src/ayab/encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* `m_machineType` assumed valid.
*/
void Encoders::encA_interrupt() {
m_hallActive = NoDirection;
m_hallActive = Direction_t::NoDirection;

auto currentState = static_cast<bool>(digitalRead(ENC_PIN_A));

Expand All @@ -54,9 +54,9 @@ void Encoders::encA_interrupt() {
*/
uint16_t Encoders::getHallValue(Direction_t pSensor) {
switch (pSensor) {
case Left:
case Direction_t::Left:
return analogRead(EOL_PIN_L);
case Right:
case Direction_t::Right:
return analogRead(EOL_PIN_R);
default:
return 0;
Expand All @@ -70,10 +70,10 @@ uint16_t Encoders::getHallValue(Direction_t pSensor) {
void Encoders::init(Machine_t machineType) {
m_machineType = machineType;
m_position = 0U;
m_direction = NoDirection;
m_hallActive = NoDirection;
m_direction = Direction_t::NoDirection;
m_hallActive = Direction_t::NoDirection;
m_beltShift = BeltShift::Unknown;
m_carriage = NoCarriage;
m_carriage = Carriage_t::NoCarriage;
m_oldState = false;
}

Expand Down Expand Up @@ -130,51 +130,51 @@ Machine_t Encoders::getMachineType() {
*/
void Encoders::encA_rising() {
// Update direction
m_direction = digitalRead(ENC_PIN_B) != 0 ? Right : Left;
m_direction = digitalRead(ENC_PIN_B) != 0 ? Direction_t::Right : Direction_t::Left;

// Update carriage position
if ((Right == m_direction) && (m_position < END_RIGHT[m_machineType])) {
if ((Direction_t::Right == m_direction) && (m_position < END_RIGHT[static_cast<uint8_t>(m_machineType)])) {
m_position = m_position + 1;
}

// The garter carriage has a second set of magnets that are going to
// pass the sensor and will reset state incorrectly if allowed to
// continue.
if (m_carriage == Garter) {
if (m_carriage == Carriage_t::Garter) {
return;
}

// If the carriage is already set, ignore the rest.
if (m_carriage == Knit && m_machineType == Kh270) {
if ((m_carriage == Carriage_t::Knit) && (m_machineType == Machine_t::Kh270)) {
return;
}

// In front of Left Hall Sensor?
uint16_t hallValue = analogRead(EOL_PIN_L);
if ((hallValue < FILTER_L_MIN[m_machineType]) ||
(hallValue > FILTER_L_MAX[m_machineType])) {
m_hallActive = Left;
if ((hallValue < FILTER_L_MIN[static_cast<uint8_t>(m_machineType)]) ||
(hallValue > FILTER_L_MAX[static_cast<uint8_t>(m_machineType)])) {
m_hallActive = Direction_t::Left;

Carriage detected_carriage = NoCarriage;
uint8_t start_position = END_LEFT_PLUS_OFFSET[m_machineType];
Carriage detected_carriage = Carriage_t::NoCarriage;
uint8_t start_position = END_LEFT_PLUS_OFFSET[static_cast<uint8_t>(m_machineType)];

if (hallValue >= FILTER_L_MIN[m_machineType]) {
detected_carriage = Knit;
if (hallValue >= FILTER_L_MIN[static_cast<uint8_t>(m_machineType)]) {
detected_carriage = Carriage_t::Knit;
} else {
detected_carriage = Lace;
detected_carriage = Carriage_t::Lace;
}

if (m_machineType == Kh270) {
m_carriage = Knit;
if (m_machineType == Machine_t::Kh270) {
m_carriage = Carriage_t::Knit;

// The first magnet on the carriage looks like Lace, the second looks like Knit
if (detected_carriage == Knit) {
if (detected_carriage == Carriage_t::Knit) {
start_position = start_position + MAGNET_DISTANCE_270;
}
} else if (m_carriage == NoCarriage) {
} else if (m_carriage == Carriage_t::NoCarriage) {
m_carriage = detected_carriage;
} else if (m_carriage != detected_carriage && m_position > start_position) {
m_carriage = Garter;
m_carriage = Carriage_t::Garter;

// Belt shift and start position were set when the first magnet passed
// the sensor and we assumed we were working with a standard carriage.
Expand All @@ -200,10 +200,10 @@ void Encoders::encA_rising() {
*/
void Encoders::encA_falling() {
// Update direction
m_direction = digitalRead(ENC_PIN_B) ? Left : Right;
m_direction = digitalRead(ENC_PIN_B) ? Direction_t::Left : Direction_t::Right;

// Update carriage position
if ((Left == m_direction) && (m_position > END_LEFT[m_machineType])) {
if ((Direction_t::Left == m_direction) && (m_position > END_LEFT[static_cast<uint8_t>(m_machineType)])) {
m_position = m_position - 1;
}

Expand All @@ -214,22 +214,22 @@ void Encoders::encA_falling() {
// by being explicit about that behaviour being expected.
bool hallValueSmall = false;

hallValueSmall = (hallValue < FILTER_R_MIN[m_machineType]);
hallValueSmall = (hallValue < FILTER_R_MIN[static_cast<uint8_t>(m_machineType)]);

if (hallValueSmall || hallValue > FILTER_R_MAX[m_machineType]) {
m_hallActive = Right;
if (hallValueSmall || hallValue > FILTER_R_MAX[static_cast<uint8_t>(m_machineType)]) {
m_hallActive = Direction_t::Right;

// The garter carriage has a second set of magnets that are going to
// pass the sensor and will reset state incorrectly if allowed to
// continue.
if (hallValueSmall && m_carriage != Garter) {
m_carriage = Knit;
if (hallValueSmall && (m_carriage != Carriage_t::Garter)) {
m_carriage = Carriage_t::Knit;
}

// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Shifted : BeltShift::Regular;

// Known position of the carriage -> overwrite position
m_position = END_RIGHT_MINUS_OFFSET[m_machineType];
m_position = END_RIGHT_MINUS_OFFSET[static_cast<uint8_t>(m_machineType)];
}
}
25 changes: 15 additions & 10 deletions src/ayab/encoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,33 @@

// Enumerated constants

enum Direction { NoDirection = -1, Left = 0, Right = 1, NUM_DIRECTIONS = 2 };
enum class Direction : unsigned char {
NoDirection = 0xFF,
Left = 0,
Right = 1
};
constexpr int NUM_DIRECTIONS = 2;
using Direction_t = enum Direction;

enum Carriage {
NoCarriage = -1,
enum class Carriage : unsigned char {
NoCarriage = 0xFF,
Knit = 0,
Lace = 1,
Garter = 2,
NUM_CARRIAGES = 3
Garter = 2
};
constexpr int NUM_CARRIAGES = 3;
using Carriage_t = enum Carriage;

enum MachineType {
NoMachine = -1,
enum class MachineType : unsigned char {
NoMachine = 0xFF,
Kh910 = 0,
Kh930 = 1,
Kh270 = 2,
NUM_MACHINES = 3
Kh270 = 2
};
constexpr int NUM_MACHINES = 3;
using Machine_t = enum MachineType;

enum class BeltShift { Unknown, Regular, Shifted, Lace_Regular, Lace_Shifted };
enum class BeltShift : unsigned char { Unknown, Regular, Shifted, Lace_Regular, Lace_Shifted };
using BeltShift_t = enum BeltShift;

// Machine constants
Expand Down
2 changes: 1 addition & 1 deletion src/ayab/fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void Fsm::state_error() {
// every 500ms
// send `indState` and flash LEDs
unsigned long now = millis();
if (now - m_flashTime >= 500) {
if (now - m_flashTime >= FLASH_DELAY) {
digitalWrite(LED_PIN_A, m_flash); // green LED
digitalWrite(LED_PIN_B, !m_flash); // yellow LED
m_flash = !m_flash;
Expand Down
13 changes: 11 additions & 2 deletions src/ayab/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
#ifndef FSM_H_
#define FSM_H_

enum class OpState {wait_for_machine, init, ready, knit, test, error};
enum class OpState : unsigned char {
wait_for_machine,
init,
ready,
knit,
test,
error
};
using OpState_t = enum OpState;

// As of APIv6, the only important distinction
Expand All @@ -33,7 +40,7 @@ using OpState_t = enum OpState;
// diagnostic purposes (that is, for debugging).
// Non-zero error codes are subject to change.
// Such changes will be considered non-breaking.
enum class ErrorCode {
enum class ErrorCode : unsigned char {
SUCCESS = 0x00,

// message not understood
Expand Down Expand Up @@ -71,6 +78,8 @@ enum class ErrorCode {
};
using Err_t = enum ErrorCode;

constexpr unsigned int FLASH_DELAY = 500; // ms

class FsmInterface {
public:
virtual ~FsmInterface() = default;
Expand Down
Loading

0 comments on commit 5f5fbed

Please sign in to comment.