diff --git a/.gitignore b/.gitignore index 5c3c5cd..5b29abd 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ *.swp *.swo *.log +*.orig diff --git a/cmd/main.go b/cmd/main.go index 3ec05e3..e93479c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -21,8 +21,6 @@ func main() { utils.StartSignalWatcher() start := time.Now() - log.Println("Command line:", os.Args[1:]) - commands.Version = version commands.Copyright = copyright cmd := commands.NewCli() diff --git a/internal/stm32CubeMX/stm32CubeMX.go b/internal/stm32CubeMX/stm32CubeMX.go index 425ecc1..2258529 100644 --- a/internal/stm32CubeMX/stm32CubeMX.go +++ b/internal/stm32CubeMX/stm32CubeMX.go @@ -27,13 +27,17 @@ import ( ) var watcher *fsnotify.Watcher +var running bool // true if running in wait loop waiting for .ioc file func procWait(proc *os.Process) { if proc != nil { _, _ = proc.Wait() log.Println("CubeMX ended") - watcher.Close() - log.Println("Watcher closed") + if watcher != nil { + watcher.Close() + log.Println("Watcher closed") + } + running = false // cubeMX ended, do not wait for .ioc file anymore } } @@ -103,11 +107,46 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int) } iocprojectPath := path.Join(cubeIocPath, "STM32CubeMX.ioc") mxprojectPath := path.Join(cubeIocPath, ".mxproject") + log.Printf("pid of CubeMX in daemon: %d", pid) + running = true + first := true + iocProjectWait := false for { - log.Printf("pid of CubeMX in daemon: %d", pid) proc, err := os.FindProcess(pid) // this only works for windows as it is now if err == nil { // cubeMX already runs - go procWait(proc) + if first { // only start wait thread once + go procWait(proc) + first = false + } + if !running { + break // out of loop if CubeMX does not run anymore + } + _, err := os.Stat(iocprojectPath) + if err != nil { + iocProjectWait = true + time.Sleep(time.Second) + continue + } + if iocProjectWait { // .ioc file was created new, there will not be multiple changes + log.Println("new project file:", iocprojectPath) + i := 1 + for ; i < 100; i++ { + _, err := os.Stat(mxprojectPath) + if err == nil { + break + } + time.Sleep(time.Second) + } + if i < 100 { + mxproject, err := IniReader(mxprojectPath, parms.Subsystem[0].Compiler, false) + if err == nil { + err = ReadContexts(iocprojectPath, parms) + if err == nil { + _ = WriteCgenYml(workDir, mxproject, parms) + } + } + } + } watcher, err = fsnotify.NewWatcher() if err != nil { log.Fatal(err) @@ -124,7 +163,7 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int) select { case event := <-watcher.Events: if event.Op&fsnotify.Write == fsnotify.Write { - log.Println("Modified file:", event.Name) + log.Println("Modified file:", event.Name, " changes ", changes) if changes == 1 { infomx0, _ = os.Stat(mxprojectPath) } @@ -176,6 +215,8 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int) } <-done log.Println("Watcher raus") + } else { + break // CubeMX does not run anymore } log.Println("Process loop") }