unit testing

This commit is contained in:
2021-01-27 08:27:08 +01:00
parent f78f3c5eaa
commit d36d67bd2c
20 changed files with 360 additions and 78 deletions

View File

@@ -44,5 +44,6 @@
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
},
"cmake.configureOnOpen": true
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

View File

@@ -0,0 +1,100 @@
// Arduino Button Library
// https://github.com/JChristensen/JC_Button
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
#include "JC_Button.h"
/*----------------------------------------------------------------------*
/ initialize a Button object and the pin it's connected to. *
/-----------------------------------------------------------------------*/
void Button::begin()
{
pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT);
m_state = digitalRead(m_pin);
if (m_invert) m_state = !m_state;
m_time = millis();
m_lastState = m_state;
m_changed = false;
m_lastChange = m_time;
}
/*----------------------------------------------------------------------*
/ returns the state of the button, true if pressed, false if released. *
/ does debouncing, captures and maintains times, previous state, etc. *
/-----------------------------------------------------------------------*/
bool Button::read()
{
uint32_t ms = millis();
bool pinVal = digitalRead(m_pin);
if (m_invert) pinVal = !pinVal;
if (ms - m_lastChange < m_dbTime)
{
m_changed = false;
}
else
{
m_lastState = m_state;
m_state = pinVal;
m_changed = (m_state != m_lastState);
if (m_changed) m_lastChange = ms;
}
m_time = ms;
return m_state;
}
/*----------------------------------------------------------------------*
* isPressed() and isReleased() check the button state when it was last *
* read, and return false (0) or true (!=0) accordingly. *
* These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
bool Button::isPressed()
{
return m_state;
}
bool Button::isReleased()
{
return !m_state;
}
/*----------------------------------------------------------------------*
* wasPressed() and wasReleased() check the button state to see if it *
* changed between the last two reads and return false (0) or *
* true (!=0) accordingly. *
* These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
bool Button::wasPressed()
{
return m_state && m_changed;
}
bool Button::wasReleased()
{
return !m_state && m_changed;
}
/*----------------------------------------------------------------------*
* pressedFor(ms) and releasedFor(ms) check to see if the button is *
* pressed (or released), and has been in that state for the specified *
* time in milliseconds. Returns false (0) or true (!=0) accordingly. *
* These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
bool Button::pressedFor(uint32_t ms)
{
return m_state && m_time - m_lastChange >= ms;
}
bool Button::releasedFor(uint32_t ms)
{
return !m_state && m_time - m_lastChange >= ms;
}
/*----------------------------------------------------------------------*
* lastChange() returns the time the button last changed state, *
* in milliseconds. *
*----------------------------------------------------------------------*/
uint32_t Button::lastChange()
{
return m_lastChange;
}

View File

@@ -0,0 +1,111 @@
// Arduino Button Library
// https://github.com/JChristensen/JC_Button
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
#ifndef JC_BUTTON_H_INCLUDED
#define JC_BUTTON_H_INCLUDED
#include <Arduino.h>
class Button
{
public:
// Button(pin, dbTime, puEnable, invert) instantiates a button object.
//
// Required parameter:
// pin The Arduino pin the button is connected to
//
// Optional parameters:
// dbTime Debounce time in milliseconds (default 25ms)
// puEnable true to enable the AVR internal pullup resistor (default true)
// invert true to interpret a low logic level as pressed (default true)
Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
: m_pin(pin), m_dbTime(dbTime), m_puEnable(puEnable), m_invert(invert) {}
// Initialize a Button object and the pin it's connected to
void begin();
// Returns the current debounced button state, true for pressed,
// false for released. Call this function frequently to ensure
// the sketch is responsive to user input.
bool read();
// Returns true if the button state was pressed at the last call to read().
// Does not cause the button to be read.
bool isPressed();
// Returns true if the button state was released at the last call to read().
// Does not cause the button to be read.
bool isReleased();
// Returns true if the button state at the last call to read() was pressed,
// and this was a change since the previous read.
bool wasPressed();
// Returns true if the button state at the last call to read() was released,
// and this was a change since the previous read.
bool wasReleased();
// Returns true if the button state at the last call to read() was pressed,
// and has been in that state for at least the given number of milliseconds.
bool pressedFor(uint32_t ms);
// Returns true if the button state at the last call to read() was released,
// and has been in that state for at least the given number of milliseconds.
bool releasedFor(uint32_t ms);
// Returns the time in milliseconds (from millis) that the button last
// changed state.
uint32_t lastChange();
private:
uint8_t m_pin; // arduino pin number connected to button
uint32_t m_dbTime; // debounce time (ms)
bool m_puEnable; // internal pullup resistor enabled
bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
bool m_state; // current button state, true=pressed
bool m_lastState; // previous button state
bool m_changed; // state changed since last read
uint32_t m_time; // time of current state (ms from millis)
uint32_t m_lastChange; // time of last state change (ms)
};
// a derived class for a "push-on, push-off" (toggle) type button.
// initial state can be given, default is off (false).
class ToggleButton : public Button
{
public:
// constructor is similar to Button, but includes the initial state for the toggle.
ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
: Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {}
// read the button and return its state.
// should be called frequently.
bool read()
{
Button::read();
if (wasPressed())
{
m_toggleState = !m_toggleState;
m_changed = true;
}
else
{
m_changed = false;
}
return m_toggleState;
}
// has the state changed?
bool changed() {return m_changed;}
// return the current state
bool toggleState() {return m_toggleState;}
private:
bool m_toggleState;
bool m_changed;
};
#endif

View File

@@ -15,5 +15,11 @@ framework = arduino
upload_port = stlink
debug_tool = stlink
lib_deps =
http://192.168.2.3/Bonobo.Git.Server/JCButton.git
# http://192.168.2.3/Bonobo.Git.Server/JCButton.git
[env:native]
platform = native
build_flags = -std=gnu++11
lib_deps =
ArduinoFake

View File

@@ -1,27 +1,55 @@
#ifndef BOARDH
#define BOARDH
#define LED1 PB0 //D3
#define LED2 PB7 //D4
#define LED3 PB6 //D5
#ifndef UNIT_TEST
#define DETECT1 PA6 //A5
#define DETECT2 PA5 //A4
#define DETECT3 PA4 //A3
#define LED1 PB0 //D3
#define LED2 PB7 //D4
#define LED3 PB6 //D5
#define SWITCH1 PA7 //A6 TOGGLE1
#define SWITCH12 PA2 //A7 MOMENTARY1
#define SWITCH2 PA1 //A1 TOGGLE1
#define SWITCH22 PA3 //A2 MOMENTARY1
#define SWITCH3 PB5 //D11 TOGGLE1
#define SWITCH32 PB4 //D12 MOMENTARY1
#define DETECT1 PA6 //A5
#define DETECT2 PA5 //A4
#define DETECT3 PA4 //A3
#define LD3LED PB3
#define WAKEUPPIN PA2
#define SWITCH1 PA7 //A6 TOGGLE1
#define SWITCH12 PA2 //A7 MOMENTARY1
#define SWITCH2 PA1 //A1 TOGGLE1
#define SWITCH22 PA3 //A2 MOMENTARY1
#define SWITCH3 PB5 //D11 TOGGLE1
#define SWITCH32 PB4 //D12 MOMENTARY1
#define REDLEDRES
#define YELLOWLEDRES
#define GREENLEDRES
#define LD3LED PB3
#define WAKEUPPIN PA2
#define REDLEDRES
#define YELLOWLEDRES
#define GREENLEDRES
#else
#define LED1 0 //D3
#define LED2 1 //D4
#define LED3 2 //D5
#define DETECT1 3 //A5
#define DETECT2 4 //A4
#define DETECT3 5 //A3
#define SWITCH1 6 //A6 TOGGLE1
#define SWITCH12 7 //A7 MOMENTARY1
#define SWITCH2 8 //A1 TOGGLE1
#define SWITCH22 9 //A2 MOMENTARY1
#define SWITCH3 10 //D11 TOGGLE1
#define SWITCH32 11 //D12 MOMENTARY1
#define LD3LED 12
#define WAKEUPPIN 13
#define REDLEDRES
#define YELLOWLEDRES
#define GREENLEDRES
#endif //unit_test
#endif //BOARDH

View File

@@ -1,8 +1,6 @@
#include "buttons.h"
#include "board.h"
#include <vector>
#include "Arduino.h"
#include "JC_Button.h"
std::vector<c_button *> buttonlist;

View File

@@ -1,9 +1,12 @@
#ifndef BUTTONSH
#define BUTTONSH
#include <Arduino.h>
#include "Arduino.h"
#include <vector>
#include "JC_Button.h"
#ifndef UNIT_TEST
#include "JC_Button.h"
#endif
#include "board.h"
#include "led.h"
class c_button : public ToggleButton

View File

@@ -1,30 +1,21 @@
#include "chainGame.h"
#include "Arduino.h"
#include "buttons.h"
#ifndef UNIT_TEST
#ifdef ARDUINO
#include "chainGame.h"
uint8_t patternIndex = 0;
bool patternFlag = false;
bool firstpattern = false;
//e_ledcolor cheatbutton = NONE;
//bool firstpattern = false;
uint16_t cheatbutton = 0;
bool cheatButtonFlag = false;
// e_ledcolor ledpattern[4] =
// {
// YELLOW,
// GREEN,
// YELLOW,
// RED};
uint16_t ledpattern[4] =
{
1,
3,
1,
2
};
{
1,
3,
1,
2};
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
@@ -44,36 +35,44 @@ void ResetChainGame(void)
{
patternIndex = 0;
patternFlag = false;
firstpattern = false;
//firstpattern = false;
cheatbutton = 0;
cheatButtonFlag = false;
}
void HandleChainGame(void)
{
if (buttonIsPressed(ledpattern[patternIndex]) && !patternFlag && !cheatButtonFlag)
if (!patternFlag && !cheatButtonFlag)
{
turnOnLed(ledpattern[patternIndex]);
patternFlag = true;
cheatbutton = 0;
}
else if (buttonIsPressed(cheatbutton) && !patternFlag && !cheatButtonFlag)
{
turnOnLed(cheatbutton);
cheatButtonFlag = true;
}
else if (anybutton() && !patternFlag && !cheatButtonFlag )
{
cheatbutton = 0;
if (buttonIsPressed(ledpattern[patternIndex]))
{
//pattern button pressed, turn on LED, set flag
turnOnLed(ledpattern[patternIndex]);
patternFlag = true;
cheatbutton = 0;
}
else if (buttonIsPressed(cheatbutton))
{
// cheatbutton pressed, turn on cheat led, set flag
turnOnLed(cheatbutton);
cheatButtonFlag = true;
}
else if (anybutton())
{
// if any other button is pressed, clear cheat button
cheatbutton = 0;
}
}
if (!buttonIsPressed(ledpattern[patternIndex]))
{
// pattern switch is open, turn off pattern LED
turnOffLed(ledpattern[patternIndex]);
}
if (!buttonIsPressed(cheatbutton) && cheatButtonFlag)
{
// cheat switch is open, turn of cheat LED
turnOffLed(cheatbutton);
cheatButtonFlag = false;
cheatbutton = 0;
@@ -81,9 +80,11 @@ void HandleChainGame(void)
if (!anybutton())
{
//all switches are open, turn off all LEDs
turnOffAllLed();
if (patternFlag)
{
// pattern LED was triggerd, reset flag, move to next pattern
patternFlag = false;
nextPattern();
}
@@ -92,14 +93,20 @@ void HandleChainGame(void)
//check cheatbuttons
if (buttonIsPressed(4) && (cheatbutton == 0))
{
// cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1
cheatbutton = 1;
}
else if (buttonIsPressed(5) && (cheatbutton == 0))
{
// cheatbutton 5 (momentary 2) was closed, set cheatbutton to 2
cheatbutton = 2;
}
else if (buttonIsPressed(6) && (cheatbutton == 0))
{
// cheatbutton 5 (momentary 3) was closed, set cheatbutton to 3
cheatbutton = 3;
}
}
#endif
#endif

View File

@@ -1,6 +1,8 @@
#ifndef CHAINGAMEH
#define CHAINGAMEH
#include "Arduino.h"
#include "buttons.h"
void HandleChainGame( void );
void ResetChainGame(void);

View File

@@ -1,9 +1,5 @@
#include "Arduino.h"
#include "detectled.h"
#include "buttons.h"
#include "board.h"
#include "led.h"
#include "vector"
#define CHANNELS 3
#define SAMPLES 20
@@ -37,3 +33,4 @@ void initDetectLed(void)
analogReadResolution(10);
}

View File

@@ -1,6 +1,11 @@
#ifndef DETECTLEDH
#define DETECTLEDH
#include "buttons.h"
#include "board.h"
#include "led.h"
#include "vector"
void handleDetectLed( void );
void initDetectLed( void );

View File

@@ -1,5 +1,6 @@
#include "led.h"
#include "Arduino.h"
c_leds ledlist;
@@ -237,3 +238,4 @@ bool c_led::checkIndex(uint16_t index)
}
return false;
}

View File

@@ -1,8 +1,9 @@
#ifndef LEDH
#define LEDH
#include "arduino.h"
#include "Arduino.h"
#include "vector"
#include "board.h"
enum e_ledcolor
{
@@ -93,3 +94,4 @@ void turnOnLed(uint16_t index);
void turnOffAllLed();
#endif //LEDH

View File

@@ -1,7 +1,6 @@
#include "magicSwitchBoard.h"
#include "Arduino.h"
#include "buttons.h"
//#include "JC_Button.h"
#define CHANNELS 3
#define TIMEOUT 7000 //game timeout
@@ -171,3 +170,5 @@ void handleMagicSwitchBoard(void)
}

View File

@@ -1,6 +1,8 @@
#ifndef MAGICSWITCHBOARDH
#define MAGICSWITCHBOARDH
#include "buttons.h"
void handleMagicSwitchBoard( void );
#endif //MAGICSWITCHBOARDH

View File

@@ -1,4 +1,7 @@
#include <Arduino.h>
#include "Arduino.h"
#include "board.h"
#include "chainGame.h"
#include "detectled.h"
@@ -8,7 +11,7 @@
#include "led.h"
#define TIMEOUT 15000 // 15sec * 1000ms
#define GAMESELECTTIMEOUT 5000 // 7sec * 1000ms
#define GAMESELECTTIMEOUT 10000 // 7sec * 1000ms
typedef enum
{

View File

@@ -1,7 +1,5 @@
#include "simpleled.h"
#include "led.h"
#include "buttons.h"
#include "vector"
extern std::vector<c_button *> buttonlist;

View File

@@ -1,6 +1,10 @@
#ifndef SIMPLELEDH
#define SIMPLELEDH
#include "led.h"
#include "buttons.h"
#include "vector"
void initSimpleLed( void );
void handleSimpleLed( void );

View File

@@ -0,0 +1,12 @@
#include <unity.h>
#ifdef UNIT_TEST
int main( int argc, char **argv) {
UNITY_BEGIN();
UNITY_END();
}
#endif //UNIT_TEST