Skip to content

Commit

Permalink
Merge pull request #64 from norberttak/63-bug-clean-up-on-start-a-new…
Browse files Browse the repository at this point in the history
…-flight

bug #63 : clean up on starting a new flight
  • Loading branch information
norberttak authored Jan 9, 2023
2 parents aea47c9 + 6ab6b44 commit 4b673e4
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 79 deletions.
9 changes: 7 additions & 2 deletions src/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ void ActionQueue::push(Action* action)
int multi_mid, multi_high;
action->get_dynamic_speed_params(&tick_per_sec_mid, &multi_mid, &tick_per_sec_high, &multi_high);

guard.lock();
if (tick_per_sec_mid != 0 || tick_per_sec_high != 0)
{
{
if (action_ageing_counters.count(action->get_hash()) == 0) {
action_ageing_counters[action->get_hash()] = new AgeingCounter();
}
Expand All @@ -306,7 +307,6 @@ void ActionQueue::push(Action* action)
counter->clear_old_events(2 * time_base_in_ms);
}

guard.lock();
action_queue.push_back(action);
guard.unlock();
}
Expand All @@ -332,5 +332,10 @@ void ActionQueue::clear_all_actions()
{
guard.lock();
action_queue.clear();
for (auto it = action_ageing_counters.begin(); it != action_ageing_counters.end(); ++it)
{
delete(it->second);
}
action_ageing_counters.clear();
guard.unlock();
}
4 changes: 2 additions & 2 deletions src/ArduinoHomeCockpit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ ArduinoHomeCockpit::ArduinoHomeCockpit(DeviceConfiguration& config) :UsbHidDevic
register_buttons(arduino_buttons);
register_displays(arduino_displays);

for (auto config_display : config.generic_displays)
for (auto &config_display : config.generic_displays)
{
int nr_of_bytes = 0;
for (auto panel_display : arduino_displays)
for (auto &panel_display : arduino_displays)
{
if (panel_display.config_name == config_display.first)
nr_of_bytes = panel_display.width;
Expand Down
31 changes: 25 additions & 6 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include "Logger.h"
#include "Configuration.h"

/*------ Plugin level configuration options ------------*/
Expand Down Expand Up @@ -33,12 +34,20 @@ void Configuration::clear()

Configuration::~Configuration()
{
device_configs.clear();
Logger(TLogLevel::logTRACE) << "Configuration destructor called" << std::endl;
clear();
}

/* ----------- Device specific configuration options ------------------*/
DeviceConfiguration::DeviceConfiguration()
{
Logger(TLogLevel::logTRACE) << "DeviceConfiguration constructor called" << std::endl;
}

DeviceConfiguration& DeviceConfiguration::operator=(const DeviceConfiguration& other)
{
Logger(TLogLevel::logTRACE) << "DeviceConfiguration = operator called" << std::endl;

if (this == &other)
return *this;

Expand All @@ -58,21 +67,31 @@ DeviceConfiguration& DeviceConfiguration::operator=(const DeviceConfiguration& o
return *this;
}

DeviceConfiguration::~DeviceConfiguration()
void DeviceConfiguration::clear()
{
for (auto act : push_actions)
for (auto &act : push_actions)
act.second.clear();
push_actions.clear();

for (auto act : release_actions)
for (auto &act : release_actions)
act.second.clear();
release_actions.clear();

for (auto act : encoder_inc_actions)
for (auto &act : encoder_inc_actions)
act.second.clear();
encoder_inc_actions.clear();

for (auto act : encoder_dec_actions)
for (auto &act : encoder_dec_actions)
act.second.clear();
encoder_dec_actions.clear();

for (auto &act : light_triggers)
act.second.clear();
light_triggers.clear();
}

DeviceConfiguration::~DeviceConfiguration()
{
Logger(TLogLevel::logTRACE) << "DeviceConfiguration destructor called" << std::endl;
clear();
}
3 changes: 3 additions & 0 deletions src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class DeviceConfiguration
{
public:
~DeviceConfiguration();
DeviceConfiguration();
DeviceConfiguration& operator=(const DeviceConfiguration& other);
void clear();

DeviceType device_type = UNKNOWN_DEVICE_TYPE;
unsigned int vid = 0;
unsigned int pid = 0;
Expand Down
38 changes: 17 additions & 21 deletions src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
#include "Device.h"
#include "Logger.h"

Device::Device(DeviceConfiguration& _config, int _read_buffer_size, int _write_buffer_size)
Device::Device(DeviceConfiguration& _config, int _read_buffer_size, int _write_buffer_size):
config(_config), read_buffer_size(_read_buffer_size), write_buffer_size(_write_buffer_size)
{
read_buffer = (unsigned char*)calloc(_read_buffer_size, sizeof(unsigned char));
read_buffer_old = (unsigned char*)calloc(_read_buffer_size, sizeof(unsigned char));
write_buffer = (unsigned char*)calloc(_write_buffer_size, sizeof(unsigned char));

read_buffer_size = _read_buffer_size;
write_buffer_size = _write_buffer_size;

config = _config;
}

Device::~Device()
Expand Down Expand Up @@ -114,7 +110,7 @@ bool Device::updateLightStates()
else
update_cycle++;

for (auto it : config.light_triggers)
for (auto &it : config.light_triggers)
{
std::vector<PanelLight>::iterator panel_light_it;

Expand All @@ -128,7 +124,7 @@ bool Device::updateLightStates()
if (panel_light_it == lights.end())
continue;

for (auto trigger : it.second)
for (auto &trigger : it.second)
{
TriggerType light_change = trigger->get_and_clear_stored_action();
switch (light_change) {
Expand Down Expand Up @@ -170,7 +166,7 @@ bool Device::updateLightStates()

void Device::process_selector_switch()
{
for (auto sel : selectors)
for (auto &sel : selectors)
{
if (!is_bit_changed(read_buffer, read_buffer_old, sel.bit))
continue;
Expand All @@ -179,20 +175,20 @@ void Device::process_selector_switch()
{
stored_button_states[sel.config_name] = 1;

for (auto button : buttons)
for (auto &button : buttons)
{
for (auto act : config.push_actions[button.config_name.c_str()])
for (auto &act : config.push_actions[button.config_name.c_str()])
{
act->set_condition_active(sel.config_name);
}
for (auto act : config.release_actions[button.config_name.c_str()])
for (auto &act : config.release_actions[button.config_name.c_str()])
{
act->set_condition_active(sel.config_name);
}
}

/* update condition for displays */
for (auto display : panel_displays)
for (auto &display : panel_displays)
{
if (config.multi_displays.count(display.config_name) > 0 && config.multi_displays[display.config_name] != NULL && config.multi_displays[display.config_name]->is_registered_selector(sel.config_name))
{
Expand All @@ -205,13 +201,13 @@ void Device::process_selector_switch()
{
stored_button_states[sel.config_name] = 0;

for (auto button : buttons)
for (auto &button : buttons)
{
for (auto act : config.push_actions[button.config_name.c_str()])
for (auto &act : config.push_actions[button.config_name.c_str()])
{
act->set_condition_inactive(sel.config_name);
}
for (auto act : config.release_actions[button.config_name.c_str()])
for (auto &act : config.release_actions[button.config_name.c_str()])
{
act->set_condition_inactive(sel.config_name);
}
Expand All @@ -231,15 +227,15 @@ void Device::process_and_store_button_states()
Logger(TLogLevel::logTRACE) << "Device " << button.config_name << " button bit changed " << std::endl;
if (get_bit_value(read_buffer, button.bit) && config.push_actions.find(button.config_name.c_str()) != config.push_actions.end())
{
for (auto act : config.push_actions[button.config_name])
for (auto &act : config.push_actions[button.config_name])
{
Logger(TLogLevel::logTRACE) << "Device " << button.config_name << " button push action called" << std::endl;
ActionQueue::get_instance()->push(act);
}
}
else if (!get_bit_value(read_buffer, button.bit) && config.release_actions.find(button.config_name.c_str()) != config.release_actions.end())
{
for (auto act : config.release_actions[button.config_name])
for (auto &act : config.release_actions[button.config_name])
{
Logger(TLogLevel::logTRACE) << "Device " << button.config_name << " button release action called" << std::endl;
ActionQueue::get_instance()->push(act);
Expand Down Expand Up @@ -278,7 +274,7 @@ void Device::process_and_store_encoder_rotations()
{
Logger(TLogLevel::logTRACE) << "encoder " << encoder.config_name << " incremented (" << (int)read_buffer_old[encoder.reg_index] << "->" << (int)read_buffer[encoder.reg_index] << ") d = " << encoder_delta << std::endl;
while (encoder_delta >= encoder.pulse_per_click) {
for (auto act : config.encoder_inc_actions[encoder.config_name])
for (auto &act : config.encoder_inc_actions[encoder.config_name])
{
ActionQueue::get_instance()->push(act);
}
Expand All @@ -289,7 +285,7 @@ void Device::process_and_store_encoder_rotations()
{
Logger(TLogLevel::logTRACE) << "encoder " << encoder.config_name << " decremented (" << (int)read_buffer_old[encoder.reg_index] << "->" << (int)read_buffer[encoder.reg_index] << ") d = " << encoder_delta << std::endl;
while (abs(encoder_delta) >= encoder.pulse_per_click) {
for (auto act : config.encoder_dec_actions[encoder.config_name])
for (auto &act : config.encoder_dec_actions[encoder.config_name])
{
ActionQueue::get_instance()->push(act);
}
Expand All @@ -299,4 +295,4 @@ void Device::process_and_store_encoder_rotations()
encoder.accumulated_change = encoder_delta;
Logger(TLogLevel::logTRACE) << "encoder " << encoder.config_name << " accumulated change = " << encoder.accumulated_change << std::endl;
}
}
}
4 changes: 2 additions & 2 deletions src/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class Device
private:
int compute_rotation_delta_with_overflow(unsigned char rot, unsigned char rot_old);
protected:
DeviceConfiguration config;
DeviceConfiguration &config;
std::vector<PanelButton> selectors;
std::vector<PanelButton> buttons;
std::vector<PanelDisplay> panel_displays;
std::vector< PanelRotaryEncoder> encoders;
std::vector<PanelRotaryEncoder> encoders;
std::map<std::string, int> stored_button_states;
std::map<std::string, TriggerType> stored_light_states;
std::vector<PanelLight> lights;
Expand Down
16 changes: 8 additions & 8 deletions src/FIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,26 @@ void FIPDevice::process_page_conditions(FIPScreen* screen)

if (!page_condition_name_old.empty())
{
for (auto button : buttons)
for (auto &button : buttons)
{
for (auto act : config.push_actions[button.config_name.c_str()])
for (auto &act : config.push_actions[button.config_name.c_str()])
{
act->set_condition_inactive(page_condition_name_old);
}
for (auto act : config.release_actions[button.config_name.c_str()])
for (auto &act : config.release_actions[button.config_name.c_str()])
{
act->set_condition_inactive(page_condition_name_old);
}
}
}

for (auto button : buttons)
for (auto &button : buttons)
{
for (auto act : config.push_actions[button.config_name.c_str()])
for (auto &act : config.push_actions[button.config_name.c_str()])
{
act->set_condition_active(page_condition_name);
}
for (auto act : config.release_actions[button.config_name.c_str()])
for (auto &act : config.release_actions[button.config_name.c_str()])
{
act->set_condition_active(page_condition_name);
}
Expand All @@ -149,7 +149,7 @@ void FIPDevice::thread_func()
_thread_finish.store(false);

// create page registers in physical device
for (const auto& it_screen : config.fip_screens)
for (const auto &it_screen : config.fip_screens)
{
for (int page_index = 0; page_index <= it_screen.second->get_last_page_index(); page_index++)
{
Expand All @@ -162,7 +162,7 @@ void FIPDevice::thread_func()
while (_thread_run.load() == true)
{
std::this_thread::sleep_for(20ms);
for (auto screen : config.fip_screens)
for (auto &screen : config.fip_screens)
{
render_screen(screen.second);
process_page_conditions(screen.second);
Expand Down
4 changes: 2 additions & 2 deletions src/FIPScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ FIPScreen::FIPScreen() :GenericScreen()

FIPScreen::~FIPScreen()
{
for (const auto& it_page : pages)
for (const auto &it_page : pages)
delete it_page.second;

pages.clear();
}

void FIPScreen::evaluate_and_store_screen_action()
{
for (auto action : screen_action_queue)
for (auto &action : screen_action_queue)
{
int action_value = 0;
if (action->data_ref != NULL)
Expand Down
2 changes: 1 addition & 1 deletion src/GenericScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ GenericScreen::GenericScreen()

GenericScreen::~GenericScreen()
{
for (auto action : screen_action_queue)
for (auto &action : screen_action_queue)
{
delete action;
}
Expand Down
4 changes: 2 additions & 2 deletions src/LuaHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void LuaHelper::register_hid_device(UsbHidDevice* _device)

int LuaHelper::get_button_state(int vid, int pid, std::string button_name)
{
for (auto dev : hid_devices)
for (auto &dev : hid_devices)
{
if (dev->get_vid() == vid && dev->get_pid() == pid)
{
Expand All @@ -315,7 +315,7 @@ int LuaHelper::get_button_state(int vid, int pid, std::string button_name)

TriggerType LuaHelper::get_light_state(int vid, int pid, std::string light_name)
{
for (auto dev : hid_devices)
for (auto &dev : hid_devices)
{
if (dev->get_vid() == vid && dev->get_pid() == pid)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SaitekMultiPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ SaitekMultiPanel::SaitekMultiPanel(DeviceConfiguration& config) :UsbHidDevice(co

register_displays(multi_displays);

for (auto config_display : config.multi_displays)
for (auto &config_display : config.multi_displays)
{
config_display.second->set_nr_bytes(display_width);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SaitekRadioPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ SaitekRadioPanel::SaitekRadioPanel(DeviceConfiguration& config) :UsbHidDevice(co

register_displays(radio_displays);

for (auto config_display : config.multi_displays)
for (auto &config_display : config.multi_displays)
{
config_display.second->set_nr_bytes(display_width);
}
Expand Down
Loading

0 comments on commit 4b673e4

Please sign in to comment.