diff --git a/platforms/gpio/gpio.go b/platforms/gpio/gpio.go index 7b3746c3f..a94fea525 100644 --- a/platforms/gpio/gpio.go +++ b/platforms/gpio/gpio.go @@ -36,6 +36,8 @@ const ( Error = "error" // Data event Data = "data" + // Vibration event + Vibration = "vibration" ) // PwmWriter interface represents an Adaptor which has Pwm capabilities diff --git a/platforms/gpio/grove_drivers.go b/platforms/gpio/grove_drivers.go index 750cffd5d..04dbff0a3 100644 --- a/platforms/gpio/grove_drivers.go +++ b/platforms/gpio/grove_drivers.go @@ -11,6 +11,7 @@ var _ gobot.Driver = (*GroveSoundSensorDriver)(nil) var _ gobot.Driver = (*GroveButtonDriver)(nil) var _ gobot.Driver = (*GroveBuzzerDriver)(nil) var _ gobot.Driver = (*GroveLightSensorDriver)(nil) +var _ gobot.Driver = (*GrovePiezoVibrationSensorDriver)(nil) var _ gobot.Driver = (*GroveLedDriver)(nil) var _ gobot.Driver = (*GroveRotaryDriver)(nil) var _ gobot.Driver = (*GroveRelayDriver)(nil) @@ -31,6 +32,10 @@ type GroveLightSensorDriver struct { *AnalogSensorDriver } +type GrovePiezoVibrationSensorDriver struct { + *AnalogSensorDriver +} + type GroveBuzzerDriver struct { *BuzzerDriver } @@ -89,6 +94,22 @@ func NewGroveLightSensorDriver(a AnalogReader, name string, pin string, v ...tim } } +func NewGrovePiezoVibrationSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *GrovePiezoVibrationSensorDriver { + sensor := &GrovePiezoVibrationSensorDriver{ + AnalogSensorDriver: NewAnalogSensorDriver(a, name, pin, v...), + } + + sensor.AddEvent(Vibration) + + gobot.On(sensor.Event(Data), func(data interface{}) { + if data.(int) == 1023 { + gobot.Publish(sensor.Event(Vibration), data) + } + }) + + return sensor +} + func NewGroveSoundSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *GroveSoundSensorDriver { return &GroveSoundSensorDriver{ AnalogSensorDriver: NewAnalogSensorDriver(a, name, pin, v...),