1 Star 0 Fork 0

人月神话 / gpio

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mcp3008.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
人月神话 提交于 2021-03-20 11:47 . modify package name
// Copyright © 2018 Kent Gibson <warthog618@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"gitee.com/lhjw9810/gpio"
"gitee.com/lhjw9810/gpio/spi/mcp3w0c"
"github.com/warthog618/config"
"github.com/warthog618/config/blob"
"github.com/warthog618/config/blob/decoder/json"
"github.com/warthog618/config/dict"
"github.com/warthog618/config/env"
"github.com/warthog618/config/pflag"
)
// This example reads both channels from an MCP3008 connected to the RPI by four
// data lines - CSZ, CLK, DI, and DO. The default pin assignments are defined in
// loadConfig, but can be altered via configuration (env, flag or config file).
// The DI and DO may be tied to reduce the pin count by one, though I prefer to
// keep the two separate to remove the chance of accidental conflict.
// All pins other than DO are outputs so do not run this example on a board
// where those pins serve other purposes.
func main() {
cfg := loadConfig()
err := gpio.Open()
if err != nil {
panic(err)
}
defer gpio.Close()
tclk := cfg.MustGet("tclk").Duration()
adc := mcp3w0c.NewMCP3008(
tclk,
cfg.MustGet("clk").Int(),
cfg.MustGet("csz").Int(),
cfg.MustGet("di").Int(),
cfg.MustGet("do").Int())
defer adc.Close()
for ch := 0; ch < 8; ch++ {
d := adc.Read(ch)
fmt.Printf("ch%d=0x%04x\n", ch, d)
}
}
func loadConfig() *config.Config {
defaultConfig := map[string]interface{}{
"tclk": "500ns",
"clk": gpio.GPIO21,
"csz": gpio.GPIO6,
"di": gpio.GPIO19,
"do": gpio.GPIO26,
}
def := dict.New(dict.WithMap(defaultConfig))
cfg := config.New(
pflag.New(pflag.WithFlags(
[]pflag.Flag{{Short: 'c', Name: "config-file"}})),
env.New(env.WithEnvPrefix("MCP3008_")),
config.WithDefault(def))
cfg.Append(
blob.NewConfigFile(cfg, "config.file", "mcp3008.json", json.NewDecoder()))
cfg = cfg.GetConfig("", config.WithMust())
return cfg
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lhjw9810/gpio.git
git@gitee.com:lhjw9810/gpio.git
lhjw9810
gpio
gpio
v1.0.1

搜索帮助

344bd9b3 5694891 D2dac590 5694891