示例#1
0
文件: button.go 项目: rubyist/pi-pong
func (b *Button) Poll() {
	go func() {
		lastState, _ := hwio.DigitalRead(b.pin)
		for {
			currentState, _ := hwio.DigitalRead(b.pin)
			if currentState != lastState {
				lastState = currentState
				if currentState == 1 {
					if b.Rising != nil {
						b.Rising()
					}
				} else {
					if b.Falling != nil {
						b.Falling()
					}
				}

				if b.Change != nil {
					b.Change(currentState)
				}
			}
			hwio.Delay(50)
		}
	}()
}
示例#2
0
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
	}
}
示例#3
0
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)
			}
		}
	}

}
示例#4
0
文件: laundry.go 项目: drewp/homeauto
func DigitalRead(p hwio.Pin) int {
	v, err := hwio.DigitalRead(p)
	if err != nil {
		panic(err)
	}
	return v
}
示例#5
0
func waitTillButtonPushed(buttonPin hwio.Pin) int {

	// loop
	for {
		// Read the tracker value
		value, e := hwio.DigitalRead(buttonPin)
		if e != nil {
			panic(e)
		}
		// Was the button pressed, value = 1?
		if value == 1 {
			return value
		}
	}
}
示例#6
0
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)
			}
		}

	}
}
示例#7
0
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)
			}
		}
	}
}
示例#8
0
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *DigitalPin) Run() {
	var pin hwio.Pin
	var pinStr string
	var err error
	for {
		select {
		case ruleI := <-b.inrule:
			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.INPUT)
			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,
			}

		case <-b.inpoll:
			if pin == 0 {
				continue
			}
			v, err := hwio.DigitalRead(pin)
			if err != nil {
				b.Log(v)
				b.Error(err)
				continue
			}
			outValue := float64(v)
			out := map[string]interface{}{
				"value": outValue,
				"pin":   pinStr,
			}
			b.out <- out
		}
	}
}