Skip to content

Commit

Permalink
Now waiting for .ioc file (#42)
Browse files Browse the repository at this point in the history
* wait for .ioc file

* no file watch for .ioc file if it was created before
  • Loading branch information
bgn42 authored Apr 2, 2024
1 parent 9a79318 commit 8ea2746
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*.swp
*.swo
*.log
*.orig
2 changes: 0 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
51 changes: 46 additions & 5 deletions internal/stm32CubeMX/stm32CubeMX.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 8ea2746

Please sign in to comment.