Добавлена возможность добавлять Arduino.h

Этот коммит содержится в:
Softonik 2022-11-19 22:30:19 +03:00 коммит произвёл Nikolay Kopitonenko
родитель 08dc7bad19
коммит 6d3dfea7d3
2 изменённых файлов: 26 добавлений и 9 удалений

Просмотреть файл

@ -14,14 +14,15 @@ const (
) )
func main() { func main() {
mode, source, target, header_file := getFlags() mode, source, target, header_file, addArduinoH := getFlags()
checkFlagsAreValid(source) checkFlagsAreValid(source)
safeTranspile(mode, source, target, header_file) safeTranspile(mode, source, target, header_file, addArduinoH)
} }
func getFlags() (int, string, string, string) { func getFlags() (int, string, string, string, bool) {
pmode := flag.Bool("p", false, "Mode: C++ or Python") pmode := flag.Bool("p", false, "Mode: C++ or Python")
header_file := flag.String("h", "", "Write headers to file also") header_file := flag.String("h", "", "Write headers to file also")
addArduinoH := flag.Bool("a", false, "Add Arduino.h")
flag.Parse() flag.Parse()
source := flag.Arg(0) source := flag.Arg(0)
@ -32,7 +33,7 @@ func getFlags() (int, string, string, string) {
mode = PYTHON_MODE mode = PYTHON_MODE
} }
return mode, source, target, *header_file return mode, source, target, *header_file, *addArduinoH
} }
func checkFlagsAreValid(source string) { func checkFlagsAreValid(source string) {
@ -48,10 +49,10 @@ func printUsage() {
flag.PrintDefaults() flag.PrintDefaults()
fmt.Print("\n") fmt.Print("\n")
fmt.Print("Example:\n") fmt.Print("Example:\n")
fmt.Printf("\tgo-tr [-p] controller.go controller.ino\n\n") fmt.Printf("\tgo-tr [-a] [-h header.h] [-p] controller.go [output.cpp]\n\n")
} }
func safeTranspile(mode int, source, target, header_file string) { func safeTranspile(mode int, source, target, header_file string, addArduinoH bool) {
// Read the Golang source file. // Read the Golang source file.
in, err := os.Open(source) in, err := os.Open(source)
if err != nil { if err != nil {
@ -85,6 +86,7 @@ func safeTranspile(mode int, source, target, header_file string) {
// Transpiles the Golang source into Arduino sketch. // Transpiles the Golang source into Arduino sketch.
service := transpile.NewService(in, out) service := transpile.NewService(in, out)
service.SetHeaderWriter(header_f) service.SetHeaderWriter(header_f)
service.AddIncludeArduinoH(addArduinoH)
if err := service.Start(); err != nil { if err := service.Start(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err) fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1) os.Exit(1)

Просмотреть файл

@ -16,6 +16,7 @@ import (
type Service interface { type Service interface {
Start() error Start() error
SetHeaderWriter(io.Writer) error SetHeaderWriter(io.Writer) error
AddIncludeArduinoH(bool)
} }
const ( const (
@ -38,6 +39,7 @@ type defaultService struct {
in io.Reader in io.Reader
out io.Writer out io.Writer
header io.Writer header io.Writer
addIncludeArduinoH bool
} }
// NewService creates a a new transpile and returns its address. // NewService creates a a new transpile and returns its address.
@ -57,6 +59,10 @@ func (s *defaultService) SetHeaderWriter(w io.Writer) error {
return nil return nil
} }
func (s *defaultService) AddIncludeArduinoH(add bool) {
s.addIncludeArduinoH = add
}
// Start ... // Start ...
func (s *defaultService) Start() error { func (s *defaultService) Start() error {
if s.in == nil { if s.in == nil {
@ -100,6 +106,7 @@ func (s *defaultService) Start() error {
} }
} }
s.printIncludeHeaders()
s.printFunctionDeclarations() s.printFunctionDeclarations()
s.printGoHelperDeclarations() s.printGoHelperDeclarations()
@ -117,6 +124,14 @@ func (s *defaultService) Start() error {
return nil return nil
} }
func (s *defaultService) printIncludeHeaders() {
if !s.addIncludeArduinoH {
return
}
h := "#include <Arduino.h>\n\n"
s.out.Write([]byte(h))
}
func (s *defaultService) printFunctionDeclarations() { func (s *defaultService) printFunctionDeclarations() {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()