func main() { // get a pin by name. You could also just use the logical pin number, but this is // more readable. On BeagleBone, USR0 is an on-board LED. ledPin, err := hwio.GetPin("USR1") // Generally we wouldn't expect an error, but perhaps someone is running this a if err != nil { fmt.Println(err) os.Exit(1) } // Set the mode of the pin to output. This will return an error if, for example, // we were trying to set an analog input to an output. err = hwio.PinMode(ledPin, hwio.OUTPUT) if err != nil { fmt.Println(err) os.Exit(1) } // Run the blink forever for { hwio.DigitalWrite(ledPin, hwio.HIGH) hwio.Delay(1000) hwio.DigitalWrite(ledPin, hwio.LOW) hwio.Delay(1000) } }
func readButton(name string, buttonPin hwio.Pin, ledPin hwio.Pin) { //value readed from button, initially set to 0, because the button will not pressed oldValue := 0 t1 := time.Now() // loop for { // Read the button value value, e := hwio.DigitalRead(buttonPin) if e != nil { panic(e) } t1 = time.Now() // time at this point // Did value change? if value != oldValue { fmt.Printf("[%s] %v (%d)\n", name, t1.Sub(t0), value) oldValue = value // Write the value to the led. if value == 1 { hwio.DigitalWrite(ledPin, hwio.HIGH) } else { hwio.DigitalWrite(ledPin, hwio.LOW) } } } }
func readTracker(name string, trackerPin hwio.Pin) { oldValue := 0 //value readed from tracker, initially set to 0, because the tracker was innactive timeAction := time.Now() // time of the action detected // loop for theAcq.getState() != stateSTOPPED { // Read the tracker value value, e := hwio.DigitalRead(trackerPin) if e != nil { panic(e) } //timeActionOld=timeAction //store the last time timeAction = time.Now() // time at this point // Did value change? if (value == 1) && (value != oldValue) { if theAcq.getState() != statePAUSED { dataString := fmt.Sprintf("[%s], %v, %d\n", name, timeAction.Sub(theAcq.getTime0()), value) log.Println(dataString) theAcq.outputFile.WriteString(dataString) } // Write the value to the led indicating somewhat is happened if value == 1 { hwio.DigitalWrite(theOshi.actionLed, hwio.HIGH) } else { hwio.DigitalWrite(theOshi.actionLed, hwio.LOW) } } oldValue = value } }
func blinkingLed(ledPin hwio.Pin) int { // loop for { hwio.DigitalWrite(ledPin, hwio.HIGH) hwio.Delay(500) hwio.DigitalWrite(ledPin, hwio.LOW) hwio.Delay(500) } }
func (h *Hardware) SetStrike(state string) { switch state { case "unlocked": hwio.DigitalWrite(h.OutStrike, 1) case "locked": hwio.DigitalWrite(h.OutStrike, 0) default: panic(errors.New("unknown state")) } h.LastOutStrike = state }
func (h *Hardware) SetLed(state string) { switch state { case "on": hwio.DigitalWrite(h.OutLed, 1) case "off": hwio.DigitalWrite(h.OutLed, 0) default: panic(errors.New("unknown state")) } h.LastOutLed = state }
func resumeHandler(w http.ResponseWriter, r *http.Request) { theAcq.setStateRUNNING() p := &Page{Title: "Start", Body: "State: " + theAcq.state} hwio.DigitalWrite(theOshi.statusLed, hwio.HIGH) renderTemplate(w, "start", p) }
func main() { //value readed from button, initially set to 0, because the button will not pressed oldValue := 0 // Set up 'button' as an input button, e := hwio.GetPinWithMode(buttonPin, hwio.INPUT) if e != nil { panic(e) } // Set up 'led' as an output led, e := hwio.GetPinWithMode(ledPin, hwio.OUTPUT) if e != nil { panic(e) } fmt.Printf("Beginning.....\n") t0 := time.Now() // time 0 for { // Read the button value value, e := hwio.DigitalRead(button) t1 := time.Now() // time at this point if e != nil { panic(e) } // Did value change? if value != oldValue { fmt.Printf("[%v] %d\n", t1.Sub(t0), value) oldValue = value // Write the value to the led. if value == 1 { hwio.DigitalWrite(led, hwio.HIGH) } else { hwio.DigitalWrite(led, hwio.LOW) } } } }
func stopHandler(w http.ResponseWriter, r *http.Request) { theAcq.setStateSTOPPED() hwio.DigitalWrite(theOshi.statusLed, hwio.LOW) log.Println("Finnishing.....") // close the GPIO pins //hwio.CloseAll() theAcq.closeOutputFile() //close the file when finished p := &Page{Title: "Stop", Body: "State: " + theAcq.state} renderTemplate(w, "stop", p) //log.Printf("There are %v goroutines", runtime.NumGoroutine()) }
func readTracker(name string, trackerPin hwio.Pin) { //value readed from tracker, initially set to 0, because the tracker was innactive oldValue := 0 timeAction := time.Now() // time of the action detected timeActionOld := time.Now() // time of the action-1 detected //fmt.Printf("[%s] File: %s\n",name,outputFile) // loop for { // Read the tracker value value, e := hwio.DigitalRead(trackerPin) if e != nil { panic(e) } timeActionOld = timeAction //store the last time timeAction = time.Now() // time at this point // Did value change? if value != oldValue { dataString := fmt.Sprintf("[%s] %v (%v) -> %d\n", name, timeAction.Sub(t0), timeAction.Sub(timeActionOld), value) //fmt.Printf("[%s] %v (%v) -> %d\n", // name,timeAction.Sub(t0),timeAction.Sub(timeActionOld),value) fmt.Printf(dataString) outputFile.WriteString(dataString) oldValue = value // Write the value to the led indicating somewhat is happened if value == 1 { hwio.DigitalWrite(actionLed, hwio.HIGH) } else { hwio.DigitalWrite(actionLed, hwio.LOW) } } } }
func main() { // Get the pins we're going to use. These are on a beaglebone. sinPin, _ := hwio.GetPin("P9.11") sclkPin, _ := hwio.GetPin("P9.12") xlatPin, _ := hwio.GetPin("P9.13") gsclkPin, _ := hwio.GetPin("P9.14") blankPin, _ := hwio.GetPin("P9.15") fmt.Printf("Pins are: sin=%d, sclk=%d, xlat=%d, gsclk=%d, blank=%d", sinPin, sclkPin, xlatPin, gsclkPin, blankPin) // Make them all outputs e := hwio.PinMode(sinPin, hwio.OUTPUT) if e == nil { hwio.PinMode(sclkPin, hwio.OUTPUT) } if e == nil { hwio.PinMode(xlatPin, hwio.OUTPUT) } if e == nil { hwio.PinMode(gsclkPin, hwio.OUTPUT) } if e == nil { hwio.PinMode(blankPin, hwio.OUTPUT) } if e != nil { fmt.Printf("Could not initialise pins: %s", e) return } // set clocks low hwio.DigitalWrite(sclkPin, hwio.LOW) hwio.DigitalWrite(xlatPin, hwio.LOW) hwio.DigitalWrite(gsclkPin, hwio.LOW) // run GS clock in it's own space hwio.DigitalWrite(blankPin, hwio.HIGH) hwio.DigitalWrite(blankPin, hwio.LOW) clockData(gsclkPin) for b := 0; b < 4096; b++ { writeData(uint(b), sinPin, sclkPin, xlatPin) for j := 0; j < 10; j++ { hwio.DigitalWrite(blankPin, hwio.HIGH) hwio.DigitalWrite(blankPin, hwio.LOW) clockData(gsclkPin) } // hwio.Delay(100) } // hwio.ShiftOut(dataPin, clockPin, uint(data), hwio.MSBFIRST) }
func main() { // Get the pins we're going to use. These are on a beaglebone. dataPin, _ := hwio.GetPin("P8.3") // connected to pin 14 clockPin, _ := hwio.GetPin("P8.4") // connected to pin 11 storePin, _ := hwio.GetPin("P8.5") // connected to pin 12 // Make them all outputs hwio.PinMode(dataPin, hwio.OUTPUT) hwio.PinMode(clockPin, hwio.OUTPUT) hwio.PinMode(storePin, hwio.OUTPUT) data := 0 // Set the initial state of the clock and store pins to low. These both // trigger on the rising edge. hwio.DigitalWrite(clockPin, hwio.LOW) hwio.DigitalWrite(storePin, hwio.LOW) for { // Shift out 8 bits hwio.ShiftOut(dataPin, clockPin, uint(data), hwio.MSBFIRST) // You can use this line instead to clock out 16 bits if you have // two 74HC595's chained together // hwio.ShiftOutSize(dataPin, clockPin, uint(data), hwio.MSBFIRST, 16) // Pulse the store pin once hwio.DigitalWrite(storePin, hwio.HIGH) hwio.DigitalWrite(storePin, hwio.LOW) // Poor humans cannot read binary as fast as the machine can display it hwio.Delay(100) data++ } }
func startHandler(w http.ResponseWriter, r *http.Request) { // manage file depending the previous state if theAcq.getState() == stateSTOPPED { theAcq.reopenOutputFile() log.Printf("Reopen file %s\n", theAcq.outputFile) } // sets the new time0 only with a new scenery if theAcq.getState() == stateNEW { theAcq.setTime0() } theAcq.setStateRUNNING() //waitTillButtonPushed(buttonA) p := &Page{Title: "Start", Body: "State: " + theAcq.state} hwio.DigitalWrite(theOshi.statusLed, hwio.HIGH) log.Println("Beginning.....") renderTemplate(w, "start", p) // launch the trackers log.Printf("There are %v goroutines", runtime.NumGoroutine()) log.Printf("Launching the Gourutines") go theAcq.readFromArduino2() log.Println("Started Arduino") go readTracker("A", theOshi.trackerA) log.Println("Started Tracker A") go readTracker("B", theOshi.trackerB) log.Println("Started Tracker B") go readTracker("C", theOshi.trackerC) log.Println("Started Tracker C") go readTracker("D", theOshi.trackerD) log.Println("Started Tracker D") log.Printf("There are %v goroutines", runtime.NumGoroutine()) }
// val is a 12-bit int func writeData(val uint, sinPin hwio.Pin, sclkPin hwio.Pin, xlatPin hwio.Pin) { fmt.Printf("writing data %d\n", val) bits := 0 mask := uint(1) << 11 for i := 0; i < 16; i++ { v := val for j := 0; j < 12; j++ { if (v & mask) != 0 { hwio.DigitalWrite(sinPin, hwio.HIGH) } else { hwio.DigitalWrite(sinPin, hwio.HIGH) } hwio.DigitalWrite(sclkPin, hwio.HIGH) hwio.DigitalWrite(sclkPin, hwio.LOW) v = v << 1 bits++ } } hwio.DigitalWrite(xlatPin, hwio.HIGH) hwio.DigitalWrite(xlatPin, hwio.LOW) fmt.Printf("Wrote %d bits\n", bits) }
func clockData(gsclkPin hwio.Pin) { for g := 0; g < 4096; g++ { hwio.DigitalWrite(gsclkPin, hwio.HIGH) hwio.DigitalWrite(gsclkPin, hwio.LOW) } }
func (c *Car) Motor(v1, v2, v3, v4 int) { hwio.DigitalWrite(c.pins[in1], v1) hwio.DigitalWrite(c.pins[in2], v2) hwio.DigitalWrite(c.pins[in3], v3) hwio.DigitalWrite(c.pins[in4], v4) }
func (acq *Acquisition) readFromArduino() { var register, reg []byte // operate with the gobal variables theSensorData and theSensorDataInBytes; more speed? // don't use the first readding ?? I'm not sure about that reader := bufio.NewReader(acq.serialPort) // find the begging of an stream of data from the sensors register, err := reader.ReadBytes('\x24') if err != nil { log.Fatal(err) } //log.Println(register) //log.Printf(">>>>>>>>>>>>>>") // loop for acq.getState() != stateSTOPPED { // Read the serial and decode register = nil reg = nil //n, err = s.Read(register) for len(register) < 38 { // in case of \x24 chars repeted the length will be less than the expected 38 bytes reg, err = reader.ReadBytes('\x24') if err != nil { log.Fatal(err) } register = append(register, reg...) } receptionTime := time.Now() // time of the action detected if register[0] == '\x23' { // if first byte is '#', lets decode the stream of bytes in register //decode the register theSensorDataInBytes.sincroMicroSecondsInBytes = register[1:5] buf := bytes.NewReader(theSensorDataInBytes.sincroMicroSecondsInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.sincroMicroSeconds) theSensorDataInBytes.sensorMicroSecondsInBytes = register[5:9] buf = bytes.NewReader(theSensorDataInBytes.sensorMicroSecondsInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.sensorMicroSeconds) theSensorDataInBytes.distanceInBytes = register[9:13] buf = bytes.NewReader(theSensorDataInBytes.distanceInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.distance) theSensorDataInBytes.accXInBytes = register[13:17] buf = bytes.NewReader(theSensorDataInBytes.accXInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.accX) theSensorDataInBytes.accYInBytes = register[17:21] buf = bytes.NewReader(theSensorDataInBytes.accYInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.accY) theSensorDataInBytes.accZInBytes = register[21:25] buf = bytes.NewReader(theSensorDataInBytes.accZInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.accZ) theSensorDataInBytes.gyrXInBytes = register[25:29] buf = bytes.NewReader(theSensorDataInBytes.gyrXInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.gyrX) theSensorDataInBytes.gyrYInBytes = register[29:33] buf = bytes.NewReader(theSensorDataInBytes.gyrYInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.gyrY) theSensorDataInBytes.gyrZInBytes = register[33:37] buf = bytes.NewReader(theSensorDataInBytes.gyrZInBytes) err = binary.Read(buf, binary.LittleEndian, &theSensorData.gyrZ) } // if if acq.getState() != statePAUSED { //compound the dataline and write to the output //receptionTime= time.Now() // Alternative: time at this point dataString := fmt.Sprintf("[%s], %d, %d, %d, %d, %f, %f, %f, %f, %f, %f\n", "Ard", int64(receptionTime.Sub(theAcq.getTime0())/time.Microsecond), theSensorData.sincroMicroSeconds, theSensorData.sensorMicroSeconds, theSensorData.distance, theSensorData.accX, theSensorData.accY, theSensorData.accZ, theSensorData.gyrX, theSensorData.gyrY, theSensorData.gyrZ) log.Println(dataString) acq.outputFile.WriteString(dataString) } // Write the value to the led indicating somewhat is happened hwio.DigitalWrite(theOshi.actionLed, hwio.HIGH) hwio.DigitalWrite(theOshi.actionLed, hwio.LOW) } }
// Run is the block's main loop. Here we listen on the different channels we set up. func (b *ToDigitalPin) Run() { var pin hwio.Pin var pinStr string var tree *jee.TokenTree var path string var err error for { select { case ruleI := <-b.inrule: path, err = util.ParseString(ruleI, "Path") if err != nil { b.Error(err) continue } token, err := jee.Lexer(path) if err != nil { b.Error(err) continue } tree, err = jee.Parser(token) if err != nil { b.Error(err) continue } if pinStr != "" { b.Log("closing pin " + pinStr) err = hwio.ClosePin(pin) if err != nil { b.Error(err) } } pinStr, err = util.ParseString(ruleI, "Pin") if err != nil { b.Error(err) continue } pin, err = hwio.GetPin(pinStr) if err != nil { pinStr = "" pin = 0 b.Error(err) continue } err = hwio.PinMode(pin, hwio.OUTPUT) if err != nil { b.Error(err) continue } case <-b.quit: // quit the block err = hwio.ClosePin(pin) b.Error(err) return case c := <-b.queryrule: // deal with a query request c <- map[string]interface{}{ "Pin": pinStr, "Path": path, } case msg := <-b.in: if tree == nil { continue } valI, err := jee.Eval(tree, msg) if err != nil { b.Error(err) continue } val, ok := valI.(float64) if !ok { log.Println(msg) b.Error(errors.New("couldn't assert value to a float")) continue } if int(val) == 0 { hwio.DigitalWrite(pin, hwio.LOW) } else if int(val) == 1 { hwio.DigitalWrite(pin, hwio.HIGH) } else { b.Error(errors.New("value must be 0 for LOW and 1 for HIGH")) continue } } } }
func (acq *Acquisition) readFromArduino2() { var register, reg []byte var microSecondsInBytes []byte var distanceInBytes []byte var accXInBytes []byte var accYInBytes []byte var accZInBytes []byte var gyrXInBytes []byte var gyrYInBytes []byte var gyrZInBytes []byte var sincro byte var microSeconds uint32 var distance uint32 var accX float32 var accY float32 var accZ float32 var gyrX float32 var gyrY float32 var gyrZ float32 // don't use the first readding ?? I'm not sure about that reader := bufio.NewReader(acq.serialPort) // find the begging of an stream of data from the sensors register, err := reader.ReadBytes('\x24') if err != nil { log.Fatal(err) } //log.Println(register) //log.Printf(">>>>>>>>>>>>>>") // loop for acq.getState() != stateSTOPPED { // Read the serial and decode register = nil reg = nil //n, err = s.Read(register) for len(register) < 34 { // in case of \x24 chars repeted reg, err = reader.ReadBytes('\x24') if err != nil { log.Fatal(err) } register = append(register, reg...) } timeAction := time.Now() // time of the action detected if register[0] == '\x23' || register[0] == '\x64' { //decode the register if register[0] == '\x64' { sincro = 1 //sincro signal } else { sincro = 0 } microSecondsInBytes = register[1:5] buf := bytes.NewReader(microSecondsInBytes) err = binary.Read(buf, binary.LittleEndian, µSeconds) distanceInBytes = register[5:9] buf = bytes.NewReader(distanceInBytes) err = binary.Read(buf, binary.LittleEndian, &distance) accXInBytes = register[9:13] buf = bytes.NewReader(accXInBytes) err = binary.Read(buf, binary.LittleEndian, &accX) accYInBytes = register[13:17] buf = bytes.NewReader(accYInBytes) err = binary.Read(buf, binary.LittleEndian, &accY) accZInBytes = register[17:21] buf = bytes.NewReader(accZInBytes) err = binary.Read(buf, binary.LittleEndian, &accZ) gyrXInBytes = register[21:25] buf = bytes.NewReader(gyrXInBytes) err = binary.Read(buf, binary.LittleEndian, &gyrX) gyrYInBytes = register[25:29] buf = bytes.NewReader(gyrYInBytes) err = binary.Read(buf, binary.LittleEndian, &gyrY) gyrZInBytes = register[29:33] buf = bytes.NewReader(gyrZInBytes) err = binary.Read(buf, binary.LittleEndian, &gyrZ) } // if if acq.getState() != statePAUSED { //compound the dataline and write to the output //timeAction= time.Now() // Alternative: time at this point dataString := fmt.Sprintf("[%s], %v, %v, %v, %v, %v, %v, %v, %v, %v, %v\n", "Ard", timeAction.Sub(theAcq.getTime0()), sincro, microSeconds, distance, accX, accY, accZ, gyrX, gyrY, gyrZ) log.Println(dataString) acq.outputFile.WriteString(dataString) } // Write the value to the led indicating somewhat is happened hwio.DigitalWrite(theOshi.actionLed, hwio.HIGH) hwio.DigitalWrite(theOshi.actionLed, hwio.LOW) } }
func main() { // setup // open file (create if not exists!) if len(os.Args) != 2 { fmt.Printf("Usage: %s fileBaseName\n", os.Args[0]) os.Exit(1) } t := time.Now() thisOutputFileName := fmt.Sprintf("%s_%d%02d%02d%02d%02d%02d.csv", os.Args[1], t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) thisOutputFile, e := os.Create(thisOutputFileName) if e != nil { panic(e) } fmt.Printf("File: %s\n", thisOutputFileName) outputFile = thisOutputFile // Set up 'trakers' as inputs trackerA, e := hwio.GetPinWithMode(trackerAPin, hwio.INPUT) if e != nil { panic(e) } trackerB, e := hwio.GetPinWithMode(trackerBPin, hwio.INPUT) if e != nil { panic(e) } trackerC, e := hwio.GetPinWithMode(trackerCPin, hwio.INPUT) if e != nil { panic(e) } trackerD, e := hwio.GetPinWithMode(trackerDPin, hwio.INPUT) if e != nil { panic(e) } // Set up 'buttons' as inputs buttonA, e := hwio.GetPinWithMode(buttonAPin, hwio.INPUT) if e != nil { panic(e) } buttonB, e := hwio.GetPinWithMode(buttonBPin, hwio.INPUT) if e != nil { panic(e) } // Set up 'leds' as outputs statusLed, e := hwio.GetPinWithMode(statusLedPin, hwio.OUTPUT) if e != nil { panic(e) } thisActionLed, e := hwio.GetPinWithMode(actionLedPin, hwio.OUTPUT) if e != nil { panic(e) } actionLed = thisActionLed fmt.Printf("Push button A to start, B to finish...\n") // read the button A change to init the data readdings waitTillButtonPushed(buttonA) hwio.DigitalWrite(statusLed, hwio.HIGH) t0 = time.Now() fmt.Printf("Beginning.....\n") // launch the trackers go readTracker("A", trackerA) go readTracker("B", trackerB) go readTracker("C", trackerC) go readTracker("D", trackerD) // wait till button B is pushed waitTillButtonPushed(buttonB) hwio.DigitalWrite(statusLed, hwio.LOW) fmt.Printf("Finnishing.....\n") //fmt.Print("Enter to finish: ") //var input string //fmt.Scanln(&input) // close the GPIO pins defer hwio.CloseAll() defer thisOutputFile.Close() //close the file when main finished }