Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finite Machine states inherit from OpInterface #154

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[submodule "libraries/PacketSerial"]
[submodule "lib/PacketSerial"]
active = true
path = lib/PacketSerial
url = https://github.com/bakercp/PacketSerial.git
[submodule "libraries/Adafruit_MCP23008"]
url = https://github.com/adafruit/Adafruit-MCP23008-library.git
[submodule "lib/Adafruit_MCP23008"]
active = true
path = lib/Adafruit_MCP23008
url = https://github.com/adafruit/Adafruit-MCP23008-library.git
[submodule "lib/avr-libc"]
active = true
path = lib/avr-libc
url = https://github.com/avrdudes/avr-libc.git
17 changes: 10 additions & 7 deletions doc/finite_state_machine.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
### Finite State Machine

The finite state machine is defined in the `Fsm` class.
The finite state machine is defined in the `Controller` class.

| State | Action |
--: | :--
`Idle` | Wait for information on machine type.
`Init` | Wait for carriage to be put in the correct location.
`Ready` | Wait to start operation.
`Knit` | Operate in knitting mode.
Expand All @@ -14,9 +15,11 @@ A tabular representation of state transitions follows.

| Transition | Function / condition |
--: | :--
`Init -> Test` | `Tester::startTest()`
`Ready -> Test` | `Tester::startTest()`
`Test -> Init` | `Tester::quitCmd()`
`Init -> Ready` | `Knitter::isReady()`
`Ready -> Knit` | `Knitter::startKnitting()`
`Knit -> Ready` | `m_workedOnLine && m_lastLineFlag`
`Idle -> Init` | `Com::h_reqInit()`
`Init -> Ready` | `OpInit::update()`
`Ready -> Knit` | `OpKnit::startKnitting()`
`Init -> Test` | `OpTest::startTest()`
`Ready -> Test` | `OpTest::startTest()`
`Knit -> Test` | `OpTest::startTest()`
`Knit -> Init` | `m_workedOnLine && m_lastLineFlag`
`Test -> Init` | `OpTest::end()`
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ default_envs = uno

[env]
framework = arduino
extra_scripts =
pre:scripts/preBuild.py
extra_scripts =
pre:scripts/preBuild.py

[env:uno]
platform = atmelavr
Expand Down
2 changes: 1 addition & 1 deletion scripts/preBuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Import("env")
print("Pre build script")

# Reads the current git tag of the repo and returns the version number
# Reads the current git tag of the repo and returns the version number
# elements
# In case there are changes since the last tag, the dirty flag is set
# In case the git tag does not match the x.y.z format, 0.0.0 is used as fallback
Expand Down
58 changes: 58 additions & 0 deletions src/ayab/atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <avr/io.h>
#include <avr/interrupt.h>

/* Inline assembly suggested by @jpcornil-git */

#define ENTER_CRITICAL() __asm__ __volatile__ ( \
"in __tmp_reg__, __SREG__" "\n\t" \
"cli" "\n\t" \
"push __tmp_reg__" "\n\t" \
::: "memory" \
)

#define EXIT_CRITICAL() __asm__ __volatile__ ( \
"pop __tmp_reg__" "\n\t" \
"out __SREG__, __tmp_reg__" "\n\t" \
::: "memory" \
)

/* Suggested by @jpcornil-git based on https://arduino.stackexchange.com/questions/77494/which-arduinos-support-atomic-block */

#define ATOMIC_BLOCK(type) for(type; type##_OBJECT_NAME.run(); type##_OBJECT_NAME.stop())
#define ATOMIC_RESTORESTATE_OBJECT_NAME atomicBlockRestoreState_
#define ATOMIC_RESTORESTATE AtomicBlockRestoreState ATOMIC_RESTORESTATE_OBJECT_NAME

class AtomicBlockRestoreState
{
public:
// Constructor: called when the object is created
inline AtomicBlockRestoreState()
{
sreg_save = SREG; // save status register
cli(); // turn interrupts OFF
}

// Destructor: called when the object is destroyed (ex: goes out-of-scope)
inline ~AtomicBlockRestoreState()
{
SREG = sreg_save; // restore status register
__asm__ volatile ("" ::: "memory"); // memory barrier
}

// Can we run? Returns true to run the `for` loop or
// `false` to stop it.
inline bool run()
{
return run_now;
}

// Tell the `for` loop to stop
inline void stop()
{
run_now = false;
}

private:
bool run_now = true;
uint8_t sreg_save;
};
4 changes: 2 additions & 2 deletions src/ayab/beeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void Beeper::endWork() {
/*!
* Beep handler scheduled from main loop
*/
void Beeper::schedule() {
void Beeper::update() {
long unsigned int now = millis();
switch (m_currentState) {
case BeepState::On:
Expand All @@ -107,7 +107,7 @@ void Beeper::schedule() {
}
break;
case BeepState::Idle:
default:
default: // GCOVR_EXCL_LINE
break;
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/ayab/beeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* 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
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

Expand All @@ -44,12 +44,12 @@ class BeeperInterface {

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

// Container class for the static methods that control the beeper.
Expand All @@ -67,12 +67,12 @@ class GlobalBeeper final {
static BeeperInterface *m_instance;

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

/*!
Expand All @@ -81,12 +81,12 @@ class GlobalBeeper final {
class Beeper : public BeeperInterface {
public:
void init(bool enabled) final;
bool enabled() final;
BeepState getState() final;
void update() final;
void ready() final;
void finishedLine() final;
void endWork() final;
void schedule() final;
BeepState getState() final;
bool enabled() final;

private:
void beep(uint8_t repeats);
Expand Down
Loading