Skip to content

Commit

Permalink
Add HardwareTestInterface class, GlobalHardwareTest class.
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Aug 16, 2020
1 parent eaad74f commit e397e56
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 158 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ matrix:
sources:
- ubuntu-toolchain-r-test
packages:
- g++
- g++-9
- gcc-9
env:
- MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"
- MATRIX_EVAL="CC=gcc-9 && CXX=g++-9"
before_install:
- eval "${MATRIX_EVAL}"
- pip install gcovr
Expand Down
87 changes: 87 additions & 0 deletions src/ayab/global_hw_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*!
* \file global_hw_test.cpp
* 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
*/

#include "hw_test.h"

// static member functions

void GlobalHardwareTest::helpCmd() {
m_instance->helpCmd();
}

void GlobalHardwareTest::sendCmd() {
m_instance->sendCmd();
}

void GlobalHardwareTest::beepCmd() {
m_instance->beepCmd();
}

void GlobalHardwareTest::setSingleCmd() {
m_instance->setSingleCmd();
}

void GlobalHardwareTest::setAllCmd() {
m_instance->setAllCmd();
}

void GlobalHardwareTest::readEOLsensorsCmd() {
m_instance->readEOLsensorsCmd();
}

void GlobalHardwareTest::readEncodersCmd() {
m_instance->readEncodersCmd();
}

void GlobalHardwareTest::autoReadCmd() {
m_instance->autoReadCmd();
}

void GlobalHardwareTest::autoTestCmd() {
m_instance->autoTestCmd();
}

void GlobalHardwareTest::stopCmd() {
m_instance->stopCmd();
}

void GlobalHardwareTest::quitCmd() {
m_instance->quitCmd();
}

void GlobalHardwareTest::unrecognizedCmd(const char *buffer) {
m_instance->unrecognizedCmd(buffer);
}

void GlobalHardwareTest::setUp() {
m_instance->setUp();
}

void GlobalHardwareTest::loop() {
m_instance->loop();
}

#ifndef AYAB_TESTS
void GlobalHardwareTest::encoderAChange() {
m_instance->encoderAChange();
}
#endif // AYAB_TESTS
162 changes: 83 additions & 79 deletions src/ayab/hw_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@
#include "hw_test.h"
#include "knitter.h"

extern Knitter *knitter;

// initialise static members
SerialCommand HardwareTest::m_sCmd = SerialCommand();
bool HardwareTest::m_autoReadOn = false;
bool HardwareTest::m_autoTestOn = false;
bool HardwareTest::m_timerEvent = false;
bool HardwareTest::m_timerEventOdd = false;
unsigned long HardwareTest::m_lastTime = 0U;
// public interface

/*!
* \brief Help command handler.
*/
void HardwareTest::helpCmd() {
Serial.println("The following commands are available:");
Serial.println("setSingle [0..15] [1/0]");
Expand All @@ -50,17 +45,16 @@ void HardwareTest::helpCmd() {
Serial.println("help");
}

/*!
* \brief Send command handler.
*/
void HardwareTest::sendCmd() {
Serial.println("Called send");
uint8_t p[] = {1, 2, 3};
knitter->send(p, 3);
Serial.print("\n");
}

void HardwareTest::beep() {
knitter->m_beeper.ready();
}

/*!
* \brief Beep command handler.
*/
Expand Down Expand Up @@ -117,13 +111,6 @@ void HardwareTest::setAllCmd() {
knitter->setSolenoids(solenoidState);
}

void HardwareTest::readEOLsensors() {
Serial.print(" EOL_L: ");
Serial.print(analogRead(EOL_PIN_L));
Serial.print(" EOL_R: ");
Serial.print(analogRead(EOL_PIN_R));
}

/*!
* \brief Read EOL sensors command handler.
*/
Expand All @@ -133,18 +120,6 @@ void HardwareTest::readEOLsensorsCmd() {
Serial.print("\n");
}

void HardwareTest::readEncoders() {
Serial.print(" ENC_A: ");
bool state = digitalRead(ENC_PIN_A);
Serial.print(state ? "HIGH" : "LOW");
Serial.print(" ENC_B: ");
state = digitalRead(ENC_PIN_B);
Serial.print(state ? "HIGH" : "LOW");
Serial.print(" ENC_C: ");
state = digitalRead(ENC_PIN_C);
Serial.print(state ? "HIGH" : "LOW");
}

/*!
* \brief Read encoders command handler.
*/
Expand All @@ -154,13 +129,6 @@ void HardwareTest::readEncodersCmd() {
Serial.print("\n");
}

void HardwareTest::autoRead() {
Serial.print("\n");
readEOLsensors();
readEncoders();
Serial.print("\n");
}

/*!
* \brief Auto read command handler.
*/
Expand All @@ -169,20 +137,6 @@ void HardwareTest::autoReadCmd() {
m_autoReadOn = true;
}

void HardwareTest::autoTestEven() {
Serial.println("Set even solenoids");
digitalWrite(LED_PIN_A, 1);
digitalWrite(LED_PIN_B, 1);
knitter->setSolenoids(0xAAAA);
}

void HardwareTest::autoTestOdd() {
Serial.println("Set odd solenoids");
digitalWrite(LED_PIN_A, 0);
digitalWrite(LED_PIN_B, 0);
knitter->setSolenoids(0x5555);
}

/*!
* \brief Auto test command handler.
*/
Expand All @@ -191,11 +145,17 @@ void HardwareTest::autoTestCmd() {
m_autoTestOn = true;
}

/*!
* \brief Stop command handler.
*/
void HardwareTest::stopCmd() {
m_autoReadOn = false;
m_autoTestOn = false;
}

/*!
* \brief Quit command handler.
*/
void HardwareTest::quitCmd() {
knitter->setQuitFlag(true);
knitter->setUpInterrupt();
Expand All @@ -215,25 +175,23 @@ void HardwareTest::unrecognizedCmd(const char *buffer) {
helpCmd();
}

// Member functions

/*!
* \brief Setup for hw tests.
* \brief Setup for hardware tests.
*/
void HardwareTest::setUp() {
// set up callbacks for SerialCommand commands
m_sCmd.addCommand("setSingle", HardwareTest::setSingleCmd);
m_sCmd.addCommand("setAll", HardwareTest::setAllCmd);
m_sCmd.addCommand("readEOLsensors", HardwareTest::readEOLsensorsCmd);
m_sCmd.addCommand("readEncoders", HardwareTest::readEncodersCmd);
m_sCmd.addCommand("beep", HardwareTest::beepCmd);
m_sCmd.addCommand("autoRead", HardwareTest::autoReadCmd);
m_sCmd.addCommand("autoTest", HardwareTest::autoTestCmd);
m_sCmd.addCommand("send", HardwareTest::sendCmd);
m_sCmd.addCommand("stop", HardwareTest::stopCmd);
m_sCmd.addCommand("quit", HardwareTest::quitCmd);
m_sCmd.addCommand("help", HardwareTest::helpCmd);
m_sCmd.setDefaultHandler(HardwareTest::unrecognizedCmd);
m_sCmd.addCommand("setSingle", GlobalHardwareTest::setSingleCmd);
m_sCmd.addCommand("setAll", GlobalHardwareTest::setAllCmd);
m_sCmd.addCommand("readEOLsensors", GlobalHardwareTest::readEOLsensorsCmd);
m_sCmd.addCommand("readEncoders", GlobalHardwareTest::readEncodersCmd);
m_sCmd.addCommand("beep", GlobalHardwareTest::beepCmd);
m_sCmd.addCommand("autoRead", GlobalHardwareTest::autoReadCmd);
m_sCmd.addCommand("autoTest", GlobalHardwareTest::autoTestCmd);
m_sCmd.addCommand("send", GlobalHardwareTest::sendCmd);
m_sCmd.addCommand("stop", GlobalHardwareTest::stopCmd);
m_sCmd.addCommand("quit", GlobalHardwareTest::quitCmd);
m_sCmd.addCommand("help", GlobalHardwareTest::helpCmd);
m_sCmd.setDefaultHandler(GlobalHardwareTest::unrecognizedCmd);

// Print welcome message
Serial.print("AYAB Hardware Test, Firmware v");
Expand All @@ -248,25 +206,16 @@ void HardwareTest::setUp() {
// attach interrupt for ENC_PIN_A(=2), interrupt #0
detachInterrupt(0);
#ifndef AYAB_TESTS
attachInterrupt(0, encoderAChange, RISING);
attachInterrupt(0, GlobalHardwareTest::encoderAChange, RISING);
#endif // AYAB_TESTS

m_lastTime = millis();
#endif // AYAB_TESTS
m_autoReadOn = false;
m_autoTestOn = false;
m_timerEventOdd = false;
knitter->setQuitFlag(false);
}

#ifndef AYAB_TESTS
/*!
* \brief Interrupt service routine for encoder A.
*/
void HardwareTest::encoderAChange() {
beep();
}
#endif // AYAB_TESTS

/*!
* \brief Main loop for hardware tests.
*/
Expand All @@ -278,6 +227,61 @@ void HardwareTest::loop() {
}
}

// Private member functions

void HardwareTest::beep() {
knitter->m_beeper.ready();
}

void HardwareTest::readEncoders() {
Serial.print(" ENC_A: ");
bool state = digitalRead(ENC_PIN_A);
Serial.print(state ? "HIGH" : "LOW");
Serial.print(" ENC_B: ");
state = digitalRead(ENC_PIN_B);
Serial.print(state ? "HIGH" : "LOW");
Serial.print(" ENC_C: ");
state = digitalRead(ENC_PIN_C);
Serial.print(state ? "HIGH" : "LOW");
}

void HardwareTest::readEOLsensors() {
Serial.print(" EOL_L: ");
Serial.print(analogRead(EOL_PIN_L));
Serial.print(" EOL_R: ");
Serial.print(analogRead(EOL_PIN_R));
}

void HardwareTest::autoRead() {
Serial.print("\n");
readEOLsensors();
readEncoders();
Serial.print("\n");
}

void HardwareTest::autoTestEven() {
Serial.println("Set even solenoids");
digitalWrite(LED_PIN_A, 1);
digitalWrite(LED_PIN_B, 1);
knitter->setSolenoids(0xAAAA);
}

void HardwareTest::autoTestOdd() {
Serial.println("Set odd solenoids");
digitalWrite(LED_PIN_A, 0);
digitalWrite(LED_PIN_B, 0);
knitter->setSolenoids(0x5555);
}

/*!
* \brief Interrupt service routine for encoder A.
*/
#ifndef AYAB_TESTS
void HardwareTest::encoderAChange() {
beep();
}
#endif // AYAB_TESTS

/*!
* \brief Timer event every 500ms to handle auto functions.
*/
Expand Down
Loading

0 comments on commit e397e56

Please sign in to comment.