Пример #1
0
// StartListening starts another go routine that listens for changes in the touch sensor
func (sensor *TouchSensor) StartListening() {
	if sensor.isListening {
		return
	}
	go func() {
		snr := findSensor(sensor.port, TypeTouch)
		path := fmt.Sprintf("%s/%s", baseSensorPath, snr)
		curVal := utilities.ReadUInt8Value(path, "value0")

		for {
			select {
			case <-sensor.chStop:
				return
			default:
				value := utilities.ReadUInt8Value(path, "value0")
				if value != curVal {
					for _, ch := range sensor.channels {
						go sendEvent(ch, value)
					}
					curVal = value
				}
			}
			time.Sleep(time.Millisecond * 50)
		}
	}()
	sensor.isListening = true
}
Пример #2
0
// Reads the proximity value (in range 0 - 100) reported by the infrared sensor. A value of 100 corresponds to a range of approximately 70 cm.
func (self *InfraredSensor) ReadProximity() uint8 {
	snr := findSensor(self.port, TypeInfrared)

	path := fmt.Sprintf("%s/%s", baseSensorPath, snr)
	utilities.WriteStringValue(path, "mode", "IR-PROX")
	value := utilities.ReadUInt8Value(path, "value0")

	return value
}
Пример #3
0
// Reads the ambient light intensity in range [0, 100].
func (self *ColorSensor) ReadAmbientLightIntensity() uint8 {
	snr := findSensor(self.port, TypeColor)

	path := fmt.Sprintf("%s/%s", baseSensorPath, snr)
	utilities.WriteStringValue(path, "mode", "COL-AMBIENT")
	value := utilities.ReadUInt8Value(path, "value0")

	return value
}
Пример #4
0
// Reads one of seven color values.
func (self *ColorSensor) ReadColor() Color {
	snr := findSensor(self.port, TypeColor)

	path := fmt.Sprintf("%s/%s", baseSensorPath, snr)
	utilities.WriteStringValue(path, "mode", "COL-COLOR")
	value := utilities.ReadUInt8Value(path, "value0")

	return Color(value)
}
Пример #5
0
// Looks for other nearby ultrasonic sensors and returns true if one is found.
func (self *UltrasonicSensor) Listen() bool {
	snr := findSensor(self.port, TypeUltrasonic)

	path := fmt.Sprintf("%s/%s", baseSensorPath, snr)
	utilities.WriteStringValue(path, "mode", "US-LISTEN")
	value := utilities.ReadUInt8Value(path, "value0")

	if value == 1 {
		return true
	}

	return false
}
Пример #6
0
// Waits for the touch sensor to be pressed.
func (self *TouchSensor) Wait() {
	snr := findSensor(self.port, TypeTouch)
	path := fmt.Sprintf("%s/%s", baseSensorPath, snr)

	for {
		value := utilities.ReadUInt8Value(path, "value0")

		if value == 1 {
			return
		}

		time.Sleep(time.Millisecond * 50)
	}
}
Пример #7
0
// Returns the current system volume in range [0, 100].
func CurrentVolume() uint8 {
	return utilities.ReadUInt8Value("/sys/devices/platform/snd-legoev3", "volume")
}