diff --git a/.circleci/circleci.yaml b/.circleci/circleci.yaml new file mode 100644 index 0000000..a25983b --- /dev/null +++ b/.circleci/circleci.yaml @@ -0,0 +1,40 @@ +version: 2 +jobs: + build: + docker: + - image: circleci/golang:1.9 + working_directory: /go/src/github.com/WolfgangMau/chamg + steps: + - checkout + - run: make + - store_artifacts: + path: ./build + release: + docker: + - image: circleci/golang:1.9 + working_directory: /go/src/github.com/WolfgangMau/chamg + steps: + - checkout + - run: make package + - run: go get github.com/aktau/github-release + - run: github-release info --user ${CIRCLE_PROJECT_USERNAME} --repo ${CIRCLE_PROJECT_REPONAME} + - run: github-release release --user ${CIRCLE_PROJECT_USERNAME} --repo ${CIRCLE_PROJECT_REPONAME} --tag ${CIRCLE_TAG} --name ${CIRCLE_TAG} --description ${CIRCLE_TAG} --pre-release + - run: for f in build/*.tar.gz; do github-release upload --user ${CIRCLE_PROJECT_USERNAME} --repo ${CIRCLE_PROJECT_REPONAME} --tag ${CIRCLE_TAG} --name `basename ${f}` --file ${f}; done + - run: github-release edit --user ${CIRCLE_PROJECT_USERNAME} --repo ${CIRCLE_PROJECT_REPONAME} --tag ${CIRCLE_TAG} --name ${CIRCLE_TAG} --description ${CIRCLE_TAG} + +workflows: + version: 2 + build_and_release: + jobs: + - build: + filters: + tags: + only: /.*/ + - release: + requires: + - build + filters: + tags: + only: /v[0-9]+(\.[0-9]+)*(-.*)*/ + branches: + ignore: /.*/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f1e5d89 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/github.com/tarm/serial"] + path = vendor/github.com/tarm/serial + url = https://github.com/tarm/serial diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1a03881 --- /dev/null +++ b/Makefile @@ -0,0 +1,64 @@ +SHELL := /bin/bash + +# The destination for binaries +BUILD_DIR = build + +# The name of the executable (default is current directory name) +TARGET := $(shell echo $${PWD\#\#*/}) +.DEFAULT_GOAL: $(TARGET) + +# These will be provided to the target +VERSION :=0.9.0-rc1 +BUILD := `git rev-parse HEAD` + +# Use linker flags to provide version/build settings to the target +LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)" + +# go source files, ignore vendor directory +SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*") + +.PHONY: all build clean install uninstall fmt simplify check run + +all: check compile + +$(TARGET): $(SRC) + @go build $(LDFLAGS) -o $(TARGET) + +compile: check goxcompile + +goxcompile: export CGO_ENABLED=1 +goxcompile: dependencies + gox -arch amd64 -os 'darwin linux' -osarch 'windows/386' -output "$(BUILD_DIR)/{{.OS}}/$(NAME)/${TARGET}" . + +clean: + @rm -f $(TARGET) + @rm -rf $(BUILD_DIR) + +install: dependencies + @go install $(LDFLAGS) + +uninstall: clean + @rm -f $$(which ${TARGET}) + +fmt: + @gofmt -l -w $(SRC) + +simplify: + @gofmt -s -l -w $(SRC) + +package: compile + @for d in $$(ls build); do tar -czf $(BUILD_DIR)/${TARGET}-$${d}.tar.gz -C $(BUILD_DIR)/$${d} .; done + +check: dependencies + @test -z $(shell gofmt -l ${TARGET}.go | tee /dev/stderr) || echo "[WARN] Fix formatting issues with 'make fmt'" + @for d in $$(go list ./... | grep -v /vendor/); do golint $${d}; done + @go tool vet ${SRC} + +run: install + @$(TARGET) + +dependencies: + go get github.com/mitchellh/gox + go get github.com/jstemmer/go-junit-report + go get github.com/golang/lint/golint + git submodule update --init --recursive \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..15eed36 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# chamgo diff --git a/chamgo.go b/chamgo.go new file mode 100644 index 0000000..9fc22c0 --- /dev/null +++ b/chamgo.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "github.com/tarm/serial" + "log" + "os" + "strings" +) + +var ui = "" + +func main() { + argCount := len(os.Args[1:]) + if argCount != 1 { + fmt.Printf("Usage: %s \ne.g.:\n\t%s com5 (on windows)\n\t%s /dev/ttyUSB0 (on linux)\n\t%s /dev/cu.usbmodem1411 (on osx)\n", os.Args[0], os.Args[0], os.Args[0], os.Args[0]) + os.Exit(0) + } + c := &serial.Config{Name: os.Args[1], Baud: 9600} + s, err := serial.OpenPort(c) + if err != nil { + log.Fatal(err) + } + + //for _, c := range Commands { + //log.Printf("%s", sendCmd(Commands[0], s)) + //} + for { + ui = getUserInput() + if ui == "exit" || ui == "quit" { + os.Exit(0) + } + + rb := sendCmd(ui, s) + fmt.Print(string(rb)) + } +} + +/** +* sending cmds to serial +* returns {bytes} responde from device +**/ +func sendCmd(cmd string, port *serial.Port) (returnmessage []byte) { + cmd += "\r" + n, err := port.Write([]byte(cmd)) + if err != nil { + log.Fatal(err) + } + + buf := make([]byte, 256) + n, err = port.Read(buf) + if err != nil { + log.Fatal(err) + } + + return buf[0:n] +} + +/** +* for receiving user input +* returns {string} with no newline (|n) +**/ +func getUserInput() string { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() // use `for scanner.Scan()` to keep reading + line := scanner.Text() + return strings.Replace(string(line), "\n", "", -1) +} diff --git a/vendor/github.com/tarm/serial b/vendor/github.com/tarm/serial new file mode 160000 index 0000000..eaafced --- /dev/null +++ b/vendor/github.com/tarm/serial @@ -0,0 +1 @@ +Subproject commit eaafced92e9619f03c72527efeab21e326f3bc36