Buttons with function pointer when press or release

This commit is contained in:
Rémi Heredero 2023-06-13 09:09:40 +02:00
parent 3f1ce81ad5
commit 06ad7c948b
7 changed files with 205 additions and 19 deletions

179
02-UML/ST_BUTTON.uxf Normal file
View File

@ -0,0 +1,179 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<diagram program="umlet" version="15.1">
<zoom_level>14</zoom_level>
<element>
<id>UMLSpecialState</id>
<coordinates>
<x>546</x>
<y>140</y>
<w>28</w>
<h>28</h>
</coordinates>
<panel_attributes>type=initial</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>210</x>
<y>364</y>
<w>196</w>
<h>56</h>
</coordinates>
<panel_attributes>ST_PBPRESSED</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>714</x>
<y>364</y>
<w>196</w>
<h>56</h>
</coordinates>
<panel_attributes>ST_PBRELEASED</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>798</x>
<y>406</y>
<w>126</w>
<h>168</h>
</coordinates>
<panel_attributes>lt=-&gt;
evPBPoll</panel_attributes>
<additional_attributes>10.0;10.0;10.0;100.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>546</x>
<y>154</y>
<w>126</w>
<h>126</h>
</coordinates>
<panel_attributes>lt=-&gt;
evPBInit</panel_attributes>
<additional_attributes>10.0;10.0;10.0;70.0</additional_attributes>
</element>
<element>
<id>UMLSpecialState</id>
<coordinates>
<x>532</x>
<y>252</y>
<w>56</w>
<h>56</h>
</coordinates>
<panel_attributes>type=decision</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>294</x>
<y>266</y>
<w>266</w>
<h>126</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[pB==1]</panel_attributes>
<additional_attributes>170.0;10.0;10.0;10.0;10.0;70.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>294</x>
<y>406</y>
<w>126</w>
<h>126</h>
</coordinates>
<panel_attributes>lt=-&gt;
evPBPoll</panel_attributes>
<additional_attributes>10.0;10.0;10.0;70.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>574</x>
<y>266</y>
<w>266</w>
<h>126</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[pB==0]</panel_attributes>
<additional_attributes>10.0;10.0;170.0;10.0;170.0;70.0</additional_attributes>
</element>
<element>
<id>UMLSpecialState</id>
<coordinates>
<x>280</x>
<y>504</y>
<w>56</w>
<h>56</h>
</coordinates>
<panel_attributes>type=decision</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLSpecialState</id>
<coordinates>
<x>784</x>
<y>546</y>
<w>56</w>
<h>56</h>
</coordinates>
<panel_attributes>type=decision</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>350</x>
<y>308</y>
<w>462</w>
<h>308</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[pB==1] / pbPress()</panel_attributes>
<additional_attributes>310.0;190.0;260.0;190.0;40.0;10.0;10.0;10.0;10.0;40.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>322</x>
<y>308</y>
<w>462</w>
<h>266</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[pB==0] / pbRelease()</panel_attributes>
<additional_attributes>10.0;160.0;60.0;160.0;280.0;10.0;310.0;10.0;310.0;40.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>154</x>
<y>308</y>
<w>154</w>
<h>266</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[else]</panel_attributes>
<additional_attributes>90.0;160.0;10.0;160.0;10.0;10.0;70.0;10.0;70.0;40.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>826</x>
<y>308</y>
<w>154</w>
<h>308</h>
</coordinates>
<panel_attributes>lt=-&gt;
m1=[else]</panel_attributes>
<additional_attributes>10.0;190.0;90.0;190.0;90.0;10.0;30.0;10.0;30.0;40.0</additional_attributes>
</element>
</diagram>

View File

@ -86,9 +86,16 @@ void Factory_init() {
BUTTON_initHW(b3());
}
void lON(){
LED_on(l9());
}
void lOFF() {
LED_off(l9());
}
//connect objects if required
void Factory_build() {
BUTTON_setEventFunctions(b1(), &lON, &lOFF);
}
//start all state machines

View File

@ -1,8 +1,8 @@
/* this is the Factory class */
#ifndef FACTORY_ONCE
#define FACTORY_ONCE
#ifndef FACTORY_H
#define FACTORY_H
#include <stdint.h>
#include <stdbool.h>

View File

@ -1,9 +1,7 @@
#include "../mcc_generated_files/mcc.h"
#include "../board/led/led.h"
#include "../app/factory/factory.h"
#include "../xf/xf.h"
#include "../board/button/button.h"

View File

@ -8,7 +8,6 @@
#include "button.h"
#include "../../mcc_generated_files/pin_manager.h"
#include "../led/led.h"
#include "../../app/factory/factory.h"
/**
@ -82,31 +81,29 @@ bool BUTTON_processEvent(Event * ev) {
switch(me->state){
case ST_PBINIT:
if (evid == evPBInit) {
POST(me, &BUTTON_processEvent, evPBPoll, 0, 0);
if(BUTTON_isPressed(me)) {
me->state = ST_PBPRESSED;
} else {
me->state = ST_PBRELEASED;
}
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
}
break;
case ST_PBPOLL:
break;
case ST_PBRELEASED:
if(evid == evPBPoll) {
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
if(BUTTON_isPressed(me)) {
me->state = ST_PBPRESSED;
}
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
}
break;
case ST_PBPRESSED:
if(evid == evPBPoll) {
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
if(!BUTTON_isPressed(me)){
me->state = ST_PBRELEASED;
}
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
}
break;
@ -116,16 +113,19 @@ bool BUTTON_processEvent(Event * ev) {
switch(me->state){
case ST_PBINIT:
break;
case ST_PBPOLL:
break;
case ST_PBRELEASED:
LED_off(l10());
if(me->fButtonRelease != NULL) me->fButtonRelease();
break;
case ST_PBPRESSED:
LED_on(l10());
if(me->fButtonPress != NULL) me->fButtonPress();
break;
}
processed = true;
}
return processed;
}
void BUTTON_setEventFunctions(BUTTON* me, void (*fPress)(void), void (*fRelease)(void)) {
me->fButtonPress = fPress;
me->fButtonRelease = fRelease;
}

View File

@ -6,7 +6,7 @@
*
*/
#ifndef BUTTON_H
#define BUTTON_H
#define BUTTON_H
#include <stdint.h>
#include <stdbool.h>
@ -14,13 +14,14 @@
#define PB_POLL_TIME 20
typedef enum {ST_PBINIT, ST_PBWAIT, ST_PBPOLL, ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES;
typedef enum {ST_PBINIT, ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES;
typedef enum {evPBInit=100, evPBPoll} BUTTON_EVENTS;
typedef struct {
uint8_t id;
BUTTON_STATES state;
void (*fButtonPress)(void);
void (*fButtonRelease)(void);
} BUTTON;
void BUTTON_init(BUTTON* me, uint8_t id);
@ -28,5 +29,6 @@ void BUTTON_initHW(BUTTON* me);
bool BUTTON_isPressed(BUTTON* me);
void BUTTON_startBehaviour(BUTTON* me);
bool BUTTON_processEvent(Event* ev);
void BUTTON_setEventFunctions(BUTTON* me, void (*fPress)(void), void (*fRelease)(void));
#endif /* BUTTON_H */

View File

@ -6,7 +6,7 @@
*/
#ifndef LED_H
#define LED_H
#define LED_H
#include <stdint.h>