Skip to content

Commit e08f23c

Browse files
committed
Moved effects array into effectscalculator
1 parent 630ad2e commit e08f23c

6 files changed

Lines changed: 24 additions & 37 deletions

File tree

Firmware/FFBoard/Inc/EffectsCalculator.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class EffectsCalculator: public PersistentStorage,
7676
void saveFlash();
7777
void restoreFlash();
7878

79-
// virtual bool processHidCommand(HID_Custom_Data_t* data);
8079
bool isActive();
8180
void setActive(bool active);
8281
void calculateEffects(std::vector<std::unique_ptr<Axis>> &axes);
@@ -89,12 +88,11 @@ class EffectsCalculator: public PersistentStorage,
8988
void logEffectState(uint8_t type,uint8_t state);
9089
void resetLoggedActiveEffects(bool reinit);
9190

92-
//virtual ParseStatus command(ParsedCommand_old *cmd, std::string *reply);
9391
CommandStatus command(const ParsedCommand& cmd,std::vector<CommandReply>& replies);
9492
virtual std::string getHelpstring() { return "Controls internal FFB effects"; }
9593

96-
void setEffectsArray(FFB_Effect* pEffects);
97-
FFB_Effect* effects = nullptr; // ptr to effects array in HidFFB
94+
static const uint32_t max_effects = MAX_EFFECTS;
95+
std::array<FFB_Effect,max_effects> effects; // Main effects storage
9896

9997
// Thread impl
10098
void Run();

Firmware/FFBoard/Inc/HidFFB.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class HidFFB: public UsbHidHandler {
2020
public:
21-
HidFFB();
21+
HidFFB(EffectsCalculator &ec);
2222
virtual ~HidFFB();
2323

2424
void hidOut(uint8_t report_id, hid_report_type_t report_type,const uint8_t* buffer, uint16_t bufsize) override;
@@ -36,8 +36,8 @@ class HidFFB: public UsbHidHandler {
3636
void set_gain(uint8_t gain);
3737

3838
void sendStatusReport(uint8_t effect);
39-
void setEffectsCalculator(EffectsCalculator* ec);
40-
FFB_Effect effects[MAX_EFFECTS];
39+
40+
std::array<FFB_Effect,EffectsCalculator::max_effects>& effects; // Must be passed in constructor
4141
private:
4242
// HID
4343
EffectsCalculator* effects_calc = nullptr;
@@ -57,7 +57,6 @@ class HidFFB: public UsbHidHandler {
5757
void set_filters(FFB_Effect* effect);
5858

5959

60-
//uint8_t last_effect_id = 0;
6160
uint16_t used_effects = 0;
6261
bool ffb_active = false;
6362
FFB_BlockLoad_Feature_Data_t blockLoad_report;

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,10,0}; // Version as array. 8 bit each!
11+
static const uint8_t SW_VERSION_INT[3] = {1,10,1}; // 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/Src/EffectsCalculator.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ int32_t EffectsCalculator::calcComponentForce(FFB_Effect *effect, int32_t forceV
359359
}
360360
}
361361
}
362+
// No break required here. The filter is a special preprocessing case for the constant force effect.
362363
case FFB_EFFECT_RAMP:
363364
case FFB_EFFECT_SQUARE:
364365
case FFB_EFFECT_TRIANGLE:
@@ -574,11 +575,6 @@ void EffectsCalculator::setGain(uint8_t gain)
574575

575576
uint8_t EffectsCalculator::getGain() { return global_gain; }
576577

577-
void EffectsCalculator::setEffectsArray(FFB_Effect *pEffects)
578-
{
579-
effects = pEffects;
580-
}
581-
582578

583579

584580
/*

Firmware/FFBoard/Src/HidFFB.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
#include "cppmain.h"
1313

1414

15-
HidFFB::HidFFB() {
15+
HidFFB::HidFFB(EffectsCalculator &ec) : effects_calc(&ec), effects(ec.effects)
16+
{
1617

1718
// Initialize reports
1819
blockLoad_report.effectBlockIndex = 1;
19-
blockLoad_report.ramPoolAvailable = (MAX_EFFECTS-used_effects)*sizeof(FFB_Effect);
20+
blockLoad_report.ramPoolAvailable = (effects.size()-used_effects)*sizeof(FFB_Effect);
2021
blockLoad_report.loadStatus = 1;
2122

22-
pool_report.ramPoolSize = MAX_EFFECTS*sizeof(FFB_Effect);
23-
pool_report.maxSimultaneousEffects = MAX_EFFECTS;
23+
pool_report.ramPoolSize = effects.size()*sizeof(FFB_Effect);
24+
pool_report.maxSimultaneousEffects = effects.size();
2425
pool_report.memoryManagement = 1;
2526

2627

@@ -30,12 +31,6 @@ HidFFB::HidFFB() {
3031
HidFFB::~HidFFB() {
3132
}
3233

33-
void HidFFB::setEffectsCalculator(EffectsCalculator *ec) {
34-
this->effects_calc = ec;
35-
assert(effects_calc != nullptr);
36-
this->effects_calc->setEffectsArray(this->effects);
37-
this->effects_calc->setActive(this->ffb_active);
38-
}
3934

4035

4136
bool HidFFB::getFfbActive(){
@@ -169,7 +164,7 @@ void HidFFB::hidOut(uint8_t report_id, hid_report_type_t report_type, uint8_t co
169164

170165

171166
void HidFFB::free_effect(uint16_t idx){
172-
if(idx < MAX_EFFECTS){
167+
if(idx < this->effects.size()){
173168
effects_calc->logEffectType(effects[idx].type, true); // Effect off
174169
effects[idx].type=FFB_EFFECT_NONE;
175170
for(int i=0; i< MAX_AXIS; i++) {
@@ -263,7 +258,7 @@ void HidFFB::ffb_control(uint8_t cmd){
263258

264259

265260
void HidFFB::set_constant_effect(FFB_SetConstantForce_Data_t* data){
266-
if(data->effectBlockIndex == 0 || data->effectBlockIndex > MAX_EFFECTS){
261+
if(data->effectBlockIndex == 0 || data->effectBlockIndex > effects.size()){
267262
return;
268263
}
269264
cfUpdatePeriodAvg.addValue((uint32_t)(HAL_GetTick() - lastCfUpdate));
@@ -302,15 +297,15 @@ void HidFFB::new_effect(FFB_CreateNewEffect_Feature_Data_t* effect){
302297
//reportFFBStatus.effectBlockIndex = index;
303298
blockLoad_report.effectBlockIndex = index;
304299
used_effects++;
305-
blockLoad_report.ramPoolAvailable = (MAX_EFFECTS-used_effects)*sizeof(FFB_Effect);
300+
blockLoad_report.ramPoolAvailable = (effects.size()-used_effects)*sizeof(FFB_Effect);
306301
blockLoad_report.loadStatus = 1;
307302
sendStatusReport(index);
308303

309304

310305
}
311306
void HidFFB::set_effect(FFB_SetEffect_t* effect){
312307
uint8_t index = effect->effectBlockIndex;
313-
if(index > MAX_EFFECTS || index == 0)
308+
if(index > effects.size() || index == 0)
314309
return;
315310

316311
FFB_Effect* effect_p = &effects[index-1];
@@ -344,7 +339,7 @@ void HidFFB::set_effect(FFB_SetEffect_t* effect){
344339
}
345340

346341
void HidFFB::set_condition(FFB_SetCondition_Data_t *cond){
347-
if(cond->effectBlockIndex == 0 || cond->effectBlockIndex > MAX_EFFECTS){
342+
if(cond->effectBlockIndex == 0 || cond->effectBlockIndex > effects.size()){
348343
return;
349344
}
350345
uint8_t axis = cond->parameterBlockOffset;
@@ -368,7 +363,7 @@ void HidFFB::set_condition(FFB_SetCondition_Data_t *cond){
368363
}
369364

370365
void HidFFB::set_effect_operation(FFB_EffOp_Data_t* report){
371-
if(report->effectBlockIndex == 0 || report->effectBlockIndex > MAX_EFFECTS){
366+
if(report->effectBlockIndex == 0 || report->effectBlockIndex > effects.size()){
372367
return; // Invalid ID
373368
}
374369
// Start or stop effect
@@ -407,7 +402,7 @@ void HidFFB::set_effect_operation(FFB_EffOp_Data_t* report){
407402

408403

409404
void HidFFB::set_envelope(FFB_SetEnvelope_Data_t *report){
410-
if(report->effectBlockIndex == 0 || report->effectBlockIndex > MAX_EFFECTS){
405+
if(report->effectBlockIndex == 0 || report->effectBlockIndex > effects.size()){
411406
return;
412407
}
413408
FFB_Effect *effect = &effects[report->effectBlockIndex - 1];
@@ -420,7 +415,7 @@ void HidFFB::set_envelope(FFB_SetEnvelope_Data_t *report){
420415
}
421416

422417
void HidFFB::set_ramp(FFB_SetRamp_Data_t *report){
423-
if(report->effectBlockIndex == 0 || report->effectBlockIndex > MAX_EFFECTS){
418+
if(report->effectBlockIndex == 0 || report->effectBlockIndex > effects.size()){
424419
return;
425420
}
426421
FFB_Effect *effect = &effects[report->effectBlockIndex - 1];
@@ -430,7 +425,7 @@ void HidFFB::set_ramp(FFB_SetRamp_Data_t *report){
430425
}
431426

432427
void HidFFB::set_periodic(FFB_SetPeriodic_Data_t* report){
433-
if(report->effectBlockIndex == 0 || report->effectBlockIndex > MAX_EFFECTS){
428+
if(report->effectBlockIndex == 0 || report->effectBlockIndex > effects.size()){
434429
return;
435430
}
436431
FFB_Effect* effect = &effects[report->effectBlockIndex-1];
@@ -443,7 +438,7 @@ void HidFFB::set_periodic(FFB_SetPeriodic_Data_t* report){
443438
}
444439

445440
uint8_t HidFFB::find_free_effect(uint8_t type){ //Will return the first effect index which is empty or the same type
446-
for(uint8_t i=0;i<MAX_EFFECTS;i++){
441+
for(uint8_t i=0;i<effects.size();i++){
447442
if(effects[i].type == FFB_EFFECT_NONE){
448443
return(i+1);
449444
}
@@ -452,7 +447,7 @@ uint8_t HidFFB::find_free_effect(uint8_t type){ //Will return the first effect i
452447
}
453448

454449
void HidFFB::reset_ffb(){
455-
for(uint8_t i=0;i<MAX_EFFECTS;i++){
450+
for(uint8_t i=0;i<effects.size();i++){
456451
free_effect(i);
457452
}
458453
//this->reportFFBStatus.effectBlockIndex = 1;

Firmware/FFBoard/UserExtensions/Src/FFBHIDMain.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ FFBHIDMain::FFBHIDMain(uint8_t axisCount) :
8383
axes_manager = std::make_unique<AxesManager>(&control);
8484
axes_manager->setEffectsCalculator(effects_calc.get());
8585
// Create the USB effects handler & pass in the effects calculator
86-
this->ffb = std::make_unique<HidFFB>();
87-
this->ffb->setEffectsCalculator(effects_calc.get());
86+
this->ffb = std::make_unique<HidFFB>(*effects_calc);
8887

8988
axes_manager->setAxisCount(axisCount);
9089
restoreFlash(); // Load parameters

0 commit comments

Comments
 (0)