Skip to content

Commit c78e73c

Browse files
committed
Added pulse mode for local buttons
1 parent fe26eca commit c78e73c

File tree

9 files changed

+98
-41
lines changed

9 files changed

+98
-41
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
### Changes this version:
2+
- Added local button pulse mode
3+
4+
### Changes since v1.14.0
25
- Save TMC space vector PWM mode in flash. Should be usually on for BLDC motors if the star point is isolated.
36
- Allow using the motors flux component to dissipate energy with the TMC4671 instead of the brake resistor. May cause noticable braking in the motor but takes stress off the resistor.
47
- Axis speed limiter usable and saved in flash.

Firmware/FFBoard/Inc/constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* For more settings see target_constants.h in a target specific folder
99
*/
1010

11-
static const uint8_t SW_VERSION_INT[3] = {1,14,1}; // Version as array. 8 bit each!
11+
static const uint8_t SW_VERSION_INT[3] = {1,14,2}; // Version as array. 8 bit each!
1212
#define MAX_AXIS 2 // ONLY USE 2 for now else screws HID Reports
1313
#define FLASH_VERSION 0 // Counter to increase whenever a full flash erase is required.
1414

Firmware/FFBoard/UserExtensions/Inc/LocalButtons.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717

1818
class LocalButtons: public ButtonSource,CommandHandler{
1919
enum class LocalButtons_commands : uint32_t{
20-
mask,polarity,pins,values,
20+
mask,polarity,pins,values,pulse
2121
};
2222
private:
23-
uint32_t mask = 0xff;
23+
uint32_t mask = 0xff; // Can have 16 bits stored
24+
uint32_t pulsemask = 0;
25+
static constexpr uint32_t pulseTimeout = 50; // Update only every 50 ms
26+
uint32_t lastPulseTime = 0;
2427
void setMask(uint32_t mask);
2528
bool polarity = false;
2629
static const std::array<InputPin,BUTTON_PINS> button_pins;
27-
30+
uint64_t lastOutputs = 0;
31+
uint64_t lastButtons = 0;
2832
public:
2933
LocalButtons();
3034
virtual ~LocalButtons();
3135
uint8_t readButtons(uint64_t* buf);
36+
uint8_t getButtonInputs(uint64_t* buf,bool pol);
3237

3338
const ClassIdentifier getInfo();
3439
static ClassIdentifier info;

Firmware/FFBoard/UserExtensions/Inc/eeprom_addresses.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*
2-
* eeprom_addresses.h
3-
*
4-
* Created on: 24.01.2020
5-
* Author: Yannick
6-
*
7-
* /!\ Generated from the file memory_map.csv
8-
/ ! \ DO NOT EDIT THIS DIRECTLY !!!
9-
*/
2+
* eeprom_addresses.h
3+
*
4+
* Created on: 24.01.2020
5+
* Author: Yannick
6+
*
7+
* /!\ Generated from the file memory_map.csv
8+
/ ! \ DO NOT EDIT THIS DIRECTLY !!!
9+
*/
1010

1111
#ifndef EEPROM_ADDRESSES_H_
1212
#define EEPROM_ADDRESSES_H_
1313

1414
#include "main.h"
1515
// Change this to the amount of currently registered variables
16-
#define NB_OF_VAR 147
16+
#define NB_OF_VAR 148
1717
extern const uint16_t VirtAddVarTab[NB_OF_VAR];
1818

1919
// Amount of variables in exportable list
20-
#define NB_EXPORTABLE_ADR 132
20+
#define NB_EXPORTABLE_ADR 133
2121
extern const uint16_t exportableFlashAddresses[NB_EXPORTABLE_ADR];
2222

2323

@@ -50,11 +50,12 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) will return 1 if
5050
// Button Sources:
5151
#define ADR_SPI_BTN_1_CONF 0x201
5252
#define ADR_SHIFTERANALOG_CONF 0x202
53-
#define ADR_LOCAL_BTN_CONF 0x203
54-
#define ADR_LOCAL_BTN_CONF_2 0x204
53+
#define ADR_LOCAL_BTN_CONF 0x203 // Pin mask
54+
#define ADR_LOCAL_BTN_CONF_2 0x204 // Misc settings
5555
#define ADR_SPI_BTN_2_CONF 0x205
5656
#define ADR_SPI_BTN_1_CONF_2 0x206
5757
#define ADR_SPI_BTN_2_CONF_2 0x207
58+
#define ADR_LOCAL_BTN_CONF_3 0x208 // Pulse mask
5859
// Local encoder
5960
#define ADR_ENCLOCAL_CPR 0x210
6061
#define ADR_ENCLOCAL_OFS 0x211

Firmware/FFBoard/UserExtensions/Src/FFBHIDMain.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ bool FFBHIDMain::getFfbActive(){
258258
* Sends periodic gamepad reports of buttons and analog axes
259259
*/
260260
void FFBHIDMain::send_report(){
261+
// Check if HID command interface wants to send something and allow that if we did not skip too many reports
262+
if(!tud_hid_n_ready(0) || (reportSendCounter++ < usb_report_rate*2 && this->hidCommands->waitingToSend())){
263+
return;
264+
}
265+
//Try semaphore
261266
if(!sourcesSem.Take(10)){
262267
return;
263268
}
@@ -302,10 +307,7 @@ void FFBHIDMain::send_report(){
302307
/*
303308
* Only send a new report if actually changed since last time or timeout and hid is ready
304309
*/
305-
if( (reportSendCounter++ > 100/usb_report_rate || (memcmp(&lastReportHID,&reportHID,sizeof(reportHID_t)) != 0) )
306-
&& tud_hid_n_ready(0)
307-
&& !(reportSendCounter < usb_report_rate*2 && this->hidCommands->waitingToSend())
308-
) // Check if HID command interface wants to send something and allow that if we did not skip too many reports
310+
if( (reportSendCounter > 100/usb_report_rate || (memcmp(&lastReportHID,&reportHID,sizeof(reportHID_t)) != 0) ))
309311
{
310312

311313

Firmware/FFBoard/UserExtensions/Src/LocalButtons.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LocalButtons::LocalButtons() : CommandHandler("dpin",CLSID_BTN_LOCAL,0) {
2121
registerCommand("polarity", LocalButtons_commands::polarity, "Pin polarity",CMDFLAG_GET | CMDFLAG_SET);
2222
registerCommand("pins", LocalButtons_commands::pins, "Available pins",CMDFLAG_GET | CMDFLAG_SET);
2323
registerCommand("values", LocalButtons_commands::values, "pin values",CMDFLAG_GET);
24+
registerCommand("pulse", LocalButtons_commands::pulse, "Toggle to pulse mode mask",CMDFLAG_GET | CMDFLAG_SET);
2425
}
2526

2627
LocalButtons::~LocalButtons() {
@@ -42,12 +43,12 @@ void LocalButtons::setMask(uint32_t mask){
4243
}
4344
}
4445

45-
uint8_t LocalButtons::readButtons(uint64_t* buf){
46+
uint8_t LocalButtons::getButtonInputs(uint64_t* buf,bool pol){
4647
uint8_t cur_btn = 0;
4748
for(uint8_t i = 0;i<this->maxButtons;i++){
4849
if(mask & (0x1 << i)){
4950
bool b{readButton(i)};
50-
if(this->polarity){ // positive polarity
51+
if(pol){ // positive polarity
5152
*buf |= b << cur_btn++;
5253
}else{ // Negative polarity (normal)
5354
*buf |= !b << cur_btn++;
@@ -57,12 +58,40 @@ uint8_t LocalButtons::readButtons(uint64_t* buf){
5758
return this->btnnum;
5859
}
5960

61+
uint8_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+
6086
void LocalButtons::saveFlash(){
6187
uint16_t dat = this->mask & 0xffff;
6288
Flash_Write(ADR_LOCAL_BTN_CONF, dat);
6389

6490
uint16_t dat2 = this->polarity & 0x01;
6591
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);
6695
}
6796

6897
void LocalButtons::restoreFlash(){
@@ -74,6 +103,10 @@ void LocalButtons::restoreFlash(){
74103
if(Flash_Read(ADR_LOCAL_BTN_CONF_2,&dat)){
75104
this->polarity = dat & 0x01;
76105
}
106+
107+
if(Flash_Read(ADR_LOCAL_BTN_CONF_3,&dat)){
108+
this->pulsemask = (dat & 0xffff);
109+
}
77110
}
78111

79112
CommandStatus LocalButtons::command(const ParsedCommand& cmd,std::vector<CommandReply>& replies){
@@ -110,13 +143,23 @@ CommandStatus LocalButtons::command(const ParsedCommand& cmd,std::vector<Command
110143
case LocalButtons_commands::values:
111144
if(cmd.type == CMDtype::get){
112145
uint64_t buf = 0;
113-
readButtons(&buf);
146+
getButtonInputs(&buf,this->polarity);
114147
replies.emplace_back(buf);
115148
}else{
116149
return CommandStatus::ERR;
117150
}
118151
break;
119152

153+
case LocalButtons_commands::pulse:
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+
120163
default:
121164
return CommandStatus::NOT_FOUND;
122165
}

Firmware/FFBoard/UserExtensions/Src/eeprom_addresses.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* eeprom_addresses.c
3-
*
4-
* Created on: 24.01.2020
5-
* Author: Yannick
6-
*
7-
* /!\ Generated from the file memory_map.csv
8-
/ ! \ DO NOT EDIT THIS DIRECTLY !!!
9-
*/
2+
* eeprom_addresses.c
3+
*
4+
* Created on: 24.01.2020
5+
* Author: Yannick
6+
*
7+
* /!\ Generated from the file memory_map.csv
8+
/ ! \ DO NOT EDIT THIS DIRECTLY !!!
9+
*/
1010

1111
#include "eeprom_addresses.h"
1212

@@ -32,11 +32,12 @@ const uint16_t VirtAddVarTab[NB_OF_VAR] =
3232
// Button Sources:
3333
ADR_SPI_BTN_1_CONF,
3434
ADR_SHIFTERANALOG_CONF,
35-
ADR_LOCAL_BTN_CONF,
36-
ADR_LOCAL_BTN_CONF_2,
35+
ADR_LOCAL_BTN_CONF, // Pin mask
36+
ADR_LOCAL_BTN_CONF_2, // Misc settings
3737
ADR_SPI_BTN_2_CONF,
3838
ADR_SPI_BTN_1_CONF_2,
3939
ADR_SPI_BTN_2_CONF_2,
40+
ADR_LOCAL_BTN_CONF_3, // Pulse mask
4041
// Local encoder
4142
ADR_ENCLOCAL_CPR,
4243
ADR_ENCLOCAL_OFS,
@@ -193,8 +194,8 @@ const uint16_t VirtAddVarTab[NB_OF_VAR] =
193194
};
194195

195196
/**
196-
* Variables to be included in a flash dump
197-
*/
197+
* Variables to be included in a flash dump
198+
*/
198199
const uint16_t exportableFlashAddresses[NB_EXPORTABLE_ADR] =
199200
{
200201
// System variables
@@ -212,11 +213,12 @@ const uint16_t exportableFlashAddresses[NB_EXPORTABLE_ADR] =
212213
// Button Sources:
213214
ADR_SPI_BTN_1_CONF,
214215
ADR_SHIFTERANALOG_CONF,
215-
ADR_LOCAL_BTN_CONF,
216-
ADR_LOCAL_BTN_CONF_2,
216+
ADR_LOCAL_BTN_CONF, // Pin mask
217+
ADR_LOCAL_BTN_CONF_2, // Misc settings
217218
ADR_SPI_BTN_2_CONF,
218219
ADR_SPI_BTN_1_CONF_2,
219220
ADR_SPI_BTN_2_CONF_2,
221+
ADR_LOCAL_BTN_CONF_3, // Pulse mask
220222
// Local encoder
221223
ADR_ENCLOCAL_CPR,
222224
ADR_ENCLOCAL_OFS,
@@ -370,4 +372,4 @@ const uint16_t exportableFlashAddresses[NB_EXPORTABLE_ADR] =
370372
ADR_ADS111X_MAX_2,
371373
ADR_ADS111X_MIN_3,
372374
ADR_ADS111X_MAX_3,
373-
};
375+
};

Firmware/scripts/memory_map.csv

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ Section comment,Name,Value,Comment on key,VirtAddVarTab,exportableFlashAddresses
1414
// Button Sources:,,,,,
1515
,ADR_SPI_BTN_1_CONF,0x201,,1,1
1616
,ADR_SHIFTERANALOG_CONF,0x202,,1,1
17-
,ADR_LOCAL_BTN_CONF,0x203,,1,1
18-
,ADR_LOCAL_BTN_CONF_2,0x204,,1,1
17+
,ADR_LOCAL_BTN_CONF,0x203,// Pin mask,1,1
18+
,ADR_LOCAL_BTN_CONF_2,0x204,// Misc settings,1,1
1919
,ADR_SPI_BTN_2_CONF,0x205,,1,1
2020
,ADR_SPI_BTN_1_CONF_2,0x206,,1,1
2121
,ADR_SPI_BTN_2_CONF_2,0x207,,1,1
22+
,ADR_LOCAL_BTN_CONF_3,0x208,// Pulse mask,1,1
2223
// Local encoder,,,,,
2324
,ADR_ENCLOCAL_CPR,0x210,,1,1
2425
,ADR_ENCLOCAL_OFS,0x211,,1,1

0 commit comments

Comments
 (0)