Пример #1
0
def audioTest():
    print("Start audio test")
    audio = Audio(70)

    def onPressed(mode):
        vol = audio.getVolume()
        if mode is Button.NORMAL_PRESS:
            if vol == 100:
                audio.setVolume(70)
            else:
                audio.volumeUp()

        else:
            if audio.isMute:
                audio.unmute()
            else:
                audio.mute()

        print("volume {}".format(audio.getVolume()))

    btn = Button(pinBTN)
    btn.setListener(onPressed)

    try:
        print(audio.getVolume())
        print(audio.setVolume(20))
        print(audio.getVolume())

        while True:
            btn.loop()
    except KeyboardInterrupt:
        print("Cancel audio test")
Пример #2
0
def ledStripTest():
    print("Start led strip test")
    ledStrip = LEDStrip()

    animations = []


    animations.append(ColorSetAnimation(LEDAnimation.COLOR_BLACK))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_YELLOW, 100, 50, 15, True))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_CYAN, 100, 50, 15))


    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_BLUE, 500, 10, 15))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_GREEN, 50, 10, 15))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_RED, 50, 50, 15))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_MAGENTA, 20, 50, 15))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_CYAN, 20, 50, 15))
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_YELLOW, 20, 100, 15))
    animations.append(FadeCycleAnimation(RGBColor(0, 0, 0), RGBColor(255, 255, 255), 25, int(1000 / 200)))
    animations.append(FadeAnimation(RGBColor(255, 0, 0), RGBColor(0, 255, 0), 50, int(1000 / 50)))
    animations.append(FadeAnimation(RGBColor(0, 255, 0), RGBColor(0, 0, 255), 50, int(1000 / 50)))
    animations.append(RainbowAnimation())
    animations.append(TheaterChaseAnimation(LEDAnimation.COLOR_WHITE))
    animations.append(ColorWipeAnimation(LEDAnimation.COLOR_RED))
    animations.append(RainbowCycleAnimation())
    animations.append(TheaterChaseRainbowAnimation())
    animations.append(ColorSetAnimation(LEDAnimation.COLOR_BLACK))

    ledStrip.start()

    def setNextAnimation():
        global animIndex
        animIndex += 1
        if animIndex >= len(animations):
            animIndex = 0

        a = animations[animIndex]
        ledStrip.setAnimation(a)

    def onButtonClick(mode):
        if mode is Button.NORMAL_PRESS:
            setNextAnimation()

        else:
            a = ColorWipeAnimation(LEDAnimation.COLOR_BLACK)
            ledStrip.setAnimation(a)

    button = Button(pinBTN)
    button.setListener(onButtonClick)
    setNextAnimation()

    try:
        while True:
            button.loop()
    except KeyboardInterrupt:
        print("Cancel button-LED test")

    ledStrip.stop()
Пример #3
0
def buttonTest():
    print("Start button test")

    button = Button(pinBTN)
    button.setListener(printResult)
    try:

        while True:
            button.loop()
    except KeyboardInterrupt:
        print("Cancel button test")
Пример #4
0
def audioMixerTest():
    print("Start audio mixer test")

    import os
    from os import path

    appPath = os.path.dirname(os.path.abspath(__file__))
    audioFolder = path.join(appPath, "audio")

    audio = Audio(70)
    mixer = AudioMixer()
 
    mixer.addSound("start", path.join(audioFolder, "h_start.wav"))
    mixer.addSound("stop", path.join(audioFolder, "h_stop.wav"))
    mixer.addSound("speedUp", path.join(audioFolder, "h_speedup.wav"))
    mixer.addSound("idle", path.join(audioFolder, "h_idle.wav"))
    mixer.addSound("drive", path.join(audioFolder, "h_drive.wav"))


    def onPressed(mode):
        if mode is Button.NORMAL_PRESS:
            mixer.clearQueue()
            mixer.playSound("speedUp", False, 300)
            mixer.queue("drive", True)
        else:
            mixer.clearQueue()
            mixer.playSound("stop", False, 300)
            mixer.queue("idle", True)

    btn = Button(pinBTN)
    btn.setListener(onPressed)

    mixer.playSound("start", False, 0)
    mixer.queue("idle", True)

    try:
        while True:
            btn.loop()
    except KeyboardInterrupt:
        print("Cancel audio mixer test")

    mixer.stop()
Пример #5
0
def buttonLedTest():
    print("Start Button-LED test")

    led = LED(pinLED)
    button = Button(pinBTN)
    lc = LEDController(led)
    lc.start()

    def buttonListner(mode):
        printResult(mode)
        if mode is Button.NORMAL_PRESS:
            lc.blink(3, 0.5, 0.5)
        else:
            lc.blink(1, 3, 0)

    button.setListener(buttonListner)
    try:
        while True:
            button.loop()
    except KeyboardInterrupt:
        print("Cancel button-LED test")

    lc.stop()
Пример #6
0
class RockingMotorcycleGame:
    SOUND_START = "start"  # 11 sec
    SOUND_STOP = "stop"  # 6 sev
    SOUND_SPEEDUP = "speedUp"  # 13 sec
    SOUND_IDLE = "idle"  # 40 sec
    SOUND_DRIVE = "drive"  # 22 sec

    MODE_START = 0
    MODE_DRIVE = 1
    MODE_STOP = 2

    LED_SPEED = 100

    def __init__(self, pinBTN=27, pinLED=17, startVolume=70, imuAddress=0x6a, limit=350, changeDelta=1000):
        GPIO.setmode(GPIO.BCM)

        led = LED(pinLED)

        self.ledStrip = LEDStrip()
        self.audio = Audio(startVolume)

        self.mixer = AudioMixer()
        self.initAudioMixer()

        self.btn = Button(pinBTN)
        self.btn.setListener(self.audioBtnListener)

        self.ledControl = LEDController(led)
        self.ledControl.start()

        self.changeDelta = changeDelta
        self.currentMode = -1

        # self.imuController = IMUController(imuAddress, limit=limit)
        self.imuController = LSMController(imuAddress, limit=limit)
        self.initImuController()

        self.animDrive = TheaterChaseAnimation(LEDAnimation.COLOR_CYAN, 20, 50, 15, True)
        self.animIdle = FadeCycleAnimation(RGBColor(0, 0, 0), RGBColor(0, 80, 0), 25, 20)

        self.ledStrip = LEDStrip()
        self.ledStrip.start()

        self.speed = 0
        self.lastLoop = self.getMillis()

    def initImuController(self):
        self.imuChangeTime = 0
        self.imuController.setListener(self.imuListener)
        self.imuController.start()

    def initAudioMixer(self):
        appPath = path.dirname(path.abspath(__file__))
        audioFolder = path.join(appPath, "audio")

        self.mixer.addSound(self.SOUND_START, path.join(audioFolder, "h_start.wav"))
        self.mixer.addSound(self.SOUND_STOP, path.join(audioFolder, "h_stop.wav"))
        self.mixer.addSound(self.SOUND_SPEEDUP, path.join(audioFolder, "h_speedup.wav"))
        self.mixer.addSound(self.SOUND_IDLE, path.join(audioFolder, "h_idle.wav"))
        self.mixer.addSound(self.SOUND_DRIVE, path.join(audioFolder, "h_drive.wav"))

        self.mixer.setListener(self.mixerListener)

    def run(self):
        # indicate start
        self.ledControl.blink(2, 100, 100)

        while True:
            self.btn.loop()

            mode = self.getMode(self.imuChangeTime)

            accl = self.getAcceleration(mode, self.lastLoop)

            self.speed += accl

            if self.speed < 0:
                self.speed = 0
            elif self.speed > 100:
                self.speed = 100

            self.playLEDAnimation(mode, self.speed)

            self.playSound(mode)

            self.lastLoop = self.getMillis()

            self.currentMode = mode

            time.sleep(0.1)

    def mixerListener(self, id):
        return

    def playLEDAnimation(self, mode: int, speed: int):
        curAnim = self.ledStrip.getAnimation()

        if mode == self.MODE_START or speed <= 5:
            if curAnim != self.animIdle:
                self.ledStrip.setAnimation(self.animIdle)
        else:
            if curAnim is None or curAnim != self.animDrive:
                curAnim = self.animDrive
                self.ledStrip.setAnimation(curAnim)

            percentage = (100 - speed) / 100
            color = LEDAnimation.wheel(int(80 * (1 - percentage)))
            self.animDrive.update(color, int(10 + 200 * percentage), curAnim.iterations,
                                  curAnim.distance)

    def stop(self):
        self.mixer.stop()
        self.ledControl.stop()
        self.ledStrip.setAnimation(ColorSetAnimation(LEDAnimation.COLOR_BLACK))
        time.sleep(1)
        self.ledStrip.stop()
        self.imuController.stop()
        GPIO.cleanup()

    def imuListener(self):
        self.imuChangeTime = self.getMillis()

    def getMillis(self):
        return int(round(time.time() * 1000))

    def audioBtnListener(self, mode):
        vol = self.audio.getVolume()

        if mode is Button.NORMAL_PRESS:
            if vol == 100:
                self.audio.setVolume(70)
                vol = 70
            else:
                vol = self.audio.volumeUp()

        else:
            if self.audio.isMute:
                vol = self.audio.unmute()
            else:
                self.audio.mute()
                vol = 0

        if vol >= 70:
            self.ledControl.blink(int(vol / 10) - 6, self.LED_SPEED, self.LED_SPEED)
        else:
            self.ledControl.blink(1, 1000, self.LED_SPEED)

    def getMode(self, imuChangeTime: int):
        if imuChangeTime == 0:
            return self.MODE_START
        elif self.getMillis() - imuChangeTime < self.changeDelta:
            return self.MODE_DRIVE
        else:
            return self.MODE_STOP

    def playSound(self, mode: int):

        if self.currentMode == mode:
            return

        self.mixer.clearQueue()

        if mode == self.MODE_START:
            self.mixer.playSound(self.SOUND_START, False, 0)
            self.mixer.queue(self.SOUND_IDLE, True)
        elif mode == self.MODE_DRIVE:
            self.mixer.playSound(self.SOUND_SPEEDUP, False)
            self.mixer.queue(self.SOUND_DRIVE, True)
        elif mode == self.MODE_STOP:
            self.mixer.playSound(self.SOUND_STOP, False)
            self.mixer.queue(self.SOUND_IDLE, True)

    def getAcceleration(self, mode: int, lastLoop: int):

        accl = 0

        if mode != self.MODE_START:
            # get time between loops
            diff = self.getMillis() - lastLoop

            speedChange = (diff / 1000) * 5  # 17 kmh per sec

            if mode == self.MODE_DRIVE:
                # decreases speed
                accl = speedChange

            elif mode == self.MODE_STOP:
                # increase speed
                accl -= 2 * speedChange

        return accl