Open FFBoard
Open source force feedback firmware
LocalButtons.cpp
Go to the documentation of this file.
1/*
2 * LocalButtons.cpp
3 *
4 * Created on: 09.02.2020
5 * Author: Yannick
6 */
7
8#include <LocalButtons.h>
9
11 .name = "D-Pins" ,
12 .id=CLSID_BTN_LOCAL,
13};
14
15LocalButtons::LocalButtons() : CommandHandler("dpin",CLSID_BTN_LOCAL,0) {
16 setMask(mask); // Initialize button count
18
20 registerCommand("mask", LocalButtons_commands::mask, "Enabled pins",CMDFLAG_GET | CMDFLAG_SET);
21 registerCommand("polarity", LocalButtons_commands::polarity, "Pin polarity",CMDFLAG_GET | CMDFLAG_SET);
22 registerCommand("pins", LocalButtons_commands::pins, "Available pins",CMDFLAG_GET | CMDFLAG_SET);
23 registerCommand("values", LocalButtons_commands::values, "pin values",CMDFLAG_GET);
24 registerCommand("pulse", LocalButtons_commands::pulse, "Toggle to pulse mode mask",CMDFLAG_GET | CMDFLAG_SET);
25}
26
28
29}
30
32 return info;
33}
34
35void LocalButtons::setMask(uint32_t mask){
36 this->mask = mask;
37
38 this->btnnum = 0;
39 for(uint8_t i = 0;i<this->maxButtons;i++){
40 if(mask & (1 << i)){
41 this->btnnum++;
42 }
43 }
44}
45
46uint8_t LocalButtons::getButtonInputs(uint64_t* buf,bool pol){
47 uint8_t cur_btn = 0;
48 for(uint8_t i = 0;i<this->maxButtons;i++){
49 if(mask & (0x1 << i)){
50 bool b{readButton(i)};
51 if(pol){ // positive polarity
52 *buf |= b << cur_btn++;
53 }else{ // Negative polarity (normal)
54 *buf |= !b << cur_btn++;
55 }
56 }
57 }
58 return this->btnnum;
59}
60
61uint8_t LocalButtons::readButtons(uint64_t* buf){
62
63 uint64_t tBuf = 0;
64 getButtonInputs(&tBuf,this->polarity);
65
66 // TODO processing should really be done in a general button handler class to be shared
67 if(pulsemask){
68 uint64_t pulsebtns = (lastButtons ^ tBuf) & pulsemask; // Momentary mode
69 if(HAL_GetTick() - lastPulseTime > pulseTimeout || pulsebtns){ // If timeout or something is high
70 lastPulseTime = HAL_GetTick();
71 // Update pulses
72 *buf |= pulsebtns;
73 }else{
74 *buf |= (lastOutputs & pulsemask); // Normal buttons immediate. Pulsed stored
75 }
76 *buf |= (tBuf & ~pulsemask);
77 lastOutputs = *buf;
78 }else{
79 *buf |= tBuf;
80 }
81 lastButtons = tBuf;
82
83 return this->btnnum;
84}
85
87 uint16_t dat = this->mask & 0xffff;
88 Flash_Write(ADR_LOCAL_BTN_CONF, dat);
89
90 uint16_t dat2 = this->polarity & 0x01;
91 Flash_Write(ADR_LOCAL_BTN_CONF_2, dat2);
92
93 uint16_t dat3 = this->pulsemask & 0xffff;
94 Flash_Write(ADR_LOCAL_BTN_CONF_3, dat3);
95}
96
98 uint16_t dat = 0;
99 if(Flash_Read(ADR_LOCAL_BTN_CONF,&dat)){
100 this->setMask(dat & 0xffff);
101 }
102
103 if(Flash_Read(ADR_LOCAL_BTN_CONF_2,&dat)){
104 this->polarity = dat & 0x01;
105 }
106
107 if(Flash_Read(ADR_LOCAL_BTN_CONF_3,&dat)){
108 this->pulsemask = (dat & 0xffff);
109 }
110}
111
112CommandStatus LocalButtons::command(const ParsedCommand& cmd,std::vector<CommandReply>& replies){
113
114 switch(static_cast<LocalButtons_commands>(cmd.cmdId)){
116 if(cmd.type == CMDtype::set){
117 this->setMask(cmd.val);
118 }else if(cmd.type == CMDtype::get){
119 replies.emplace_back(this->mask);
120 }else{
121 return CommandStatus::ERR;
122 }
123 break;
124
126 if(cmd.type == CMDtype::set){
127 this->polarity = cmd.val != 0;
128 }else if(cmd.type == CMDtype::get){
129 replies.emplace_back(this->polarity ? 1 : 0);
130 }else{
131 return CommandStatus::ERR;
132 }
133 break;
134
136 if(cmd.type == CMDtype::get){
137 replies.emplace_back(maxButtons);
138 }else{
139 return CommandStatus::ERR;
140 }
141 break;
142
144 if(cmd.type == CMDtype::get){
145 uint64_t buf = 0;
146 getButtonInputs(&buf,this->polarity);
147 replies.emplace_back(buf);
148 }else{
149 return CommandStatus::ERR;
150 }
151 break;
152
154 if(cmd.type == CMDtype::set){
155 this->pulsemask = cmd.val;
156 }else if(cmd.type == CMDtype::get){
157 replies.emplace_back(this->pulsemask);
158 }else{
159 return CommandStatus::ERR;
160 }
161 break;
162
163 default:
165 }
166
167 return CommandStatus::OK;
168
169}
170
175const std::array<InputPin,BUTTON_PINS> LocalButtons::button_pins {
176#if BUTTON_PINS > 0
177 InputPin(DIN0_GPIO_Port, DIN0_Pin),
178#endif
179#if BUTTON_PINS > 1
180 InputPin(DIN1_GPIO_Port, DIN1_Pin),
181#endif
182#if BUTTON_PINS > 2
183 InputPin(DIN2_GPIO_Port, DIN2_Pin),
184#endif
185#if BUTTON_PINS > 3
186 InputPin(DIN3_GPIO_Port, DIN3_Pin),
187#endif
188#if BUTTON_PINS > 4
189 InputPin(DIN4_GPIO_Port, DIN4_Pin),
190#endif
191#if BUTTON_PINS > 5
192 InputPin(DIN5_GPIO_Port, DIN5_Pin),
193#endif
194#if BUTTON_PINS > 6
195 InputPin(DIN6_GPIO_Port, DIN6_Pin),
196#endif
197#if BUTTON_PINS > 7
198 InputPin(DIN7_GPIO_Port, DIN7_Pin),
199#endif
200#if BUTTON_PINS > 8
201 InputPin(DIN8_GPIO_Port, DIN8_Pin),
202#endif
203#if BUTTON_PINS > 9
204 InputPin(DIN9_GPIO_Port, DIN9_Pin),
205#endif
206#if BUTTON_PINS > 10
207 InputPin(DIN10_GPIO_Port, DIN10_Pin),
208#endif
209#if BUTTON_PINS > 11
210 InputPin(DIN11_GPIO_Port, DIN11_Pin),
211#endif
212#if BUTTON_PINS > 12
213 InputPin(DIN12_GPIO_Port, DIN12_Pin),
214#endif
215#if BUTTON_PINS > 13
216 InputPin(DIN13_GPIO_Port, DIN13_Pin),
217#endif
218#if BUTTON_PINS > 14
219 InputPin(DIN14_GPIO_Port, DIN14_Pin),
220#endif
221#if BUTTON_PINS > 15
222 InputPin(DIN15_GPIO_Port, DIN15_Pin),
223#endif
224
225};
CommandStatus
const std::array< InputPin, BUTTON_PINS > InputPin(DIN1_GPIO_Port, DIN1_Pin)
uint16_t btnnum
Definition: ButtonSource.h:38
void registerCommand(const char *cmd, const ID cmdid, const char *help=nullptr, uint32_t flags=0)
uint32_t lastPulseTime
Definition: LocalButtons.h:26
uint32_t pulsemask
Definition: LocalButtons.h:24
uint8_t getButtonInputs(uint64_t *buf, bool pol)
static constexpr uint16_t maxButtons
Definition: LocalButtons.h:42
static const std::array< InputPin, BUTTON_PINS > button_pins
Definition: LocalButtons.h:29
void saveFlash()
void setMask(uint32_t mask)
static bool readButton(int button_num)
Definition: LocalButtons.h:51
virtual ~LocalButtons()
static ClassIdentifier info
Definition: LocalButtons.h:39
uint8_t readButtons(uint64_t *buf)
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
void restoreFlash()
uint64_t lastButtons
Definition: LocalButtons.h:31
uint64_t lastOutputs
Definition: LocalButtons.h:30
const ClassIdentifier getInfo()
uint32_t mask
Definition: LocalButtons.h:23
static constexpr uint32_t pulseTimeout
Definition: LocalButtons.h:25
bool Flash_Write(uint16_t adr, uint16_t dat)
bool Flash_Read(uint16_t adr, uint16_t *buf, bool checkempty=true)
const char * name
uint32_t cmdId