package I2C import ( "fmt" "sync" "time" ) type I2CDevice struct { *I2CBus // embeds bus bool // stores whether dev is currently connected int // addr Data *data } type data struct { string bool sync.Mutex } func (d I2CDevice) String() string { t := map[int]string{97:"DO Sensor",99:"pH Sensor",102:"Temperature Sensor"} return t[d.int] } func NewDevice(addr int,bus *I2CBus) *I2CDevice { d := &I2CDevice{} d.I2CBus = bus d.int = addr d.Data = &data{} return d } func (d *I2CDevice) GetAddr() int { return d.int } func (d *I2CDevice) GetStatus() string { // TODO s := d.I2CBus.GetStatus(d.int) if s { d.Data.Active() return "[green]ACTIVE[white]" } else { d.Data.Killed() return "[red]KILLED[white]" } } func (d *I2CDevice) GetType() string { // TODO return fmt.Sprint(d) } func (d *I2CDevice) GetData() string { d.Data.Lock() defer d.Data.Unlock() return d.Data.string } func (d *data) Active() { d.Lock() defer d.Unlock() if !d.bool { d.string = "" d.bool = true } } func (d *data) Killed() { d.Lock() defer d.Unlock() if d.bool { d.string = time.Now().Format("Mon at 03:04:05pm MST") d.bool = false } }