Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now waiting for .ioc file #42

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading