Beispiel #1
0
func main() {
	flag.Parse()
	if err := embd.InitSPI(); err != nil {
		panic(err)
	}
	defer embd.CloseSPI()

	bus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
	defer bus.Close()

	for i := 0; i < 30; i++ {
		time.Sleep(1 * time.Second)
		val, err := getSensorValue(bus)
		if err != nil {
			panic(err)
		}
		fmt.Printf("value is: %v\n", val)
	}
}
Beispiel #2
0
func main() {
	flag.Parse()
	fmt.Println("this is a sample code for mcp3008 10bit 8 channel ADC")

	if err := embd.InitSPI(); err != nil {
		panic(err)
	}
	defer embd.CloseSPI()

	spiBus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
	defer spiBus.Close()

	adc := mcp3008.New(mcp3008.SingleMode, spiBus)

	for i := 0; i < 20; i++ {
		time.Sleep(1 * time.Second)
		val, err := adc.AnalogValueAt(0)
		if err != nil {
			panic(err)
		}
		fmt.Printf("analog value is: %v\n", val)
	}
}
Beispiel #3
0
func main() {
	if err := embd.InitSPI(); err != nil {
		panic(err)
	}
	defer embd.CloseSPI()

	spiBus := embd.NewSPIBus(embd.SPIMode0, 0, 1000000, 8, 0)
	defer spiBus.Close()

	dataBuf := [3]uint8{1, 2, 3}

	if err := spiBus.TransferAndRecieveData(dataBuf[:]); err != nil {
		panic(err)
	}

	fmt.Println("received data is: %v", dataBuf)

	dataReceived, err := spiBus.ReceiveData(3)
	if err != nil {
		panic(err)
	}

	fmt.Println("received data is: %v", dataReceived)

	dataByte := byte(1)
	receivedByte, err := spiBus.TransferAndReceiveByte(dataByte)
	if err != nil {
		panic(err)
	}
	fmt.Println("received byte is: %v", receivedByte)

	receivedByte, err = spiBus.ReceiveByte()
	if err != nil {
		panic(err)
	}
	fmt.Println("received byte is: %v", receivedByte)
}