From 53d9ddfacd795ed16e9cd412dfef52d49560e0a5 Mon Sep 17 00:00:00 2001 From: bsongis Date: Wed, 7 Dec 2011 20:36:13 +0000 Subject: [PATCH] Minor changes to be able to wrap everything into a simulation tool inside companion9x --- src/Makefile | 3 --- src/file.cpp | 6 ++--- src/file.h | 2 +- src/general_menus.cpp | 1 + src/main_views.cpp | 1 + src/model_menus.cpp | 1 + src/pers.cpp | 1 - src/simpgmspace.cpp | 56 +++++++++++++++++++++++++++++++++++++++---- src/simpgmspace.h | 12 +++++++++- src/simu.cpp | 28 ++-------------------- 10 files changed, 72 insertions(+), 39 deletions(-) diff --git a/src/Makefile b/src/Makefile index 519cbcffa..7de8d7881 100644 --- a/src/Makefile +++ b/src/Makefile @@ -730,9 +730,6 @@ endif simu: $(CPPSRC) Makefile simu.cpp $(CPPSRC) simpgmspace.cpp *.h *.lbm eeprom.bin g++ simu.cpp $(CPPFLAGS) $(CPPSRC) simpgmspace.cpp $(ARCH) -MD -DSIMU -o simu $(FOXINC) $(FOXLIB) -gruvin9x.so: $(CPPSRC) Makefile export.cpp $(CPPSRC) simpgmspace.cpp *.h *.lbm - g++ export.cpp $(CPPFLAGS) $(CPPSRC) simpgmspace.cpp $(ARCH) -g -MD -DSIMU -shared -fPIC -Wl,-soname,$@ -o $@ - eeprom.bin: dd if=/dev/zero of=$@ bs=1 count=2048 diff --git a/src/file.cpp b/src/file.cpp index 93be653cc..8c78888e4 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -185,8 +185,8 @@ bool EeFsOpen() eeprom_read_block(&eeFs,0,sizeof(eeFs)); #ifdef SIMU - if(eeFs.version != EEFS_VERS) printf("bad eeFs.version\n"); - if(eeFs.mySize != sizeof(eeFs)) printf("bad eeFs.mySize\n"); + if(eeFs.version != EEFS_VERS) perror("bad eeFs.version\n"); + if(eeFs.mySize != sizeof(eeFs)) perror("bad eeFs.mySize\n"); #endif return eeFs.version == EEFS_VERS && eeFs.mySize == sizeof(eeFs); @@ -429,7 +429,7 @@ bool RlcFile::copy(uint8_t i_fileDst, uint8_t i_fileSrc) while ((len=theFile2.read(buf, 15))) { write(buf, len); - if (errno() != 0) { + if (write_errno() != 0) { s_sync_write = false; return false; } diff --git a/src/file.h b/src/file.h index c98e8311b..a0eefeb96 100644 --- a/src/file.h +++ b/src/file.h @@ -87,7 +87,7 @@ extern uint8_t s_write_err; // error reasons extern uint8_t s_sync_write; ///deliver current errno, this is reset in open -inline uint8_t errno() { return s_write_err; } +inline uint8_t write_errno() { return s_write_err; } class RlcFile: public EFile { diff --git a/src/general_menus.cpp b/src/general_menus.cpp index 34b2ee0ec..591f5947d 100644 --- a/src/general_menus.cpp +++ b/src/general_menus.cpp @@ -59,6 +59,7 @@ MenuFuncP_PROGMEM APM menuTabDiag[] = { void menuProcSetup(uint8_t event) { #define COUNT_ITEMS 19 +#undef PARAM_OFS #define PARAM_OFS 17*FW SIMPLE_MENU("RADIO SETUP", menuTabDiag, e_Setup, COUNT_ITEMS+1); diff --git a/src/main_views.cpp b/src/main_views.cpp index c730e63bc..dba42e403 100644 --- a/src/main_views.cpp +++ b/src/main_views.cpp @@ -21,6 +21,7 @@ #include "menus.h" +#undef ALTERNATE #define ALTERNATE 0x10 enum MainViews { diff --git a/src/model_menus.cpp b/src/model_menus.cpp index 9489e7ba0..a31c719ac 100644 --- a/src/model_menus.cpp +++ b/src/model_menus.cpp @@ -382,6 +382,7 @@ void EditName(uint8_t x, uint8_t y, char *name, uint8_t size, uint8_t event, boo } } +#undef PARAM_OFS #define PARAM_OFS (9*FW+2) void menuProcModel(uint8_t event) { diff --git a/src/pers.cpp b/src/pers.cpp index 77663b493..217f37bec 100644 --- a/src/pers.cpp +++ b/src/pers.cpp @@ -284,7 +284,6 @@ void eeLoadModel(uint8_t id) #ifdef SIMU if (sz > 0 && sz != sizeof(g_model)) { printf("Model data read=%d bytes vs %d bytes\n", sz, sizeof(ModelData)); - fflush(stdout); } #endif diff --git a/src/simpgmspace.cpp b/src/simpgmspace.cpp index c5487440e..18e28b125 100644 --- a/src/simpgmspace.cpp +++ b/src/simpgmspace.cpp @@ -29,18 +29,21 @@ volatile unsigned char pinb=0, pinc=0xff, pind, pine=0xff, ping=0xff, pinj=0xff, pinl=0; unsigned char portb, dummyport; -const char *eepromFile = "eeprom.bin"; +const char *eepromFile; extern uint16_t eeprom_pointer; extern const char* eeprom_buffer_data; uint8_t eeprom[EESIZE]; sem_t eeprom_write_sem; -pthread_t eeprom_thread_pid = 0; +bool eeprom_thread_running = true; void *eeprom_write_function(void *) { while (!sem_wait(&eeprom_write_sem)) { + if (!eeprom_thread_running) + return NULL; + FILE *fp = NULL; if (eepromFile) { @@ -72,19 +75,64 @@ void *eeprom_write_function(void *) return 0; } -void InitEepromThread() +bool main_thread_running = true; +void *main_thread(void *) { + g_menuStack[0] = menuMainView; + g_menuStack[1] = menuProcModelSelect; + + eeReadAll(); //load general setup and selected model + doSplash(); + checkLowEEPROM(); + checkTHR(); + checkSwitches(); + checkAlarm(); + + while (main_thread_running) { + perMain(); + usleep(1000); + } + return NULL; +} + +pthread_t main_thread_pid; +void StartMainThread() +{ + main_thread_running = true; + pthread_create(&main_thread_pid, NULL, &main_thread, NULL); +} + +void StopMainThread() +{ + main_thread_running = false; + pthread_join(main_thread_pid, NULL); +} + +pthread_t eeprom_thread_pid; +void StartEepromThread(const char *filename) +{ + eepromFile = filename; sem_init(&eeprom_write_sem, 0, 0); + eeprom_thread_running = true; assert(!pthread_create(&eeprom_thread_pid, NULL, &eeprom_write_function, NULL)); } + +void StopEepromThread() +{ + eeprom_thread_running = false; + sem_post(&eeprom_write_sem); + pthread_join(eeprom_thread_pid, NULL); +} + void eeprom_read_block (void *pointer_ram, const void *pointer_eeprom, size_t size) { if (eepromFile) { FILE *fp=fopen(eepromFile, "r"); - if(fseek(fp, (long) pointer_eeprom, SEEK_SET)==-1) perror("error in seek"); + if (!fp) { perror("error in fopen"); return; } + if (fseek(fp, (long) pointer_eeprom, SEEK_SET)==-1) perror("error in seek"); if (fread(pointer_ram, size, 1, fp) <= 0) perror("error in read"); fclose(fp); } diff --git a/src/simpgmspace.h b/src/simpgmspace.h index 06ba1b8ec..21484382e 100644 --- a/src/simpgmspace.h +++ b/src/simpgmspace.h @@ -28,6 +28,9 @@ #include #include +#undef min +#undef max + #define APM typedef unsigned char prog_uchar; @@ -91,6 +94,12 @@ extern sem_t eeprom_write_sem; #define SPIF dummyport #define SPCR dummyport +#define TIMSK dummyport +#define TIMSK1 dummyport + +#define UDR0 dummyport +#define OCIE1A dummyport + #define OUT_B_LIGHT 7 #define INP_E_ElevDR 2 #define INP_E_Trainer 5 @@ -118,7 +127,8 @@ extern sem_t eeprom_write_sem; extern volatile unsigned char pinb,pinc,pind,pine,ping,pinj,pinl; extern unsigned char portb,dummyport; -void InitEepromThread(); +void StartMainThread(); +void StartEepromThread(const char *filename="eeprom.bin"); extern const char *eepromFile; void eeprom_read_block (void *pointer_ram, diff --git a/src/simu.cpp b/src/simu.cpp index 3dfb4aa56..758c30db6 100644 --- a/src/simu.cpp +++ b/src/simu.cpp @@ -143,7 +143,6 @@ Gruvin9xSim::Gruvin9xSim(FXApp* a) bmf = new FXBitmapFrame(this,bmp,0,0,0,0,0,0,0,0,0); bmf->setOnColor(FXRGB(0,0,0)); - //getApp()->addChore(this,1); getApp()->addTimeout(this,2,100); } @@ -187,7 +186,6 @@ void Gruvin9xSim::makeSnapshot(const FXDrawable* drawable) } void Gruvin9xSim::doEvents() { - //getApp()->addChore(this,1); getApp()->runOneEvent(false); } @@ -355,25 +353,6 @@ void Gruvin9xSim::refreshDiplay() } } -void *main_thread(void *) -{ - g_menuStack[0] = menuMainView; - g_menuStack[1] = menuProcModelSelect; - - eeReadAll(); //load general setup and selected model - doSplash(); - checkLowEEPROM(); - checkTHR(); - checkSwitches(); - checkAlarm(); - - while(1) { - perMain(); - usleep(1000); - } - return 0; -} - Gruvin9xSim *th9xSim; void doFxEvents() { @@ -387,7 +366,6 @@ int main(int argc,char **argv) if(argc>=2){ eepromFile = argv[1]; } - printf("eeprom = %s\n", eepromFile); // Each FOX GUI program needs one, and only one, application object. // The application objects coordinates some common stuff shared between @@ -421,10 +399,8 @@ int main(int argc,char **argv) th9xSim->show(); // Otherwise the main window gets centred across my two monitors, split down the middle. #endif - InitEepromThread(); - - pthread_t pid; - pthread_create(&pid, NULL, &main_thread, NULL); + StartEepromThread(); + StartMainThread(); return application.run(); }