-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
996 additions
and
648 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/*! | ||
* \file fsm.cpp | ||
* \brief Singleton class containing methods for the finite state machine | ||
* that co-ordinates the AYAB firmware. | ||
* | ||
* This file is part of AYAB. | ||
* | ||
* AYAB is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* AYAB is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with AYAB. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Original Work Copyright 2013-2015 Christian Obersteiner, Andreas Müller | ||
* Modified Work Copyright 2020 Sturla Lange, Tom Price | ||
* http://ayab-knitting.com | ||
*/ | ||
|
||
#include "board.h" | ||
#include <Arduino.h> | ||
|
||
#include "com.h" | ||
#include "fsm.h" | ||
#include "knitter.h" | ||
|
||
// Public methods | ||
|
||
void Fsm::init() { | ||
m_opState = s_init; | ||
} | ||
|
||
OpState_t Fsm::getState() { | ||
return m_opState; | ||
} | ||
|
||
void Fsm::setState(OpState_t state) { | ||
m_opState = state; | ||
} | ||
|
||
/*! | ||
* \brief Dispatch on machine state | ||
* | ||
* \todo TP: add error state(s) | ||
*/ | ||
void Fsm::dispatch() { | ||
switch (m_opState) { | ||
case s_init: | ||
state_init(); | ||
break; | ||
|
||
case s_ready: | ||
state_ready(); | ||
break; | ||
|
||
case s_knit: | ||
state_knit(); | ||
break; | ||
|
||
case s_test: | ||
state_test(); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
GlobalCom::update(); | ||
} | ||
|
||
// Private methods | ||
|
||
void Fsm::state_init() { | ||
if (GlobalKnitter::isReady()) { | ||
setState(s_ready); | ||
} | ||
} | ||
|
||
void Fsm::state_ready() { | ||
digitalWrite(LED_PIN_A, 0); // green LED off | ||
} | ||
|
||
void Fsm::state_knit() { | ||
digitalWrite(LED_PIN_A, 1); // green LED on | ||
GlobalKnitter::knit(); | ||
} | ||
|
||
void Fsm::state_test() { | ||
GlobalKnitter::encodePosition(); | ||
GlobalTester::loop(); | ||
if (GlobalTester::getQuitFlag()) { | ||
setState(s_ready); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*!` | ||
* \file fsm.h | ||
* | ||
* This file is part of AYAB. | ||
* | ||
* AYAB is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* AYAB is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with AYAB. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller | ||
* Modified Work Copyright 2020 Sturla Lange, Tom Price | ||
* http://ayab-knitting.com | ||
*/ | ||
|
||
#ifndef FSM_H_ | ||
#define FSM_H_ | ||
|
||
enum OpState { s_init, s_ready, s_knit, s_test }; | ||
using OpState_t = enum OpState; | ||
|
||
class FsmInterface { | ||
public: | ||
virtual ~FsmInterface(){}; | ||
|
||
// any methods that need to be mocked should go here | ||
virtual void init() = 0; | ||
virtual OpState_t getState() = 0; | ||
virtual void setState(OpState_t state) = 0; | ||
virtual void dispatch() = 0; | ||
}; | ||
|
||
// Singleton container class for static methods. | ||
// Dependency injection is enabled using a pointer | ||
// to a global instance of either `Knitter` or `KnitterMock` | ||
// both of which classes implement the pure virtual methods | ||
// of the `KnitterInterface` class. | ||
|
||
class GlobalFsm final { | ||
private: | ||
// singleton class so private constructor is appropriate | ||
GlobalFsm() = default; | ||
|
||
public: | ||
// pointer to global instance whose methods are implemented | ||
static FsmInterface *m_instance; | ||
|
||
static void init(); | ||
static OpState_t getState(); | ||
static void setState(OpState_t state); | ||
static void dispatch(); | ||
}; | ||
|
||
class Fsm : public FsmInterface { | ||
#if AYAB_TESTS | ||
#endif | ||
|
||
public: | ||
void init(); | ||
OpState_t getState(); | ||
void setState(OpState_t state); | ||
void dispatch(); | ||
|
||
private: | ||
void state_init(); | ||
void state_ready(); | ||
void state_knit(); | ||
void state_test(); | ||
|
||
// machine state | ||
OpState_t m_opState; | ||
}; | ||
|
||
#endif // FSM_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.