func main() { fmt.Println("Attempting to engage in Penguin SPI-ing") fmt.Printf("Speed: %d\n\tDelay: %d\n\tBPW: %d\n", spi_speed, spi_delay, spi_bits) if err := embd.InitSPI(); err != nil { fmt.Println("Having a wee panic") panic(err) } defer embd.CloseSPI() var channel byte = 0 // Spi minor appears to be spi dev // Notes: chanel cf chip select // Todo, get spi1,2,and 2.1 spiBus := embd.NewSPIBus(embd.SPIMode0, channel, spi_speed, spi_bits, spi_delay) defer spiBus.Close() adc := mcp3008.New(mcp3008.SingleMode, spiBus) for i := 0; i < 20; i++ { time.Sleep(1 * time.Second) for ch := 0; ch < 16; ch++ { val, err := adc.AnalogValueAt(0) if err != nil { panic(err) } fmt.Printf("analog channel %v value is: %v\n", ch, val) } } }
//Init door func (d *Door) Init(cfg DoorSettings) { d.Cfg = cfg if err := embd.InitSPI(); err != nil { panic(err) } spiBus := embd.NewSPIBus(d.Cfg.Mode, d.Cfg.Channel, d.Cfg.Speed, d.Cfg.Bpw, d.Cfg.Delay) // defer embd.CloseSPI() // defer spiBus.Close() d.MPC3008 = mcp3008.New(mcp3008.SingleMode, spiBus) }
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) } }