Example #1
0
File: blink.go Project: FyIoT/hwio
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)
	}
}
Example #2
0
func blinkingLed(ledPin hwio.Pin) int {
	// loop
	for {
		hwio.DigitalWrite(ledPin, hwio.HIGH)
		hwio.Delay(500)
		hwio.DigitalWrite(ledPin, hwio.LOW)
		hwio.Delay(500)
	}
}
Example #3
0
func NewNunchuck(module hwio.I2CModule) (*Nunchuck, error) {
	device := module.GetDevice(DEVICE_ADDRESS)
	n := &Nunchuck{device: device}

	n.SetJoystickZero(DEFAULT_JOYSTICK_ZERO_X, DEFAULT_JOYSTICK_ZERO_Y)
	n.SetAccelZero(DEFAULT_ACCEL_ZEROX, DEFAULT_ACCEL_ZEROY, DEFAULT_ACCEL_ZEROZ)

	// instead of the common 0x40 -> 0x00 initialization, we
	// use 0xF0 -> 0x55 followed by 0xFB -> 0x00.
	// this lets us use 3rd party nunchucks (like cheap $4 ebay ones)
	// while still letting us use official oness.
	// see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264805255

	e := device.WriteByte(0xF0, 0x55) // first config register
	if e != nil {
		return nil, e
	}

	hwio.Delay(1)

	e = device.WriteByte(0xFB, 0x00) // second config register
	if e != nil {
		return nil, e
	}

	return n, nil
}
Example #4
0
func (display *HD44780) Init(cols int, lines int) {
	display.displayFunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS

	if lines > 1 {
		display.displayFunction |= LCD_2LINE
	}
	display.numLines = lines

	// for some 1 line displays you can select a 10 pixel high font
	// if (dotsize != 0) && (lines == 1) {
	// 	_displayfunction |= LCD_5x10DOTS
	// }

	// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
	// according to datasheet, we need at least 40ms after power rises above 2.7V
	// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
	hwio.DelayMicroseconds(50000)

	// Now we pull both RS and R/W low to begin commands
	display.backlight = display.bl
	display.expanderWrite(display.backlight) // reset expander and turn backlight off (Bit 8 =1)
	hwio.Delay(1000)

	//put the LCD into 4 bit mode
	// this is according to the hitachi HD44780 datasheet
	// figure 24, pg 46

	// we start in 8bit mode, try to set 4 bit mode
	display.write4bits(0x03, 0)
	hwio.DelayMicroseconds(4500) // wait min 4.1ms

	// // second try
	display.write4bits(0x03, 0)
	hwio.DelayMicroseconds(4500) // wait min 4.1ms

	// // third go!
	display.write4bits(0x03, 0)
	hwio.DelayMicroseconds(150)

	// // finally, set to 4-bit interface
	display.write4bits(0x02, 0)

	// set # lines, font size, etc.
	display.Command(LCD_FUNCTIONSET | display.displayFunction)

	// turn the display on with no cursor or blinking default
	display.displayControl = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
	display.Display()

	// clear it off
	display.Clear()

	// Initialize to default text direction (for roman languages)
	display.displayMode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT

	// set the entry mode
	display.Command(LCD_ENTRYMODESET | display.displayMode)

	display.Home()
}
Example #5
0
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)
		}
	}()
}
Example #6
0
// Read the light level in low resolution mode, which is to a 4 lux precision.
func (t *BH1750FVI) ReadLightLevel(mode ReadMode) (float32, error) {
	// Get the settings
	m := modes[mode]

	// send a command to initiate low resolution read. The empty slice indicates there are no additional bytes,
	// just the command
	t.device.Write(m.deviceMode, []byte{})

	// wait for the sampling to be complete, max of 24ms
	hwio.Delay(m.sampleTimeMs)

	// read two bytes
	// @todo verify if this is correct. From Arduino examples I've seen, they use beginTransmission with the address,
	// @todo then requestFrom with the address. However the address 0x23 is also a device register. Need to check this.
	buffer, e := t.device.Read(m.deviceMode, 2)
	if e != nil {
		return 0, e
	}
	MSB := buffer[0]
	LSB := buffer[1]

	/* Convert 12bit int using two's compliment */
	/* Credit: http://bildr.org/2011/01/tmp102-arduino/ */
	level := ((int(MSB) << 8) | int(LSB))

	// divide by 16, since lowest 4 bits are fractional.
	return float32(level), nil
}
Example #7
0
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++
	}
}