Skip to content

Commit

Permalink
chg: Code refactor - Add callbacks to isolate gui access. !minor
Browse files Browse the repository at this point in the history
  • Loading branch information
lcgamboa committed Jan 14, 2024
1 parent f70b027 commit 21d222d
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 112 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ PACKAGE=picsimlab
MAINVER=0
MINORVER=9
VERSION=0.9.1
DATE=240113
DATE=240114
VERSION_STABLE=0.9.1
15 changes: 2 additions & 13 deletions src/lib/part.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,11 @@ void part::ReadInputMap(std::string fname) {
if (!strcmp("width", name)) {
sscanf(value, "%i", &x1);
Width = x1;
// Window1.SetplWidth(x1);
// Window1.draw1.SetWidth(x1*Window1.GetScale());
// Window1.SetWidth(185+x1*Window1.GetScale());
}

if (!strcmp("height", name)) {
sscanf(value, "%i", &y1);
Height = y1;
// Window1.SetplHeight(y1);
// Window1.draw1.SetHeight(y1*Window1.GetScale());
/*
#ifdef _WIN_
Window1.SetHeight(75+y1*Window1.GetScale());
#else
Window1.SetHeight(90+y1*Window1.GetScale());
#endif
*/
}

} while (value != NULL);
Expand Down Expand Up @@ -513,7 +501,8 @@ void part::DrawSlider(const output_t* output, const unsigned char pos, const std
canvas.RotatedText(val, output->x1 + 1, output->y1 + 5 + pos / 1.66, 0);
}

void part::DrawPotentiometer(const output_t* output, const unsigned char pos, const std::string val, const lxFont font) {
void part::DrawPotentiometer(const output_t* output, const unsigned char pos, const std::string val,
const lxFont font) {
canvas.SetColor(179, 179, 179);
canvas.Rectangle(1, output->x1, output->y1, output->x2 - output->x1, output->y2 - output->y1);
canvas.SetFgColor(0, 0, 0);
Expand Down
135 changes: 44 additions & 91 deletions src/lib/picsimlab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ CPICSimLab::CPICSimLab() {
tgo = 0;
plWidth = 10;
plHeight = 10;
need_clkupdate = 0;
use_dsr_reset = 1;
settodestroy = 0;
sync = 0;
Expand All @@ -85,6 +84,12 @@ CPICSimLab::CPICSimLab() {

OnUpdateStatus = NULL;
OnConfigure = NULL;
OnClockSet = NULL;
OnReadPreferences = NULL;
OnSavePrefs = NULL;
OnLoadHexFile = NULL;
OnOpenLoadHexFileDialog = NULL;

board_Event = NULL;
board_ButtonEvent = NULL;
}
Expand Down Expand Up @@ -196,11 +201,6 @@ void CPICSimLab::Set_mcudbg(int pd) {

void CPICSimLab::SetDebugPort(unsigned short dp) {
debug_port = dp;
if (debug) {
CToggleButton* togglebutton = (CToggleButton*)Window->GetChildByName("togglebutton1");
togglebutton->SetCheck(0);
(Window->*(togglebutton->EvOnToggleButton))(NULL);
}
}

void CPICSimLab::SetRemotecPort(unsigned short rcp) {
Expand All @@ -215,14 +215,8 @@ int CPICSimLab::GetDebugStatus(void) {
return debug;
}

void CPICSimLab::SetDebugStatus(int dbs, int updatebtn) {
if (debug != dbs) {
debug = dbs;
if (updatebtn) {
CToggleButton* togglebutton = (CToggleButton*)Window->GetChildByName("togglebutton1");
(Window->*(togglebutton->EvOnToggleButton))(NULL);
}
}
void CPICSimLab::SetDebugStatus(int dbs) {
debug = dbs;
}

void CPICSimLab::UpdateStatus(const PICSimlabStatus field, const std::string msg) {
Expand All @@ -232,19 +226,8 @@ void CPICSimLab::UpdateStatus(const PICSimlabStatus field, const std::string msg
}

void CPICSimLab::SetClock(const float clk, const int update) {
if (Window) {
CCombo* combo = (CCombo*)Window->GetChildByName("combo1");

if (update) {
if (clk < 1) {
combo->SetText(FloatStrFormat("%2.1f", clk));
} else {
combo->SetText(FloatStrFormat("%2.0f", clk));
}
need_clkupdate = 0;
} else {
need_clkupdate = 1;
}
if ((OnClockSet)) {
(*OnClockSet)(clk, update);
}
NSTEP = (int)(clk * NSTEPKT);
NSTEPJ = NSTEP / JUMPSTEPS;
Expand Down Expand Up @@ -280,8 +263,9 @@ void CPICSimLab::SavePrefs(std::string name, std::string value) {
}

void CPICSimLab::OpenLoadHexFileDialog(void) {
CItemMenu* imenu = (CItemMenu*)Window->GetChildByName("menu1_File_LoadHex");
(Window->*(imenu->EvMenuActive))(NULL);
if (OnOpenLoadHexFileDialog) {
(*OnOpenLoadHexFileDialog)();
}
}

void CPICSimLab::SetNeedReboot(int nr) {
Expand Down Expand Up @@ -358,8 +342,8 @@ void CPICSimLab::EndSimulation(int saveold, const char* newpath) {
SavePrefs("picsimlab_debugt", std::to_string(GetDebugType()));
SavePrefs("picsimlab_debugp", std::to_string(GetDebugPort()));
SavePrefs("picsimlab_remotecp", std::to_string(GetRemotecPort()));
if (Window) {
SavePrefs("picsimlab_position", std::to_string(Window->GetX()) + "," + std::to_string(Window->GetY()));
if (OnSavePrefs) {
(*OnSavePrefs)();
}
SavePrefs("picsimlab_scale", std::to_string(scale));
SavePrefs("picsimlab_dsr_reset", std::to_string(GetUseDSRReset()));
Expand Down Expand Up @@ -777,7 +761,9 @@ void CPICSimLab::SaveWorkspace(std::string fnpzw) {
SavePrefs("picsimlab_debug", std::to_string(GetDebugStatus()));
SavePrefs("picsimlab_debugt", std::to_string(GetDebugType()));
SavePrefs("picsimlab_debugp", std::to_string(GetDebugPort()));
SavePrefs("picsimlab_position", std::to_string(Window->GetX()) + "," + std::to_string(Window->GetY()));
if (OnSavePrefs) {
(*OnSavePrefs)();
}
SavePrefs("picsimlab_scale", std::to_string(scale));
SavePrefs("osc_on", std::to_string(pboard->GetUseOscilloscope()));
SavePrefs("spare_on", std::to_string(pboard->GetUseSpareParts()));
Expand Down Expand Up @@ -865,7 +851,7 @@ void CPICSimLab::Configure(const char* home, int use_default_board, int create,
char* name;
char* value;

int i, j;
int i;
int lc;
int load_demo = 0;

Expand Down Expand Up @@ -953,11 +939,8 @@ void CPICSimLab::Configure(const char* home, int use_default_board, int create,
if (disable_debug) {
debug = 0;
}
if (Window) {
((CToggleButton*)Window->GetChildByName("togglebutton1"))->SetCheck(debug);
}
#endif
SetDebugStatus(debug, 0);
SetDebugStatus(debug);
}

if (!strcmp(name, "picsimlab_debugt")) {
Expand All @@ -974,26 +957,11 @@ void CPICSimLab::Configure(const char* home, int use_default_board, int create,
sscanf(value, "%i", &spare_on);
}

if (!strcmp(name, "picsimlab_position")) {
sscanf(value, "%i,%i", &i, &j);
if (Window) {
Window->SetX(i);
Window->SetY(j);
}
printf("PICSimLab: Window position x=%i y=%i\n", i, j);
}

if (!strcmp(name, "picsimlab_scale")) {
if (create) {
double s;
sscanf(value, "%lf", &s);
SetScale(s);
if (Window) {
((CDraw*)Window->GetChildByName("draw1"))->SetWidth(plWidth * GetScale());
Window->SetWidth(185 + plWidth * GetScale());
((CDraw*)Window->GetChildByName("draw1"))->SetHeight(plHeight * GetScale());
Window->SetHeight(90 + plHeight * GetScale());
}
pboard->SetScale(GetScale());
printf("PICSimLab: Window scale %5.2f \n", GetScale());
}
Expand All @@ -1009,22 +977,15 @@ void CPICSimLab::Configure(const char* home, int use_default_board, int create,

if (!strcmp(name, "picsimlab_lfile")) {
SetFNAME(std::string(value));
if (Window) {
if (GetFNAME().length() > 1)
Window->GetChildByName("menu1")
->GetChildByName("menu1_File")
->GetChildByName("menu1_File_ReloadLast")
->SetEnable(1);
else
Window->GetChildByName("menu1")
->GetChildByName("menu1_File")
->GetChildByName("menu1_File_ReloadLast")
->SetEnable(0);
}
}

if (pboard != NULL)
if (pboard) {
pboard->ReadPreferences(name, value);
}

if (OnReadPreferences) {
(*OnReadPreferences)(name, value, create);
}

Oscilloscope.ReadPreferences(name, value);
SpareParts.ReadPreferences(name, value);
Expand Down Expand Up @@ -1053,9 +1014,11 @@ void CPICSimLab::Configure(const char* home, int use_default_board, int create,
#endif
}

/*
if ((OnConfigure)) {
(*OnConfigure)();
}
*/

pboard->MSetSerial(SERIALDEVICE);

Expand Down Expand Up @@ -1108,38 +1071,35 @@ void CPICSimLab::Configure(const char* home, int use_default_board, int create,
break;
}

pboard->Reset();
if ((OnConfigure)) {
(*OnConfigure)();
} else {
pboard->Reset();
}

SetProcessorName(pboard->GetProcessorName());
if (Window) {
pboard->EvOnShow();
pboard->Draw(((CDraw*)Window->GetChildByName("draw1")));
((CDraw*)Window->GetChildByName("draw1"))->SetVisible(1);

Window->SetTitle(((Instance > 0) ? ("PICSimLab[" + std::to_string(Instance) + "] - ") : ("PICSimLab - ")) +
std::string(boards_list[lab].name) + " - " + pboard->GetProcessorName());

#ifdef _USE_PICSTARTP_
if (prog_init() >= 0)
status = "PStart: Ok ";
else
status = "PStart:Error";
if (prog_init() >= 0)
status = "PStart: Ok ";
else
status = "PStart:Error";
#else
status = "";
status = "";
#endif

UpdateStatus(PS_RUN, "Running...");
UpdateStatus(PS_RUN, "Running...");

if (Window) {
((CThread*)Window->GetChildByName("thread1"))->Run(); // parallel thread
#ifndef __EMSCRIPTEN__
// FIXME remote control disabled
((CThread*)Window->GetChildByName("thread2"))->Run(); // parallel thread
#endif
((CTimer*)Window->GetChildByName("timer1"))->SetRunState(1);
((CTimer*)Window->GetChildByName("timer2"))->SetRunState(1);

Application->ProcessEvents();
}
Application->ProcessEvents();

Oscilloscope.SetBoard(pboard);
Oscilloscope.SetBaseTimer();
Expand Down Expand Up @@ -1250,16 +1210,9 @@ int CPICSimLab::LoadHexFile(std::string fname) {

GetBoard()->Reset();

if (GetMcuRun())
Window->SetTitle(
((GetInstanceNumber() > 0) ? ("PICSimLab[" + std::to_string(GetInstanceNumber()) + "] - ")
: ("PICSimLab - ")) +
std::string(boards_list[GetLab()].name) + " - " + GetBoard()->GetProcessorName() + " - " +
((const char*)basename(((CFileDialog*)Window->GetChildByName("filedialog1"))->GetFileName()).c_str()));
else
Window->SetTitle(((GetInstanceNumber() > 0) ? ("PICSimLab[" + std::to_string(GetInstanceNumber()) + "] - ")
: ("PICSimLab - ")) +
std::string(boards_list[GetLab()].name) + " - " + GetBoard()->GetProcessorName());
if (OnLoadHexFile) {
(*OnLoadHexFile)(fname);
}

ret = !GetMcuRun();

Expand Down
11 changes: 7 additions & 4 deletions src/lib/picsimlab.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,11 @@ class CPICSimLab {
/**
* @brief Set debug status flag
*/
void SetDebugStatus(int dbs, int updatebtn = 1);
void SetDebugStatus(int dbs);

void SetClock(const float clk, const int update = 1);
float GetClock(void);

int GetNeedClkUpdate(void) { return need_clkupdate; };

/**
* @brief Save the preferences
*/
Expand Down Expand Up @@ -270,6 +268,12 @@ class CPICSimLab {

void (*OnUpdateStatus)(const int field, const std::string msg);
void (*OnConfigure)(void);
void (*OnClockSet)(const float clk, const int update);
void (*OnReadPreferences)(const char* name, const char* value, const int create);
void (*OnSavePrefs)(void);
void (*OnLoadHexFile)(const std::string fname);
void (*OnOpenLoadHexFileDialog)(void);

void (CControl::*board_Event)(CControl* control);
void (CControl::*board_ButtonEvent)(CControl* control, const uint button, const uint x, const uint y,
const uint mask);
Expand Down Expand Up @@ -302,7 +306,6 @@ class CPICSimLab {
int debug_type;
int debug;
int need_resize;
int need_clkupdate;
std::vector<std::string> prefs;
int NeedReboot;
std::vector<std::string> Errors;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/spareparts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ bool CSpareParts::LoadConfig(std::string fname, const int disable_debug) {
((CToggleButton*)PICSimLab.GetWindow()->GetChildByName("togglebutton1"))->SetCheck(x);
}
#endif
PICSimLab.SetDebugStatus(x, 0);
PICSimLab.SetDebugStatus(x);
PICSimLab.SetDebugType(y);
} else if (!strcmp(name, "osc_on")) {
unsigned char osc_on;
Expand Down
Loading

0 comments on commit 21d222d

Please sign in to comment.