|
| 1 | +package aosong |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/ffenix113/zigbee_home/sensor/base" |
| 7 | + "github.com/ffenix113/zigbee_home/templates/extenders" |
| 8 | + "github.com/ffenix113/zigbee_home/types/appconfig" |
| 9 | + dt "github.com/ffenix113/zigbee_home/types/devicetree" |
| 10 | + "github.com/ffenix113/zigbee_home/types/generator" |
| 11 | + "github.com/ffenix113/zigbee_home/zcl/cluster" |
| 12 | + "github.com/ffenix113/zigbee_home/types" |
| 13 | +) |
| 14 | + |
| 15 | +type DHT struct { |
| 16 | + *base.Base `yaml:",inline"` |
| 17 | + Pin types.Pin |
| 18 | + Variant string `yaml:"variant"` |
| 19 | +} |
| 20 | + |
| 21 | +func NewDHT() *DHT { |
| 22 | + return &DHT{ |
| 23 | + Variant: "", |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func (b DHT) String() string { |
| 28 | + return "Aosong" |
| 29 | +} |
| 30 | + |
| 31 | +func (DHT) Clusters() cluster.Clusters { |
| 32 | + return []cluster.Cluster{ |
| 33 | + cluster.Temperature{ |
| 34 | + MinMeasuredValue: -40, |
| 35 | + MaxMeasuredValue: 80, |
| 36 | + Tolerance: 1, |
| 37 | + }, |
| 38 | + cluster.NewRelativeHumidity(0, 100), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (b DHT) AppConfig() []appconfig.ConfigValue { |
| 43 | + return []appconfig.ConfigValue{ |
| 44 | + appconfig.NewValue("CONFIG_GPIO").Required(appconfig.Yes), |
| 45 | + appconfig.CONFIG_DHT.Required(appconfig.Yes), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (b DHT) ApplyOverlay(tree *dt.DeviceTree) error { |
| 50 | + |
| 51 | + pinctrlNode := tree.FindSpecificNode(dt.SearchByLabel(dt.NodeLabelPinctrl)) |
| 52 | + if pinctrlNode == nil { |
| 53 | + return dt.ErrNodeNotFound(dt.NodeLabelPinctrl) |
| 54 | + } |
| 55 | + |
| 56 | + pinLabel := fmt.Sprintf("gpio%d %d (GPIO_PULL_UP | GPIO_ACTIVE_LOW)", b.Pin.Port, b.Pin.Pin) |
| 57 | + |
| 58 | + props:= []dt.Property{ |
| 59 | + dt.NewProperty("compatible", dt.FromValue("aosong,dht")), |
| 60 | + dt.NewProperty("status", dt.FromValue("okay")), |
| 61 | + dt.NewProperty("dio-gpios", dt.Angled(dt.Label(pinLabel))), |
| 62 | + } |
| 63 | + // Add variant property only if Variant is not emtpy |
| 64 | + // For dht22 or AM2302 devices the string "dht22;" must be added to device tree overlay |
| 65 | + // See : https://docs.zephyrproject.org/latest/build/dts/api/bindings/sensor/aosong,dht.html |
| 66 | + if b.Variant != "" { |
| 67 | + props = append(props, dt.NewProperty(b.Variant, nil)) |
| 68 | + } |
| 69 | + |
| 70 | + pinctrlNode.AddNodes( |
| 71 | + &dt.Node{ |
| 72 | + Name: "dht22", |
| 73 | + Label: b.Label(), |
| 74 | + Properties: props, |
| 75 | + }, |
| 76 | + ) |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (DHT) Extenders() []generator.Extender { |
| 82 | + return []generator.Extender{ |
| 83 | + extenders.NewSensor(), |
| 84 | + extenders.GPIO{}, |
| 85 | + } |
| 86 | +} |
0 commit comments