Changed to JC_button lib

This commit is contained in:
willem oldemans
2020-11-19 08:27:52 +01:00
parent 8c9176ccbe
commit d184f1785c
8 changed files with 135 additions and 167 deletions

View File

@@ -14,3 +14,5 @@ board = nucleo_l031K6
framework = arduino
upload_port = stlink
debug_tool = stlink
lib_deps = http://192.168.2.3/Bonobo.Git.Server/JCButton.git

View File

@@ -14,7 +14,7 @@
#define SWITCH2 PA1 //A1
#define SWITCH22 PA3 //A2
#define SWITCH3 PB5 //D11
#define SWITCH32 PA0 //PB4 //D12
#define SWITCH32 PB4 //D12
#define LD3LED PB3

View File

@@ -1,90 +1,42 @@
#include "buttons.h"
#include "board.h"
#include <vector>
#include "Arduino.h"
#include "JC_Button.h"
std::vector<buttons *> buttonlist;
buttons::buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index):
_buttonIndex(index), _buttonPin(pin)
std::vector<ToggleButton *> buttonlist;
ToggleButton button1(SWITCH12);
ToggleButton button2(SWITCH22);
ToggleButton button3(SWITCH32);
ToggleButton button4(SWITCH1);
ToggleButton button5(SWITCH2);
ToggleButton button6(SWITCH3);
void buttonbegin( ToggleButton *thisbutton )
{
_buttonDelayShort = shortpress;
_buttonDelayLong = longpress;
_buttonState = INVALID;
_lastState = INVALID;
buttonlist.push_back(this);
thisbutton->begin();
buttonlist.push_back(thisbutton);
}
void buttons::begin()
void initButtons( void )
{
pinMode(_buttonPin, INPUT_PULLUP);
buttonbegin(&button1);
buttonbegin(&button2);
buttonbegin(&button3);
buttonbegin(&button4);
buttonbegin(&button5);
buttonbegin(&button6);
}
buttonState_t buttons::state()
{
return _buttonState;
}
buttonState_t buttons::lastState(void)
{
return _lastState;
}
bool buttons::raw(void)
{
return _buttonFlag;
}
void buttons::update(void)
{
unsigned long currentMillis = millis();
_buttonFlag = !digitalRead(_buttonPin);
if (_buttonFlag)
{
if (_buttonState == RELEASED)
{
//button not detected yet, check timer
if ((currentMillis - _buttonTimer) >= _buttonDelayShort)
{
_buttonState = SHORT;
_lastState = SHORT;
}
}
else if (_buttonState == SHORT)
{
if ((currentMillis - _buttonTimer) >= _buttonDelayLong)
{
_buttonState = LONG;
_lastState = LONG;
}
}
}
else
{
//button is not pressed, keep updating the timer
_buttonState = RELEASED;
_buttonTimer = millis();
}
}
unsigned int buttons::index( void )
{
return _buttonIndex;
}
void initbuttons(void)
{
for (auto &&i : buttonlist)
{
i->begin();
}
}
void handleButtons(void)
{
for (auto &&i : buttonlist)
{
i->update();
i->read();
}
}
@@ -93,7 +45,7 @@ bool anybutton(void)
handleButtons();
for (auto &&i : buttonlist)
{
if (i->raw())
if (i->isPressed())
{
return true;
}
@@ -101,12 +53,11 @@ bool anybutton(void)
return false;
}
buttons* getButton(unsigned int index)
ToggleButton* getButton(unsigned int index)
{
for (auto &&i : buttonlist)
if(index > buttonlist.size())
{
if( i->index() == index)
return i;
return NULL;
}
return NULL;
return buttonlist[index-1];
}

View File

@@ -3,45 +3,11 @@
#include <Arduino.h>
#include <vector>
typedef enum buttonState_e
{
RELEASED,
SHORT,
LONG,
INVALID
}buttonState_t;
class buttons
{
const unsigned int _buttonIndex;
const uint32_t _buttonPin;
unsigned long _buttonTimer;
buttonState_t _buttonState;
buttonState_t _lastState;
bool _buttonFlag;
unsigned long _buttonDelayShort;
unsigned long _buttonDelayLong;
public:
buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index);
void begin();
void update( void );
bool raw( void );
buttonState_t state();
buttonState_t lastState();
unsigned int index( void );
};
#include "JC_Button.h"
bool anybutton( void );
void initbuttons( void );
void initButtons( void );
void handleButtons( void );
buttons* getButton(unsigned int index);
ToggleButton* getButton(unsigned int index);
#endif //BUTTONSH

View File

@@ -1,10 +1,10 @@
#include "chainGame.h"
#include "Arduino.h"
#include "buttons.h"
#include "JC_Button.h"
extern buttons button1;
extern buttons button2;
extern buttons button3;
extern ToggleButton button4;
extern ToggleButton button5;
extern ToggleButton button6;
uint8_t patternIndex = 0;
@@ -31,9 +31,9 @@ void nextPattern(void)
}
}
void HandleChainGame(void)
void HandleChainGame( void )
{
if (button1.state())
if ((button4.isPressed()) | (button6.isPressed()))
{
if (!patternFlag)
{
@@ -51,7 +51,7 @@ void HandleChainGame(void)
}
}
if (button2.state())
if (button5.isPressed())
{
if (!patternFlag)
{
@@ -69,7 +69,7 @@ void HandleChainGame(void)
}
}
if (button1.state() | button2.state())
if ((button4.isPressed()) | (button5.isPressed()) | (button6.isPressed()))
{
//write pattern to the LEDs
digitalWrite(LED1, ledpattern[patternIndex][0]);

View File

@@ -3,25 +3,55 @@
#include "buttons.h"
#include "board.h"
extern buttons button1;
extern buttons button2;
extern buttons button3;
#define CHANNELS 3
#define SAMPLES 20
unsigned int detectled[CHANNELS] = {0,0,0};
uint32_t detectledRaw[CHANNELS][SAMPLES];
const uint32_t inputs[CHANNELS] = {DETECT1, DETECT2, DETECT3};
const uint32_t leds[CHANNELS] = {LED1, LED2, LED3};
uint32_t sampleIndex = 0;
uint32_t detectled[3] = {0,0,0};
void handleDetectLed( void )
{
detectled[0] = analogRead(DETECT1);
detectled[1] = analogRead(DETECT2);
detectled[2] = analogRead(DETECT3);
for(int i = 0;i < CHANNELS;i++)
{
detectledRaw[i][sampleIndex] = analogRead(inputs[i]);
}
sampleIndex++;
if(sampleIndex == SAMPLES)
{
for(int i = 0;i<CHANNELS;i++)
{
uint64_t sum = 0;
for(int n = 0;n < SAMPLES;n++)
{
sum += detectledRaw[i][n];
}
detectled[i] = sum / SAMPLES;
}
printf("L1 = %u, L2 = %u, L3 = %u",detectled[0],detectled[1],detectled[2]);
sampleIndex = 0;
}
for( auto &&o : leds)
{
digitalWrite(o, 0);
}
}
void initDetectLed( void )
{
pinMode(DETECT1, INPUT_ANALOG);
pinMode(DETECT2, INPUT_ANALOG);
pinMode(DETECT3, INPUT_ANALOG);
analogReadResolution(10);
for (auto &&i : inputs)
{
pinMode(i, INPUT_ANALOG);
}
analogReadResolution(10);
}

View File

@@ -1,6 +1,7 @@
#include "magicSwitchBoard.h"
#include "Arduino.h"
#include "buttons.h"
#include "JC_Button.h"
#define CHANNELS 3
#define TIMEOUT 7000 //game timeout
@@ -28,7 +29,7 @@ void showLeds(void)
for (int i = 0; i < CHANNELS; i++)
{
//get the button pointer
buttons *currentbutton = getButton(buttonIndex[sequence[i]]);
ToggleButton *currentbutton = getButton(buttonIndex[sequence[i]]);
//verify that the button pointer is not NULL
if (currentbutton == NULL)
@@ -37,7 +38,7 @@ void showLeds(void)
}
//if the button is pressed, show LED or not
if (currentbutton->raw() == true)
if (currentbutton->isPressed() == true)
{
//check if the position is already programmed
//write sequence led on
@@ -93,18 +94,18 @@ void handleLearn(void)
{
for (int i = 0; i < CHANNELS; i++)
{
buttons *currentbutton = getButton(buttonIndex[i]);
ToggleButton *currentbutton = getButton(buttonIndex[i]);
if (currentbutton == NULL)
{
return;
}
if (currentbutton->state() == !RELEASED)
if (currentbutton->isPressed())
{
bool duplicate = false;
for (int n = 0; n < CHANNELS; n++)
{
if (currentbutton->index() == buttonIndex[sequence[n]])
if (i == sequence[n])
{
duplicate = true;
}

View File

@@ -1,20 +1,24 @@
#include <Arduino.h>
#include "buttons.h"
//#include "buttons.h"
#include "board.h"
#include "chainGame.h"
#include "detectled.h"
#include "magicSwitchBoard.h"
#include "buttons.h"
#include "JC_Button.h"
#include "vector"
buttons button1(SWITCH12, 100, 1000, 1);
buttons button2(SWITCH22, 100, 1000, 2);
buttons button3(SWITCH32, 100, 1000, 3);
buttons button4(SWITCH1, 100, 1000, 4);
buttons button5(SWITCH2, 100, 1000, 5);
buttons button6(SWITCH3, 100, 1000, 6);
// extern ToggleButton button1;
// extern ToggleButton button2;
// extern ToggleButton button3;
extern ToggleButton button4;
extern ToggleButton button5;
extern ToggleButton button6;
typedef enum
{
none,
powerOff,
ChainGame,
magicSwitchBoard,
detectLED,
@@ -31,28 +35,8 @@ void setup()
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
initbuttons();
initButtons();
initDetectLed();
while (anybutton())
{
digitalWrite(LED1, 1);
digitalWrite(LED2, 1);
digitalWrite(LED3, 1);
if (button4.raw())
{
currentGame = ChainGame;
}
if (button5.raw())
{
currentGame = magicSwitchBoard;
}
if (button6.raw())
{
currentGame = detectLED;
}
}
}
void loop()
@@ -78,5 +62,39 @@ void loop()
{
HandleChainGame();
}
break;
case none:
{
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
if (button4.isPressed())
{
digitalWrite(LED1, 1);
}
if (button5.isPressed())
{
currentGame = magicSwitchBoard;
digitalWrite(LED2, 1);
}
else
{
currentGame = ChainGame;
}
if (button6.isPressed())
{
digitalWrite(LED3, 1);
}
while(anybutton());
// if (button6.isPressed())
// {
// currentGame = detectLED;
// }
}
break;
}
}