diff --git a/VERSION b/VERSION index 732507f6..4fe6fc4b 100644 --- a/VERSION +++ b/VERSION @@ -2,5 +2,5 @@ PACKAGE=picsimlab MAINVER=0 MINORVER=9 VERSION=0.9.2 -DATE=240622 +DATE=240629 VERSION_STABLE=0.9.1 diff --git a/src/boards/bsim_qemu.cc b/src/boards/bsim_qemu.cc index dd19a500..a0b81e55 100644 --- a/src/boards/bsim_qemu.cc +++ b/src/boards/bsim_qemu.cc @@ -372,7 +372,7 @@ int bsim_qemu::MInit(const char* processor, const char* _fname, float freq_) { StartThread(); while (!qemu_started) { #ifndef _WIN_ - usleep(100); + usleep(1000); #else Sleep(1); #endif @@ -931,7 +931,7 @@ void bsim_qemu::EvThreadRun(void) { qemu_started = 1; PICSimLab.SystemCmd(PSC_MUTEXUNLOCK, (const char*)&mtx_qinitId); #ifndef _WIN_ - usleep(100); + usleep(1000); #else Sleep(1); #endif diff --git a/src/boards/bsim_qemu.h b/src/boards/bsim_qemu.h index 2944b928..4a3e63e1 100644 --- a/src/boards/bsim_qemu.h +++ b/src/boards/bsim_qemu.h @@ -26,6 +26,7 @@ #ifndef BOARD_QEMU_H #define BOARD_QEMU_H +#include #include "../devices/bitbang_i2c.h" #include "../devices/bitbang_out.h" #include "../devices/bitbang_pwm.h" @@ -62,12 +63,12 @@ class bsim_qemu : virtual public board { unsigned char MGetPin(int pin) override; const picpin* MGetPinsValues(void) override; void MStep(void) override; - void MStepResume(void) override{}; + void MStepResume(void) override {}; void MReset(int flags) override; void EvThreadRun(void) override; int GetDefaultClock(void) override { return 1; }; int GetInc_ns(void) { return inc_ns; }; - virtual void PinsExtraConfig(int cfg){}; + virtual void PinsExtraConfig(int cfg) {}; user_timer_t timer; virtual void Run_CPU_ns(uint64_t time) = 0; bitbang_i2c_t master_i2c[2]; @@ -87,7 +88,7 @@ class bsim_qemu : virtual public board { const char* IcountToMipsItens(char* buffer); unsigned int ns_count; void pins_reset(void); - virtual void BoardOptions(int* argc, char** argv){}; + virtual void BoardOptions(int* argc, char** argv) {}; virtual const short int* GetPinMap(void) = 0; int icount; #ifdef _WIN_ @@ -106,7 +107,7 @@ class bsim_qemu : virtual public board { char fname_bak[2048]; unsigned short ADCvalues[16]; int mtx_qinitId; - int qemu_started; + std::atomic_int qemu_started; QEMUSimType SimType; std::string cmdline; int use_cmdline_extra; diff --git a/src/lib/oscilloscope.cc b/src/lib/oscilloscope.cc index 3cabe384..d6ab00fe 100644 --- a/src/lib/oscilloscope.cc +++ b/src/lib/oscilloscope.cc @@ -682,6 +682,8 @@ int COscilloscope::WindowCmd(const int id, const char* ControlName, const PICSim const char* Value, void* ReturnBuff) { if (Oscilloscope.OnWindowCmd) { return (*Oscilloscope.OnWindowCmd)(id, ControlName, action, Value, ReturnBuff); + } else { + printf("Error: COscilloscope::WindowCmd missing !\n"); } return -1; } \ No newline at end of file diff --git a/src/lib/part.cc b/src/lib/part.cc index 212dd2d2..ff39fc9c 100644 --- a/src/lib/part.cc +++ b/src/lib/part.cc @@ -318,6 +318,11 @@ void part::LoadPartImage(void) { SpareParts.SetPartOnDraw(id); std::string iname = PICSimLab.GetSharePath() + "parts/" + Type + "/" + GetPictureFileName(); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; + } + int bmp = SpareParts.CanvasCmd({.cmd = CC_LOADIMAGE, .LoadImage{iname.c_str(), Scale, 0, Orientation}}); if (bmp >= 0) { diff --git a/src/lib/part.h b/src/lib/part.h index 24f52c3b..de6084cf 100644 --- a/src/lib/part.h +++ b/src/lib/part.h @@ -76,17 +76,17 @@ class part { /** * @brief Called every start of CPU process */ - virtual void PreProcess(void){}; + virtual void PreProcess(void) {}; /** * @brief Called every CPU step */ - virtual void Process(void){}; + virtual void Process(void) {}; /** * @brief Called every end of CPU process */ - virtual void PostProcess(void){}; + virtual void PostProcess(void) {}; /** * @brief Return the filename of part picture @@ -126,12 +126,12 @@ class part { /** * @brief Reset part status */ - virtual void Reset(void){}; + virtual void Reset(void) {}; /** * @brief Stop part simulation */ - virtual void Stop(void){}; + virtual void Stop(void) {}; /** * @brief Event handler on the part @@ -142,7 +142,7 @@ class part { * @brief Event handler on the part */ virtual void OnMouseButtonPress(unsigned int inputId, unsigned int button, unsigned int x, unsigned int y, - unsigned int state){}; + unsigned int state) {}; /** * @brief Event handler on the part @@ -153,7 +153,7 @@ class part { * @brief Event handler on the part */ virtual void OnMouseButtonRelease(unsigned int inputId, unsigned int button, unsigned int x, unsigned int y, - unsigned int state){}; + unsigned int state) {}; /** * @brief Event handler on the part @@ -164,17 +164,17 @@ class part { * @brief Event handler on the part */ virtual void OnMouseMove(unsigned int inputId, unsigned int button, unsigned int x, unsigned int y, - unsigned int state){}; + unsigned int state) {}; /** * @brief Event handler on the part */ - virtual void EvKeyPress(unsigned int key, unsigned int mask){}; + virtual void EvKeyPress(unsigned int key, unsigned int mask) {}; /** * @brief Event handler on the part */ - virtual void EvKeyRelease(unsigned int key, unsigned int mask){}; + virtual void EvKeyRelease(unsigned int key, unsigned int mask) {}; /** * @brief Called to save part preferences in configuration file @@ -209,33 +209,33 @@ class part { /** * @brief Used by properties window combos */ - virtual void ComboChange(const char* controlname, std::string value){}; + virtual void ComboChange(const char* controlname, std::string value) {}; /** * @brief Used by properties window spin */ - virtual void SpinChange(const char* controlname, int value){}; + virtual void SpinChange(const char* controlname, int value) {}; /** * @brief Used by windows of parts */ virtual void ButtonEvent(const char* controlname, unsigned int button, unsigned int x, unsigned int y, - unsigned int state){}; + unsigned int state) {}; /** * @brief Used by windows of parts */ - virtual void KeyEvent(const char* controlname, unsigned int keysym, unsigned int ukeysym, unsigned int state){}; + virtual void KeyEvent(const char* controlname, unsigned int keysym, unsigned int ukeysym, unsigned int state) {}; /** * @brief Used by windows of parts */ - virtual void Event(const char* controlname){}; + virtual void Event(const char* controlname) {}; /** * @brief Used by properties window filedialogs */ - virtual void filedialog_EvOnClose(int retId){}; + virtual void filedialog_EvOnClose(int retId) {}; /** * @brief Called once on part creation @@ -251,12 +251,12 @@ class part { /** * @brief Called once on part after initialization */ - virtual void PostInit(void){}; + virtual void PostInit(void) {}; /** * @brief Called once on part destruction */ - virtual ~part(void){}; + virtual ~part(void) {}; /** * @brief Return the Bitmap of part @@ -333,6 +333,11 @@ class part { */ int GetId(void) { return id; }; + /** + * @brief Set the part ID + */ + void SetId(int id_) { id = id_; }; + /** * @brief Return if part need Draw update */ @@ -365,7 +370,7 @@ class part { /** * @brief Register remote control variables */ - virtual void RegisterRemoteControl(void){}; + virtual void RegisterRemoteControl(void) {}; int id; ///< part ID input_t input[MAX_IDS]; ///< input map elements diff --git a/src/lib/picsimlab.cc b/src/lib/picsimlab.cc index cb4234c6..42db6a8b 100644 --- a/src/lib/picsimlab.cc +++ b/src/lib/picsimlab.cc @@ -66,7 +66,7 @@ CPICSimLab::CPICSimLab() { lab = DEFAULT_BOARD; lab_ = DEFAULT_BOARD; Workspacefn = ""; - status.status = 0; + status = 0; scale = 1.0; need_resize = 0; tgo = 0; @@ -805,14 +805,14 @@ void CPICSimLab::SaveWorkspace(std::string fnpzw) { void CPICSimLab::SetSimulationRun(int run) { if (run) { - status.st[0] &= ~ST_DI; + status &= ~ST_DI; } else { - status.st[0] |= ST_DI; + status |= ST_DI; } } int CPICSimLab::GetSimulationRun(void) { - return (status.st[0] & ST_DI) == 0; + return (status & ST_DI) == 0; } void CPICSimLab::Configure(const char* home, int use_default_board, int create, const char* lfile, @@ -1129,14 +1129,14 @@ int CPICSimLab::LoadHexFile(std::string fname) { pa = GetMcuPwr(); SetMcuPwr(0); - while (PICSimLab.status.st[1] & ST_TH) + while (PICSimLab.status & ST_TH) usleep(100); // wait thread - status.st[0] |= ST_DI; + status |= ST_DI; msleep(BASETIMER); if (tgo) tgo = 1; - while (status.status & 0x0401) { + while (status & (ST_TH | ST_T1)) { msleep(1); WindowCmd(PW_MAIN, NULL, PWA_APPPROCESSEVENTS, NULL); } @@ -1175,7 +1175,7 @@ int CPICSimLab::LoadHexFile(std::string fname) { ret = !GetMcuRun(); SetMcuPwr(pa); - status.st[0] &= ~ST_DI; + status &= ~ST_DI; #ifdef NO_DEBUG UpdateStatus(PS_DEBUG, " "); @@ -1208,6 +1208,8 @@ int CPICSimLab::WindowCmd(const int id, const char* ControlName, const PICSimLab void* ReturnBuff) { if (PICSimLab.OnWindowCmd) { return (*PICSimLab.OnWindowCmd)(id, ControlName, action, Value, ReturnBuff); + } else { + printf("Error: CPICSimLab::WindowCmd missing !\n"); } return -1; } @@ -1215,6 +1217,8 @@ int CPICSimLab::WindowCmd(const int id, const char* ControlName, const PICSimLab int CPICSimLab::SystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* ReturnBuff) { if (PICSimLab.OnSystemCmd) { return (*PICSimLab.OnSystemCmd)(cmd, Arg, ReturnBuff); + } else { + printf("Error: CPICSimLab::SystemCmd missing !\n"); } return -1; } \ No newline at end of file diff --git a/src/lib/picsimlab.h b/src/lib/picsimlab.h index 59b14aa4..4021ee5d 100644 --- a/src/lib/picsimlab.h +++ b/src/lib/picsimlab.h @@ -33,6 +33,7 @@ extern char SERIALDEVICE[100]; +#include #include "board.h" #include "draw.h" #include "types.h" @@ -258,12 +259,9 @@ class CPICSimLab { static int SystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* ReturnBuff = NULL); - union { - char st[2]; - unsigned short int status; - } status; + std::atomic status; - int tgo; + std::atomic tgo; int plWidth; int plHeight; diff --git a/src/lib/serial_port.cc b/src/lib/serial_port.cc index 4bf8e740..ab5bcb7c 100644 --- a/src/lib/serial_port.cc +++ b/src/lib/serial_port.cc @@ -293,7 +293,7 @@ unsigned long serial_port_rec_tout(serialfd_t serialfd, unsigned char* c) { #else long nbytes; do { - usleep(100); + usleep(1000); nbytes = read(serialfd, c, 1); if (nbytes < 0) nbytes = 0; diff --git a/src/lib/spareparts.cc b/src/lib/spareparts.cc index 03dab774..dd42c344 100644 --- a/src/lib/spareparts.cc +++ b/src/lib/spareparts.cc @@ -81,12 +81,13 @@ part* CSpareParts::AddPart(const char* partname, const int x, const int y, const void CSpareParts::DeleteParts(void) { int partsc_ = partsc; - partsc = 0; // for disable process + partsc = 0; // for disable draw process partsc_aup = 0; useAlias = 0; for (int i = 0; i < partsc_; i++) { delete parts[i]; + parts[i] = NULL; } } @@ -504,15 +505,17 @@ bool CSpareParts::LoadConfig(std::string fname, const int disable_debug) { void CSpareParts::DeletePart(const int partn) { int partsc_ = partsc; - partsc = 0; // disable process + partsc = 0; // disable draw process partsc_aup = 0; delete parts[partn]; for (int i = partn; i < partsc_ - 1; i++) { parts[i] = parts[i + 1]; + parts[i]->SetId(i); } partsc_--; + parts[partsc_] = NULL; partsc = partsc_; } @@ -680,6 +683,8 @@ int CSpareParts::WPropCmd(const char* ControlName, const PICSimLabWindowAction a void* ReturnBuff) { if (SpareParts.OnWindowCmd) { return (*SpareParts.OnWindowCmd)(PW_WPROP, ControlName, action, Value, ReturnBuff); + } else { + printf("Error: CSpareParts::WindowCmd missing !\n"); } return -1; } @@ -688,6 +693,8 @@ int CSpareParts::WindowCmd(const int id, const char* ControlName, const PICSimLa void* ReturnBuff) { if (SpareParts.OnWindowCmd) { return (*SpareParts.OnWindowCmd)(id, ControlName, action, Value, ReturnBuff); + } else { + printf("Error: CSpareParts::WindowCmd missing !\n"); } return -1; } \ No newline at end of file diff --git a/src/lib/spareparts.h b/src/lib/spareparts.h index 9d9c6c47..8a6636c7 100644 --- a/src/lib/spareparts.h +++ b/src/lib/spareparts.h @@ -26,6 +26,7 @@ #ifndef SPAREPARTS #define SPAREPARTS +#include #include "draw.h" #include "part.h" #include "types.h" @@ -135,7 +136,7 @@ class CSpareParts { picpin* Pins; unsigned char PinsCount; unsigned char useAlias; - int partsc; + std::atomic partsc; part* parts[MAX_PARTS]; int partsc_aup; // always update list part* parts_aup[MAX_PARTS]; // always update list diff --git a/src/parts/input_keypad.cc b/src/parts/input_keypad.cc index 57a03426..7569904a 100644 --- a/src/parts/input_keypad.cc +++ b/src/parts/input_keypad.cc @@ -213,6 +213,7 @@ void cpart_keypad::ChangeType(unsigned char tp) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } type = tp; diff --git a/src/parts/input_pot.cc b/src/parts/input_pot.cc index 1e8fda52..d991d53e 100644 --- a/src/parts/input_pot.cc +++ b/src/parts/input_pot.cc @@ -300,6 +300,7 @@ void cpart_pot::ChangeSize(const unsigned int sz) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } Size = sz; if (Size > 4) { @@ -319,6 +320,9 @@ void cpart_pot::LoadPartImage(void) { Height = OHeight; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); diff --git a/src/parts/input_pot_r.cc b/src/parts/input_pot_r.cc index 9cacc141..20b3930b 100644 --- a/src/parts/input_pot_r.cc +++ b/src/parts/input_pot_r.cc @@ -330,6 +330,7 @@ void cpart_pot_r::ChangeSize(const unsigned int sz) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } Size = sz; if (Size > 4) { @@ -349,6 +350,9 @@ void cpart_pot_r::LoadPartImage(void) { Height = OHeight; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); diff --git a/src/parts/input_push_buttons.cc b/src/parts/input_push_buttons.cc index 6dea3ab3..9dc7c45c 100644 --- a/src/parts/input_push_buttons.cc +++ b/src/parts/input_push_buttons.cc @@ -470,6 +470,7 @@ void cpart_pbuttons::ChangeSize(const unsigned int sz) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } Size = sz; if (Size > 8) { @@ -489,6 +490,9 @@ void cpart_pbuttons::LoadPartImage(void) { Height = OHeight; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); diff --git a/src/parts/input_switches.cc b/src/parts/input_switches.cc index 163a6e3c..30ff90df 100644 --- a/src/parts/input_switches.cc +++ b/src/parts/input_switches.cc @@ -451,6 +451,7 @@ void cpart_switches::ChangeSize(const unsigned int sz) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } Size = sz; if (Size > 8) { @@ -470,6 +471,9 @@ void cpart_switches::LoadPartImage(void) { Height = OHeight; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); diff --git a/src/parts/other_logic_block.cc b/src/parts/other_logic_block.cc index 396d2995..0aeaad8c 100644 --- a/src/parts/other_logic_block.cc +++ b/src/parts/other_logic_block.cc @@ -619,6 +619,7 @@ void cpart_lblock::ChangeSize(const unsigned int sz) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } Size = sz; if (Size > 8) { @@ -638,6 +639,9 @@ void cpart_lblock::LoadPartImage(void) { Height = OHeight; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); diff --git a/src/parts/output_7s_Display.cc b/src/parts/output_7s_Display.cc index 79efce15..3f10a90b 100644 --- a/src/parts/output_7s_Display.cc +++ b/src/parts/output_7s_Display.cc @@ -784,6 +784,7 @@ void cpart_7s_display::ChangeType(unsigned char tp) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); + BitmapId = -1; } dtype = tp; diff --git a/src/parts/output_7s_Display_Dec.cc b/src/parts/output_7s_Display_Dec.cc index d1af7430..59bbc00d 100644 --- a/src/parts/output_7s_Display_Dec.cc +++ b/src/parts/output_7s_Display_Dec.cc @@ -815,6 +815,7 @@ void cpart_7s_display_dec::ChangeType(unsigned char tp) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); + BitmapId = -1; } dtype = tp; diff --git a/src/parts/output_LCD_hd44780.cc b/src/parts/output_LCD_hd44780.cc index 0a4dde6f..ab131975 100644 --- a/src/parts/output_LCD_hd44780.cc +++ b/src/parts/output_LCD_hd44780.cc @@ -377,6 +377,10 @@ void cpart_LCD_hd44780::PostProcess(void) { void cpart_LCD_hd44780::LoadPartImage(void) { int bmp = -1; + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } + switch (model) { case LCD16x2: bmp = SpareParts.CanvasCmd( diff --git a/src/parts/output_LCD_ili9341.cc b/src/parts/output_LCD_ili9341.cc index d8ee0b4b..d91721db 100644 --- a/src/parts/output_LCD_ili9341.cc +++ b/src/parts/output_LCD_ili9341.cc @@ -759,6 +759,7 @@ void cpart_LCD_ili9341::ChangeType(unsigned char tp) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } type_com = tp; diff --git a/src/parts/output_LED_WS2812B.cc b/src/parts/output_LED_WS2812B.cc index 752646df..dc1aaf48 100644 --- a/src/parts/output_LED_WS2812B.cc +++ b/src/parts/output_LED_WS2812B.cc @@ -80,6 +80,9 @@ void cpart_led_ws2812b::LoadPartImage(void) { Height = OHeight + yoff; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); @@ -234,6 +237,7 @@ void cpart_led_ws2812b::ChangeType(const unsigned int rows, const unsigned int c if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } unsigned int rows_ = rows; diff --git a/src/parts/output_LEDs.cc b/src/parts/output_LEDs.cc index ed2763f5..ccd11d5a 100644 --- a/src/parts/output_LEDs.cc +++ b/src/parts/output_LEDs.cc @@ -337,6 +337,7 @@ void cpart_leds::ChangeSize(const unsigned int sz) { if (BitmapId >= 0) { SpareParts.SetPartOnDraw(id); SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + BitmapId = -1; } Size = sz; if (Size > 8) { @@ -356,6 +357,9 @@ void cpart_leds::LoadPartImage(void) { Height = OHeight; SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd({.cmd = CC_CREATEIMAGE, .CreateImage{Width, Height, Scale, 0, Orientation}}); SpareParts.CanvasCmd({.cmd = CC_DESTROY}); diff --git a/src/parts/output_servo.cc b/src/parts/output_servo.cc index c85a842b..c77fb379 100644 --- a/src/parts/output_servo.cc +++ b/src/parts/output_servo.cc @@ -178,6 +178,9 @@ void cpart_servo::ReadPropertiesWindow(void) { void cpart_servo::LoadPartImage(void) { SpareParts.SetPartOnDraw(id); + if (BitmapId >= 0) { + SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}}); + } BitmapId = SpareParts.CanvasCmd( {.cmd = CC_LOADIMAGE, .LoadImage{(PICSimLab.GetSharePath() + "parts/" + Type + "/" + GetPictureFileName()).c_str(), Scale, 0, diff --git a/src/picsimlab1.cc b/src/picsimlab1.cc index 183a304f..17bd3701 100644 --- a/src/picsimlab1.cc +++ b/src/picsimlab1.cc @@ -100,11 +100,11 @@ void file_ready(const char* fname, const char* dir = NULL); void CPWindow1::timer1_EvOnTime(CControl* control) { // avoid run again before terminate previous - if (PICSimLab.status.st[0] & (ST_T1 | ST_DI)) + if (PICSimLab.status & (ST_T1 | ST_DI)) return; PICSimLab.SetSync(1); - PICSimLab.status.st[0] |= ST_T1; + PICSimLab.status |= ST_T1; #ifdef _NOTHREAD // printf ("overtimer = %i \n", timer1.GetOverTime ()); @@ -142,9 +142,10 @@ void CPWindow1::timer1_EvOnTime(CControl* control) { PICSimLab.tgo++; #ifndef _NOTHREAD - cpu_mutex->Lock(); - cpu_cond->Signal(); - cpu_mutex->Unlock(); + { + std::unique_lock lk(cpu_mutex); + cpu_cond.notify_one(); + } #endif if (PICSimLab.tgo > 3) { @@ -156,15 +157,10 @@ void CPWindow1::timer1_EvOnTime(CControl* control) { DrawBoard(); - PICSimLab.status.st[0] &= ~ST_T1; + PICSimLab.status &= ~ST_T1; } -CPWindow1::~CPWindow1(void) { -#ifndef _NOTHREAD - delete cpu_cond; - delete cpu_mutex; -#endif -} +CPWindow1::~CPWindow1(void) {} void CPWindow1::DrawBoard(void) { if (PICSimLab.GetNeedResize()) { @@ -238,12 +234,12 @@ void CPWindow1::thread1_EvThreadRun(CControl*) { if (PICSimLab.tgo) { t0 = cpuTime(); - PICSimLab.status.st[1] |= ST_TH; + PICSimLab.status |= ST_TH; PICSimLab.GetBoard()->Run_CPU(); if (PICSimLab.GetDebugStatus()) PICSimLab.GetBoard()->DebugLoop(); PICSimLab.tgo--; - PICSimLab.status.st[1] &= ~ST_TH; + PICSimLab.status &= ~ST_TH; t1 = cpuTime(); @@ -271,9 +267,10 @@ void CPWindow1::thread1_EvThreadRun(CControl*) { PICSimLab.SetIdleMs(0); } else { #ifndef _NOTHREAD - cpu_mutex->Lock(); - cpu_cond->Wait(); - cpu_mutex->Unlock(); + { + std::unique_lock lk(cpu_mutex); + cpu_cond.wait(lk); + } #endif } @@ -295,10 +292,10 @@ void CPWindow1::thread3_EvThreadRun(CControl*) { void CPWindow1::timer2_EvOnTime(CControl* control) { // avoid run again before terminate previous - if (PICSimLab.status.st[0] & (ST_T2 | ST_DI)) + if (PICSimLab.status & (ST_T2 | ST_DI)) return; - PICSimLab.status.st[0] |= ST_T2; + PICSimLab.status |= ST_T2; if (PICSimLab.GetBoard() != NULL) { PICSimLab.GetBoard()->RefreshStatus(); @@ -334,7 +331,7 @@ void CPWindow1::timer2_EvOnTime(CControl* control) { #endif PICSimLab.DeleteError(0); } - PICSimLab.status.st[0] &= ~ST_T2; + PICSimLab.status &= ~ST_T2; #ifdef CONVERTER_MODE if (cvt_fname.Length() > 3) { @@ -998,15 +995,16 @@ void CPWindow1::OnEndSimulation(void) { Window1.timer2.SetRunState(0); msleep(BASETIMER); - while (PICSimLab.status.status) { + while (PICSimLab.status) { msleep(1); Application->ProcessEvents(); } PICSimLab.tgo = 100000; #ifndef _NOTHREAD - Window1.cpu_mutex->Lock(); - Window1.cpu_cond->Signal(); - Window1.cpu_mutex->Unlock(); + { + std::unique_lock lk(Window1.cpu_mutex); + Window1.cpu_cond.notify_one(); + } #endif Window1.thread1.Destroy(); @@ -1177,7 +1175,7 @@ void CPWindow1::filedialog1_EvOnClose(int retId) { pa = PICSimLab.GetMcuPwr(); PICSimLab.SetMcuPwr(0); - while (PICSimLab.status.st[1] & ST_TH) + while (PICSimLab.status & ST_TH) usleep(100); // wait thread if (retId && (filedialog1.GetType() == (lxFD_OPEN | lxFD_CHANGE_DIR))) { @@ -1538,19 +1536,20 @@ int CPWindow1::OnCanvasCmd(const CanvasCmd_t cmd) { cmd.LoadImage.scale, cmd.LoadImage.usealpha)) { // find enpty bitmap int bid = -1; - for (int i = 0; i < BOARDS_MAX; i++) { + for (int i = 0; i < BITMAPS_MAX; i++) { if (Window1.Bitmaps[i] == NULL) { bid = i; break; } } - if ((bid >= 0) && (bid < BOARDS_MAX)) { + if ((bid >= 0) && (bid < BITMAPS_MAX)) { Window1.Bitmaps[bid] = new lxBitmap(&image, &Window1); image.Destroy(); return bid; } } + printf("PICSimLab: Erro CC_LOADIMAGE!\n"); return -1; } break; case CC_CREATEIMAGE: { @@ -1559,18 +1558,19 @@ int CPWindow1::OnCanvasCmd(const CanvasCmd_t cmd) { cmd.CreateImage.scale, cmd.CreateImage.scale)) { // find enpty bitmap int bid = -1; - for (int i = 0; i < BOARDS_MAX; i++) { + for (int i = 0; i < BITMAPS_MAX; i++) { if (Window1.Bitmaps[i] == NULL) { bid = i; break; } } - if ((bid >= 0) && (bid < BOARDS_MAX)) { + if ((bid >= 0) && (bid < BITMAPS_MAX)) { Window1.Bitmaps[bid] = new lxBitmap(&image, &Window1); image.Destroy(); return bid; } } + printf("PICSimLab: Erro CC_CREATEIMAGE!\n"); return -1; } break; case CC_ARC: @@ -2068,7 +2068,7 @@ int CPWindow1::OnSystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* case PSC_MUTEXCREATE: { int mid = -1; - for (int i = 0; i < BOARDS_MAX; i++) { + for (int i = 0; i < MUTEX_MAX; i++) { if (Window1.Mutexs[i] == NULL) { mid = i; break; @@ -2076,6 +2076,8 @@ int CPWindow1::OnSystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* } if (mid >= 0) { Window1.Mutexs[mid] = new lxMutex; + } else { + printf("PICSimLab: Error PSC_MUTEXCREATE!\n"); } return mid; } break; diff --git a/src/picsimlab1.h b/src/picsimlab1.h index 5c8a9860..7d078f10 100644 --- a/src/picsimlab1.h +++ b/src/picsimlab1.h @@ -50,11 +50,15 @@ #define CPWINDOW1 #include +#include +#include #include "lib/board.h" #include "lib/picsimlab.h" #define MAX_MIC 140 #define MAX_AUDIO 10 +#define BITMAPS_MAX 50 +#define MUTEX_MAX 30 /** * @brief CPWindow1 class @@ -181,9 +185,9 @@ class CPWindow1 : public CPWindow { int GetNeedClkUpdate(void) { return need_clkupdate; }; void SetNeedClkUpdate(const int ncu) { need_clkupdate = ncu; }; - lxBitmap* Bitmaps[BOARDS_MAX]; + lxBitmap* Bitmaps[BITMAPS_MAX]; CPWindow* Windows[BOARDS_MAX]; - lxMutex* Mutexs[BOARDS_MAX]; + lxMutex* Mutexs[MUTEX_MAX]; lxaudio* AudioChannels[MAX_AUDIO]; int WinCmd(CControl* ctrl, const PICSimLabWindowAction action, const char* Value, void* ReturnBuff); @@ -202,8 +206,8 @@ class CPWindow1 : public CPWindow { CThread thread3; // boards #ifndef _NOTHREAD - lxMutex* cpu_mutex; - lxCondition* cpu_cond; + std::mutex cpu_mutex; + std::condition_variable cpu_cond; #endif }; diff --git a/src/picsimlab1_d.cc b/src/picsimlab1_d.cc index 0e4f30b0..fa60d6a9 100644 --- a/src/picsimlab1_d.cc +++ b/src/picsimlab1_d.cc @@ -504,9 +504,4 @@ CPWindow1::CPWindow1(void) { memset(AudioChannels, 0, sizeof(AudioChannels)); Windows[PW_MAIN] = this; - -#ifndef _NOTHREAD - cpu_mutex = new lxMutex; - cpu_cond = new lxCondition(*cpu_mutex); -#endif } diff --git a/src/picsimlab5.cc b/src/picsimlab5.cc index 8c4483b6..00bf3364 100644 --- a/src/picsimlab5.cc +++ b/src/picsimlab5.cc @@ -816,6 +816,12 @@ void CPWindow5::pmenu2_Rotate_EvMenuActive(CControl* control) { void CPWindow5::pmenu2_Delete_EvMenuActive(CControl* control) { PartToMove = -1; SpareParts.DeletePart(PartSelected); + + for (int i = PartSelected; i < SpareParts.GetCount(); i++) { + Canvas[i] = Canvas[i + 1]; + } + Canvas[PartSelected].Destroy(); + update_all = 1; } @@ -1058,6 +1064,7 @@ int CPWindow5::OnCanvasCmd(const CanvasCmd_t cmd) { return bid; } } + printf("PICSimLab: Erro CC_LOADIMAGE!\n"); return -1; } break; case CC_CREATEIMAGE: { @@ -1078,6 +1085,7 @@ int CPWindow5::OnCanvasCmd(const CanvasCmd_t cmd) { return bid; } } + printf("PICSimLab: Erro CC_CREATEIMAGE!\n"); return -1; } break; case CC_ARC: