Exemple #1
0
 def on_start(self):
     self.knob = RotaryEncoder(UP_SWITCH, DOWN_SWITCH, 25,
                               self.encode_event, 2)
     self.waiter = Waiter.start(self.actor_ref)
     self.tracks = self.mopidy.tracklist.get_tl_tracks()
     first_track = self.mopidy.playback.get_current_tl_track()
     self._display_track(first_track)
Exemple #2
0
    def setRotaryInterface(self):
        global tunerknob
        global volumeknob

        # Internal pullup resistors can be disabled for rotary encoders (eg KY-040)
        # with their own pullup resistors. See rotary_gpio_pullup in radiod.conf

        if self.config.rotary_class == self.config.ALTERNATIVE:
            log.message("event.setInterface RotaryEncoder ALTERNATIVE",
                        log.DEBUG)
            volumeknob = RotaryEncoderAlternative(self.left_switch,
                                                  self.right_switch,
                                                  self.mute_switch,
                                                  self.volume_event)
            tunerknob = RotaryEncoderAlternative(self.down_switch,
                                                 self.up_switch,
                                                 self.menu_switch,
                                                 self.tuner_event)

        elif self.config.rotary_class == self.config.STANDARD:
            log.message("event.setInterface RotaryEncoder STANDARD", log.DEBUG)

            volumeknob = RotaryEncoder(self.left_switch,
                                       self.right_switch,
                                       self.mute_switch,
                                       self.volume_event,
                                       pullup=self.config.rotary_gpio_pullup)

            tunerknob = RotaryEncoder(self.down_switch,
                                      self.up_switch,
                                      self.menu_switch,
                                      self.tuner_event,
                                      pullup=self.config.rotary_gpio_pullup)

        msg = "Volume knob", self.left_switch, self.right_switch, self.mute_switch
        log.message(msg, log.DEBUG)
        msg = "Tuner knob", self.down_switch, self.up_switch, self.menu_switch
        log.message(msg, log.DEBUG)
        return
Exemple #3
0
 def __init__(self):
     RotaryEncoder(self.scrollUp,
                   self.scrollDown,
                   self.wheelButton,
                   callback=self.changeEvent)
     GPIO.add_event_detect(self.schalter,
                           GPIO.BOTH,
                           callback=self.changeAlarm)
     GPIO.add_event_detect(self.pauseButton,
                           GPIO.BOTH,
                           callback=self.pauseAlarm)
     GPIO.add_event_detect(self.stopButton,
                           GPIO.BOTH,
                           callback=self.stopAlarm)
Exemple #4
0
    def setCosmicInterface(self):
        from cosmic_class import Button
        global volumeknob
        global left_button, right_button, mute_button
        global up_button, down_button, menu_button
        log.message("event.setInterface Cosmic interface", log.DEBUG)

        # Buttons
        down_button = Button(self.down_switch, self.button_event, log)
        up_button = Button(self.up_switch, self.button_event, log)
        menu_button = Button(self.menu_switch, self.button_event, log)

        # Rotary encoder
        volumeknob = RotaryEncoder(self.left_switch, self.right_switch,
                                   self.mute_switch, self.volume_event)
        return
Exemple #5
0
    def __init__(self, events):
        self.events = events
        """ set up the button shutdown """
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(ControlBoard.OFFDETECTPIN,
                   GPIO.IN,
                   pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(ControlBoard.OFFDETECTPIN,
                              GPIO.FALLING,
                              callback=self.shutdown)
        """ set up the controller knob events to change the source and menus """
        self.controllerKnob = RotaryEncoder(ControlBoard.PIN_A,
                                            ControlBoard.PIN_B,
                                            ControlBoard.BUTTON,
                                            self.controlKnobEvent)

        print "ControlBoard._init__ > ready"
Exemple #6
0
def volume_event(event):
        global volumeknob, play_on
        if event == RotaryEncoder.CLOCKWISE:
                #print("Clockwise")
                # get initial state
                socketIO.emit('volume', '+')
        elif event == RotaryEncoder.ANTICLOCKWISE:
                #print("Anticlockwise")
                socketIO.emit('volume', '-')
        elif event == RotaryEncoder.BUTTONDOWN:
                #print("Button down: toggle pause/play")
                socketIO.emit('toggle')
        elif event == RotaryEncoder.BUTTONUP:
                #print("Button up")
        return

# Define the switch
volumeknob = RotaryEncoder(VOLUME_UP,VOLUME_DOWN,MUTE_SWITCH,volume_event,2)  # 2 = latest RPi Boards as of 2017

while True:
        time.sleep(0.5)
Exemple #7
0
	def getConfiguration(self):
		global left_switch,middle_switch,right_switch
		global encoder_a,encoder_b,encoder_switch

		# Button configuration
		self.left_switch = config.getLeftSwitch()
		self.middle_switch = config.getMiddleSwitch()
		self.right_switch = config.getRightSwitch()

		# Rotary encoder configuration
		self.encoder_switch = config.getEncoderSwitch()
		self.encoder_a = config.getEncoderA()
		self.encoder_b = config.getEncoderB()

		# Set up rotary encoder with rotary_event() as the callback routine
		self.rotaryknob = RotaryEncoder(self.encoder_a,self.encoder_b,
				self.encoder_switch,self.rotary_event)

		# Set up buttons
		Button(self.left_switch, self.button_event)
		Button(self.middle_switch, self.button_event)
		Button(self.right_switch, self.button_event)
Exemple #8
0
    def __init__(self):
        self.mpd = MPDClient()
        self.lcd = TextStar('/dev/serial0', 115200)
        self.lcd.quiet = True
        self.menu_rotary = RotaryEncoder(UP_SWITCH, DOWN_SWITCH, MENU_SWITCH,
                                         self.tuning_event, 2)

        self.connect()

        self.menus = ["Tuning", "Power", "Return"]
        self.current_menu = 0
        self.last_menu = len(self.menus)
        self.mainMenu = Menu(title='Menu', options=self.menus)

        self.playlist = ["Heart", "BBC Glos", "BBC R1"]

        self.tuningMenu = Menu(title='Tuning', options=self.playlist)

        self.powerMenu = Menu(title='Power', options=["Return", "Power Off"])

        self.mode = self.MODE_PLAY

        self.display()
Exemple #9
0
	elif event == RotaryEncoder.ANTICLOCKWISE:
		print name + " anticlockwise", RotaryEncoder.BUTTONDOWN
	elif event == RotaryEncoder.BUTTONDOWN:
		print name + " button down", RotaryEncoder.BUTTONDOWN
	elif event == RotaryEncoder.BUTTONUP:
		print name + " button up", RotaryEncoder.BUTTONUP
	return

# Allow user to select board revision
revision = 2
stderr("Are you using an old revision 1 board y/n: ")
answer = raw_input("")
if answer == 'y':
	revision = 1

volumeknob = RotaryEncoder(LEFT_SWITCH,RIGHT_SWITCH,MUTE_SWITCH,volume_event,revision)
tunerknob = RotaryEncoder(UP_SWITCH,DOWN_SWITCH,MENU_SWITCH,tuner_event,revision)

print "Use Ctl-C to exit"

while True:
	try:
		time.sleep(0.2)
	except KeyboardInterrupt:
		print "\nExit"
		sys.exit(0)




	left_switch = left_switch_40
	right_switch = right_switch_40
	up_switch = up_switch_40
	down_switch = down_switch_40
	mute_switch =  mute_switch_40
	stderr("40 pin wiring version selected\n")
else:
	stderr("26 pin wiring version selected\n")
	stderr("Are you using GPIO10 (pin 19) for the down switch y/n: ")
	answer = raw_input("")
	if answer == 'y':
		down_switch = dac_down_switch
		stderr("Down switch is set to GPIO" + str(dac_down_switch) + "\n")

# Set up rotary encoders
volumeknob = RotaryEncoder(left_switch,right_switch,mute_switch,volume_event,revision)
tunerknob = RotaryEncoder(down_switch,up_switch,menu_switch,tuner_event,revision)

print "Use Ctl-C to exit"

while True:
	try:
		time.sleep(0.1)
	except KeyboardInterrupt:
		print "\nExit"
		sys.exit(0)

# End of test rotary encoders


Exemple #11
0
    encoder_switch = config.getEncoderSwitch()
    encoder_a = config.getEncoderA()
    encoder_b = config.getEncoderB()

    print("Left switch GPIO {}".format(left_switch))
    print("Middle switch GPIO {}".format(middle_switch))
    print("Right switch GPIO {}".format(right_switch))
    print("Encoder A GPIO {}".format(encoder_a))
    print("Encoder B GPIO {}".format(encoder_b))
    print("Encoder switch GPIO {}".format(encoder_switch))

    Button(left_switch, button_event)
    Button(middle_switch, button_event)
    Button(right_switch, button_event)

    rotaryknob = RotaryEncoder(encoder_a, encoder_b, encoder_switch,
                               rotary_event)

    statusLed = statusLedInitialise(statusLed)
    statusLed.set(StatusLed.LED1, True)
    time.sleep(1)
    statusLed.clear()
    statusLed.set(StatusLed.LED2, True)
    time.sleep(1)
    statusLed.clear()
    statusLed.set(StatusLed.LED3, True)
    time.sleep(1)
    statusLed.clear()
    time.sleep(1)

    # Main wait loop
    try:
Exemple #12
0
    def run(self):
        global CurrentFile
        global menu_switch_value
        global volumeknob, tunerknob, statusLed, menu_switch

        menu_settle = 5  # Allow menu switch to settle

        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        # Configure RGB status LED
        rgb_red = radio.getRgbLed('rgb_red')
        rgb_green = radio.getRgbLed('rgb_green')
        rgb_blue = radio.getRgbLed('rgb_blue')
        statusLed = StatusLed(rgb_red, rgb_green, rgb_blue)
        statusLed.set(StatusLed.BUSY)

        progcall = str(sys.argv)

        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        hostname = exec_cmd('hostname')
        ipaddr = exec_cmd('hostname -I')

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())

        # Wait for the IP network
        ipaddr = ""
        waiting4network = True
        count = 10
        while waiting4network:
            ipaddr = exec_cmd('hostname -I')
            time.sleep(1)
            count -= 1
            if (count < 0) or (len(ipaddr) > 1):
                waiting4network = False

        if len(ipaddr) < 1:
            log.message("No IP network", log.INFO)
            statusLed.set(StatusLed.ERROR)
            time.sleep(1)
        else:
            log.message("IP " + ipaddr, log.INFO)

        time.sleep(2)
        log.message("Starting MPD", log.INFO)
        radio.start()
        log.message("MPD started", log.INFO)

        radio.setLastMode(radio.MODE_OPTIONS)
        radio.setLastOption(radio.RELOADLIB)
        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        time.sleep(1)

        # Auto-load music library if no Internet
        if len(ipaddr) < 1 and radio.autoload():
            log.message("Loading music library", log.INFO)
            radio.setSource(radio.PLAYER)

        # Load radio
        reload(radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)

        # Get rotary switches configuration
        up_switch = radio.getSwitchGpio("up_switch")
        down_switch = radio.getSwitchGpio("down_switch")
        left_switch = radio.getSwitchGpio("left_switch")
        right_switch = radio.getSwitchGpio("right_switch")
        menu_switch = radio.getSwitchGpio("menu_switch")
        mute_switch = radio.getSwitchGpio("mute_switch")

        boardrevision = radio.getBoardRevision()

        if radio.getRotaryClass() is radio.ROTARY_STANDARD:
            boardrevision = radio.getBoardRevision()
            volumeknob = RotaryEncoder(left_switch, right_switch, mute_switch,
                                       volume_event, boardrevision)
            tunerknob = RotaryEncoder(up_switch, down_switch, menu_switch,
                                      tuner_event, boardrevision)
        elif radio.getRotaryClass() is radio.ROTARY_ALTERNATIVE:
            volumeknob = RotaryEncoderAlternative(left_switch, right_switch,
                                                  mute_switch, volume_event,
                                                  boardrevision)
            tunerknob = RotaryEncoderAlternative(up_switch, down_switch,
                                                 menu_switch, tuner_event,
                                                 boardrevision)

        # Configure rotary switch (not rotary encoder)
        switch1 = radio.getMenuSwitch('menu_switch_value_1')
        switch2 = radio.getMenuSwitch('menu_switch_value_2')
        switch4 = radio.getMenuSwitch('menu_switch_value_4')
        menu_switch = MenuSwitch(switch1, switch2, switch4, menu_swich_event)

        log.message("Running", log.INFO)
        statusLed.set(StatusLed.NORMAL)

        # Main processing loop
        count = 0
        while True:
            display_mode = radio.getDisplayMode()
            if not radio.getReload():
                if display_mode is radio.MODE_SEARCH:
                    statusLed.set(StatusLed.SELECT)
                else:
                    statusLed.set(StatusLed.NORMAL)
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(radio, volumeknob, tunerknob)
                radio.setSwitch(0)

            if menu_switch_value > 0:
                menu_settle -= 1
                if menu_settle < 0:
                    setMenu(radio, menu_switch_value)
                    menu_settle = 5
                else:
                    time.sleep(0.2)

            dateFormat = radio.getDateFormat()
            todaysdate = strftime(dateFormat)

            # Check for IP address
            ipaddr = exec_cmd('hostname -I')
            if len(ipaddr) < 1 and radio.getSource() != radio.PLAYER:
                statusLed.set(StatusLed.ERROR)

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                while True:
                    # Not an error just shutting down
                    statusLed.set(StatusLed.ERROR)
                    radio.execCommand("shutdown -h now")
                    time.sleep(1)

            elif display_mode == radio.MODE_TIME:

                if radio.getReload():
                    log.message("Reload ", log.DEBUG)
                    reload(radio)
                    radio.setReload(False)

            # Check state (pause or play)
            checkState(radio)

            if radio.volumeChanged():
                time.sleep(0.1)

            time.sleep(0.1)
Exemple #13
0
    def run(self):
        global CurrentFile
        global volumeknob, tunerknob
        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)

        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        boardrevision = radio.getBoardRevision()
        lcd.init(boardrevision)
        lcd.backlight(True)

        hostname = exec_cmd('hostname')
        ipaddr = exec_cmd('hostname -I')
        myos = exec_cmd('uname -a')
        log.message(myos, log.INFO)

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())
        lcd.line1(message)
        lcd.line2("IP " + ipaddr)
        time.sleep(4)
        log.message("Starting MPD", log.INFO)
        lcd.line2("Starting MPD")
        radio.start()
        log.message("MPD started", log.INFO)

        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        lcd.line1("Radio ver " + radio.getVersion())
        lcd.scroll2(mpd_version, no_interrupt)
        time.sleep(1)

        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)

        # Define rotary switches
        volumeknob = RotaryEncoder(LEFT_SWITCH, RIGHT_SWITCH, MUTE_SWITCH,
                                   volume_event, boardrevision)
        tunerknob = RotaryEncoder(UP_SWITCH, DOWN_SWITCH, MENU_SWITCH,
                                  tuner_event, boardrevision)
        log.message("Running", log.INFO)

        # Main processing loop
        count = 0
        while True:
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, volumeknob, tunerknob)
                radio.setSwitch(0)

            display_mode = radio.getDisplayMode()
            lcd.setScrollSpeed(0.3)  # Scroll speed normal
            todaysdate = strftime("%H:%M %d/%m/%Y")
            ipaddr = exec_cmd('hostname -I')

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                displayShutdown(lcd)
                while True:
                    time.sleep(1)

            elif ipaddr is "":
                lcd.line2("No IP network")

            elif display_mode == radio.MODE_TIME:
                displayTime(lcd, radio)
                if radio.muted():
                    msg = "Sound muted"
                    if radio.getStreaming():
                        msg = msg + ' *'
                    lcd.line2(msg)
                else:
                    display_current(lcd, radio)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                lcd.line2("Radio v" + radio.getVersion())
                if ipaddr is "":
                    lcd.line1("No IP network")
                else:
                    lcd.scroll1("IP " + ipaddr, interrupt)

            elif display_mode == radio.MODE_RSS:
                lcd.line1(todaysdate)
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                lcd.line1(todaysdate)
                display_sleep(lcd, radio)

            # Timer function
            checkTimer(radio)

            # Check state (pause or play)
            checkState(radio)

            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                unmuteRadio(lcd, radio)
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            if radio.volumeChanged():
                lcd.line2("Volume " + str(radio.getVolume()))
                time.sleep(0.5)

            time.sleep(0.1)
Exemple #14
0
    def run(self):
        global CurrentFile
        global volumeknob, tunerknob
        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)
        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        boardrevision = radio.getBoardRevision()
        lcd.init(boardrevision)
        lcd.setWidth(20)
        lcd.line1("Radio version " + radio.getVersion())
        time.sleep(0.5)

        ipaddr = exec_cmd('hostname -I')
        myos = exec_cmd('uname -a')
        hostname = exec_cmd('hostname -s')
        log.message(myos, log.INFO)

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())
        lcd.line2(message)

        lcd.line3("Starting MPD")
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)
        lcd.line4("IP " + ipaddr)
        radio.start()
        log.message("MPD started", log.INFO)
        time.sleep(0.5)

        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        lcd.line3(mpd_version)
        lcd.line4("GPIO version " + str(GPIO.VERSION))
        time.sleep(2.0)

        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)
        lcd.line3("Radio Station " + str(radio.getCurrentID()))

        # Define rotary switches
        volumeknob = RotaryEncoder(LEFT_SWITCH, RIGHT_SWITCH, MUTE_SWITCH,
                                   volume_event, boardrevision)
        tunerknob = RotaryEncoder(UP_SWITCH, DOWN_SWITCH, MENU_SWITCH,
                                  tuner_event, boardrevision)
        log.message("Running", log.INFO)

        # Main processing loop
        count = 0
        toggleScrolling = True  # Toggle scrolling between Line 2 and 3
        while True:

            # See if we have had an interrupt
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, volumeknob, tunerknob)

            display_mode = radio.getDisplayMode()

            lcd.setScrollSpeed(0.3)  # Scroll speed normal
            todaysdate = strftime("%H:%M %d/%m/%Y")
            ipaddr = exec_cmd('hostname -I')

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                log.message("Shutting down", log.DEBUG)
                displayShutdown(lcd)
                while True:
                    time.sleep(1)

            if ipaddr is "":
                lcd.line3("No IP network")

            elif display_mode == radio.MODE_TIME:
                msg = todaysdate
                if radio.getStreaming():
                    msg = msg + ' *'
                lcd.line1(msg)
                display_current(lcd, radio, toggleScrolling)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                displayInfo(lcd, ipaddr, mpd_version)

            elif display_mode == radio.MODE_RSS:
                lcd.line1(todaysdate)
                input_source = radio.getSource()
                current_id = radio.getCurrentID()
                if input_source == radio.RADIO:
                    station = radio.getRadioStation() + ' (' + str(
                        current_id) + ')'
                    lcd.line2(station)
                else:
                    lcd.line2("Current track:" + str(current_id))
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                lcd.line1(todaysdate)
                display_sleep(lcd, radio)

            # Timer function
            checkTimer(radio)

            # Check state (pause or play)
            checkState(radio)

            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                radio.unmute()
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            # Toggle line 2 & 3 scrolling
            if toggleScrolling:
                toggleScrolling = False
            else:
                toggleScrolling = True

            time.sleep(0.1)
Exemple #15
0
def amplifier_on():
    GPIO.output(21, GPIO.HIGH)


def amplifier_off():
    GPIO.output(21, GPIO.LOW)


#Touch Schalter
GPIO.add_event_detect(24,
                      GPIO.FALLING,
                      callback=drehimpulsgeber,
                      bouncetime=1500)

knob = RotaryEncoder(6, 5, 27, drehimpulsgeber)
update_lcd(1)
dim_lcd(0)

while True:
    time.sleep(25)

    if mode == 0:
        mode = 5
        print("update Clock")
        Clock.write_big_time()
        time.sleep(0.3)
        mode = 0

    if Alarm.get_alarm_time()[0] == time.strftime("%H:%M"):
        enter_menue()
Exemple #16
0
    def run(self):
        global CurrentFile
        global volumeknob, tunerknob

        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)
        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        boardrevision = radio.getBoardRevision()
        lcd.init(boardrevision)

        # Set up LCD line width
        width = lcd.getWidth()
        if width > 0:
            lcd.setWidth(width)
        else:
            lcd.setWidth(20)

        lcd.scroll1("Radio version " + radio.getVersion(), nointerrupt)

        hostname = exec_cmd('hostname -s')

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())
        lcd.line2(message)

        # Wait for the IP network
        ipaddr = ""
        waiting4network = True
        count = 10
        while waiting4network:
            lcd.scroll4("Waiting for network", nointerrupt)
            ipaddr = exec_cmd('hostname -I')
            time.sleep(1)
            count -= 1
            if (count < 0) or (len(ipaddr) > 1):
                waiting4network = False

        if len(ipaddr) < 1:
            lcd.line4("No IP network")
        else:
            lcd.line4("IP " + ipaddr)
            log.message("IP " + ipaddr, log.INFO)

        lcd.line3("Starting MPD")
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)
        radio.start()
        log.message("MPD started", log.INFO)
        time.sleep(0.5)

        mpd_version = radio.getMpdVersion()
        log.message(mpd_version, log.INFO)
        lcd.scroll3("MPD version " + mpd_version, nointerrupt)
        lcd.scroll4("GPIO version " + str(GPIO.VERSION), nointerrupt)
        time.sleep(2.0)

        # Auto-load music library if no Internet
        if len(ipaddr) < 1 and radio.autoload():
            log.message("Loading music library", log.INFO)
            radio.setSource(radio.PLAYER)

        # Load radio
        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)
        lcd.line3("Radio Station " + str(radio.getCurrentID()))

        # Get rotary switches configuration
        up_switch = radio.getSwitchGpio("up_switch")
        down_switch = radio.getSwitchGpio("down_switch")
        left_switch = radio.getSwitchGpio("left_switch")
        right_switch = radio.getSwitchGpio("right_switch")
        menu_switch = radio.getSwitchGpio("menu_switch")
        mute_switch = radio.getSwitchGpio("mute_switch")
        aux_switch = radio.getSwitchGpio("aux_switch")

        if radio.getRotaryClass() is radio.ROTARY_STANDARD:
            volumeknob = RotaryEncoder(left_switch, right_switch, mute_switch,
                                       volume_event, boardrevision)
            tunerknob = RotaryEncoder(down_switch, up_switch, menu_switch,
                                      tuner_event, boardrevision)
        elif radio.getRotaryClass() is radio.ROTARY_ALTERNATIVE:
            volumeknob = RotaryEncoderAlternative(left_switch, right_switch,
                                                  mute_switch, volume_event,
                                                  boardrevision)
            tunerknob = RotaryEncoderAlternative(down_switch, up_switch,
                                                 menu_switch, tuner_event,
                                                 boardrevision)

        log.message("Running", log.INFO)

        # Main processing loop
        count = 0
        toggleScrolling = True  # Toggle scrolling between Line 2 and 3
        while True:

            # Check for audio device error
            if radio.audioError():
                lcd.line1("Audio Error!")
                lcd.line2("Aborting!")
                time.sleep(5)
                sys.exit(1)

            # See if we have had an interrupt
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, volumeknob, tunerknob)

            display_mode = radio.getDisplayMode()

            lcd.setScrollSpeed(0.3)  # Scroll speed normal
            dateFormat = radio.getDateFormat()
            todaysdate = strftime(dateFormat)

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                log.message("Shutting down", log.DEBUG)
                displayShutdown(lcd, radio)
                while True:
                    time.sleep(1)

            ipaddr = exec_cmd('hostname -I')
            source = radio.getSource()
            if len(ipaddr) < 1 and source != radio.PLAYER:
                lcd.line3("No IP network")

            elif display_mode == radio.MODE_TIME:
                if radio.getReload():
                    log.message("Reload ", log.DEBUG)
                    reload(lcd, radio)
                    radio.setReload(False)

                msg = todaysdate
                if radio.getStreaming():
                    msg = msg + ' *'
                lcd.line1(msg)
                if source == radio.AIRPLAY:
                    displayAirplay(lcd, radio)
                else:
                    display_current(lcd, radio, toggleScrolling)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                displayInfo(lcd, ipaddr, mpd_version)

            elif display_mode == radio.MODE_RSS:
                lcd.line1(todaysdate)
                input_source = radio.getSource()
                current_id = radio.getCurrentID()
                if input_source == radio.RADIO:
                    lcd.line2(radio.getRadioStation())
                elif input_source == radio.PLAYER:
                    lcd.line2("Current track:" + str(current_id))
                else:
                    displayVolume(lcd, radio)
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                lcd.line1(todaysdate)
                display_sleep(lcd, radio)

            # Timer function
            checkTimer(radio)

            # Check state (pause or play)
            checkState(radio)

            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                radio.unmute()
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            # Toggle line 2 & 3 scrolling
            if toggleScrolling:
                toggleScrolling = False
            else:
                toggleScrolling = True

            time.sleep(0.1)
Exemple #17
0
        up_switch = config.getSwitchGpio("up_switch")
        down_switch = config.getSwitchGpio("down_switch")
        menu_switch = config.getSwitchGpio("menu_switch")

    print("Left switch GPIO", left_switch)
    print("Right switch GPIO", right_switch)
    print("Up switch GPIO", up_switch)
    print("Down switch GPIO", down_switch)
    print("Mute switch GPIO", mute_switch)
    print("Menu switch GPIO", menu_switch)

    Button(down_switch, interrupt, log)
    Button(up_switch, interrupt, log)
    Button(menu_switch, interrupt, log)

    volumeknob = RotaryEncoder(left_switch, right_switch, mute_switch,
                               rotary_event)

    statusLed = statusLedInitialise(statusLed)
    statusLed.set(StatusLed.LED1)
    time.sleep(1)
    statusLed.set(StatusLed.LED2)
    time.sleep(1)
    statusLed.set(StatusLed.LED3)
    time.sleep(1)
    statusLed.set(StatusLed.ALL)
    time.sleep(1)
    statusLed.set(StatusLed.CLEAR)
    time.sleep(1)

    # Main wait loop
    try:
Exemple #18
0
    if data == "r3":
        trellis.color(x,y,WHITE)
    if data == "r4":
        trellis.color(x,y,YELLOW)

    if event == RotaryEncoder.CLOCKWISE:
        print("%s Clockwise" % data)
    elif event == RotaryEncoder.ANTICLOCKWISE:
        print ("%s Anticlockwise" % data)
    elif event == RotaryEncoder.BUTTONDOWN:
        print ("%s Button down" % data)
    elif event == RotaryEncoder.BUTTONUP:
        print ("%s Button up" % data)
    return

# Define the encoders
r1 = RotaryEncoder(R1_PIN_A,R1_PIN_B,R1_BUTTON,switch_event,"r1")
r2 = RotaryEncoder(R2_PIN_A,R2_PIN_B,R2_BUTTON,switch_event,"r2")
r3 = RotaryEncoder(R3_PIN_A,R3_PIN_B,R3_BUTTON,switch_event,"r3")
r4 = RotaryEncoder(R4_PIN_A,R4_PIN_B,R4_BUTTON,switch_event,"r4")

print("r1_pin_a=[%d], r1_pin_b=[%d], r1_button=[%d]," % (R1_PIN_A, R1_PIN_B, R1_BUTTON))
print("r2_pin_a=[%d], r2_pin_b=[%d], r2_button=[%d]," % (R2_PIN_A, R2_PIN_B, R2_BUTTON))
print("r3_pin_a=[%d], r3_pin_b=[%d], r3_button=[%d]," % (R3_PIN_A, R3_PIN_B, R3_BUTTON))
print("r4_pin_a=[%d], r4_pin_b=[%d], r4_button=[%d]," % (R4_PIN_A, R4_PIN_B, R4_BUTTON))

while True:
    # the trellis can only be read every 17 millisecons or so
    trellis.sync()
    time.sleep(0.02)
Exemple #19
0
    def run(self):
        global lcd
        global CurrentFile
        global volumeknob, tunerknob
        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)

        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        i2c_address = radio.getI2Caddress()  # Get address from configuration

        # Load pcf8574 i2c class or Adafruit backpack
        if radio.getBackPackType() == radio.PCF8574:
            log.message("PCF8574 backpack address: " + hex(i2c_address),
                        log.INFO)
            lcd = lcd_i2c_pcf8574()
        else:
            log.message("Adafruit backpack address: " + hex(i2c_address),
                        log.INFO)
            lcd = lcd_i2c()

        boardrevision = radio.getBoardRevision()
        lcd.init(board_rev=boardrevision, address=i2c_address)
        lcd.backlight(True)

        hostname = exec_cmd('hostname')
        ipaddr = exec_cmd('hostname -I')

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())
        lcd.line1(message)
        lcd.line2("IP " + ipaddr)
        time.sleep(4)
        log.message("Starting MPD", log.INFO)
        lcd.line2("Starting MPD")
        radio.start()
        log.message("MPD started", log.INFO)

        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        lcd.line1("Radio ver " + radio.getVersion())
        lcd.scroll2(mpd_version, no_interrupt)
        time.sleep(1)

        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)

        # Get rotary switches configuration
        up_switch = radio.getSwitchGpio("up_switch")
        down_switch = radio.getSwitchGpio("down_switch")
        left_switch = radio.getSwitchGpio("left_switch")
        right_switch = radio.getSwitchGpio("right_switch")
        menu_switch = radio.getSwitchGpio("menu_switch")
        mute_switch = radio.getSwitchGpio("mute_switch")

        if radio.getRotaryClass() is radio.ROTARY_STANDARD:
            volumeknob = RotaryEncoder(left_switch, right_switch, mute_switch,
                                       volume_event, boardrevision)
            tunerknob = RotaryEncoder(up_switch, down_switch, menu_switch,
                                      tuner_event, boardrevision)
        elif radio.getRotaryClass() is radio.ROTARY_ALTERNATIVE:
            volumeknob = RotaryEncoderAlternative(left_switch, right_switch,
                                                  mute_switch, volume_event,
                                                  boardrevision)
            tunerknob = RotaryEncoderAlternative(up_switch, down_switch,
                                                 menu_switch, tuner_event,
                                                 boardrevision)

        log.message("Running", log.INFO)

        # Main processing loop
        count = 0
        while True:
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, volumeknob, tunerknob)
                radio.setSwitch(0)

            display_mode = radio.getDisplayMode()
            lcd.setScrollSpeed(0.3)  # Scroll speed normal
            dateFormat = radio.getDateFormat()
            todaysdate = strftime(dateFormat)

            ipaddr = exec_cmd('hostname -I')

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                displayShutdown(lcd)
                while True:
                    time.sleep(1)

            elif ipaddr is "":
                lcd.line2("No IP network")

            elif display_mode == radio.MODE_TIME:
                if radio.getReload():
                    log.message("Reload ", log.DEBUG)
                    reload(lcd, radio)
                    radio.setReload(False)

                displayTime(lcd, radio)
                if radio.muted():
                    msg = "Sound muted"
                    if radio.getStreaming():
                        msg = msg + ' *'
                    lcd.line2(msg)
                else:
                    display_current(lcd, radio)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                lcd.line2("Radio v" + radio.getVersion())
                if ipaddr is "":
                    lcd.line1("No IP network")
                else:
                    lcd.scroll1("IP " + ipaddr, interrupt)

            elif display_mode == radio.MODE_RSS:
                lcd.line1(todaysdate)
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                lcd.line1(todaysdate)
                display_sleep(lcd, radio)

            # Timer function
            checkTimer(radio)

            # Check state (pause or play)
            checkState(radio)

            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                unmuteRadio(lcd, radio)
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            if radio.volumeChanged():
                lcd.line2("Volume " + str(radio.getVolume()))
                time.sleep(0.5)

            time.sleep(0.1)
Exemple #20
0
    return


def switch_event(event):
    if event == RotaryEncoder.CLOCKWISE:
        call(["mpc", "seek", "+2"], shell=False)
    elif event == RotaryEncoder.ANTICLOCKWISE:
        call(["mpc", "seek", "-2"], shell=False)
    elif event == RotaryEncoder.BUTTONDOWN:
        pass
    elif event == RotaryEncoder.BUTTONUP:
        call(["mpc", "next"], shell=False)
    return


# Define shutdown button
gpio.setmode(gpio.BCM)  # Set pin numbering to bcm numbering
gpio.setup(PUSH_BUTTON, gpio.IN,
           pull_up_down=gpio.PUD_UP)  # Set up pin 26 as an input
gpio.add_event_detect(
    PUSH_BUTTON, gpio.RISING, callback=shutdown,
    bouncetime=2000)  # Set up an interrupt to look for button presses

# Define the right switch
rswitch = RotaryEncoder(RPIN_A, RPIN_B, RBUTTON, vol_event)

# Define the left switch
lswitch = RotaryEncoder(LPIN_A, LPIN_B, LBUTTON, switch_event)

loop()  # Run the loop function to keep script running
Exemple #21
0
    def run(self):
        global CurrentFile
        global ConfigFile
        global tunerknob
        configOK = False

        GPIO.setmode(GPIO.BCM)  # Use BCM GPIO numbers
        GPIO.setwarnings(False)  # Ignore warnings

        boardrevision = radio.getBoardRevision()
        if boardrevision == 1:
            # For rev 1 boards with no inbuilt pull-up/down resistors
            # Wire the GPIO inputs to ground via a 10K resistor
            GPIO.setup(MENU_SWITCH, GPIO.IN)
            GPIO.setup(UP_SWITCH, GPIO.IN)
            GPIO.setup(DOWN_SWITCH, GPIO.IN)
            GPIO.setup(LEFT_SWITCH, GPIO.IN)
            GPIO.setup(RIGHT_SWITCH, GPIO.IN)
        else:
            # For rev 2 boards with inbuilt pull-up/down resistors the
            # following lines are used instead of the above, so
            # there is no need to physically wire the 10k resistors
            GPIO.setup(MENU_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.setup(UP_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.setup(DOWN_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.setup(LEFT_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.setup(RIGHT_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.setup(STREAM_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

        # Initialise IR
        if not (pylirc.init("radio", "/home/pi/radio/ir.conf")):
            log.message("IR initialization failed.", log.ERROR)
        else:
            pylirc.blocking(0)

        # Initialise radio
        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)
        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        if not os.path.isfile(ConfigFile) or os.path.getsize(ConfigFile) == 0:
            log.message("Missing configuration file " + ConfigFile, log.ERROR)
        else:
            log.message("Reading configuration file", log.INFO)
            self.configOK = True  # Must be set before calling getConfig()
            self.getConfig()

        lcd.init(boardrevision)
        lcd.setWidth(self.display_width)

        hostname = exec_cmd('hostname')
        ipaddr = exec_cmd('hostname -I')
        myos = exec_cmd('uname -a')
        log.message(myos, log.INFO)

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())
        lcd.line1(message)
        lcd.line2("IP " + ipaddr)
        time.sleep(2)
        log.message("Starting MPD", log.INFO)
        lcd.line2("Starting MPD")
        radio.start()
        log.message("MPD started", log.INFO)

        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        lcd.line1("Radio ver " + radio.getVersion())
        lcd.scroll2(mpd_version, no_interrupt)
        time.sleep(1)

        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)

        tunerknob = RotaryEncoder(ROTA_SWITCH, ROTB_SWITCH, PUSH_SWITCH,
                                  tuner_event, boardrevision)
        # Set up switch event processing
        GPIO.add_event_detect(MENU_SWITCH,
                              GPIO.RISING,
                              callback=switch_event,
                              bouncetime=200)
        GPIO.add_event_detect(LEFT_SWITCH,
                              GPIO.RISING,
                              callback=switch_event,
                              bouncetime=200)
        GPIO.add_event_detect(RIGHT_SWITCH,
                              GPIO.RISING,
                              callback=switch_event,
                              bouncetime=200)
        GPIO.add_event_detect(UP_SWITCH,
                              GPIO.RISING,
                              callback=switch_event,
                              bouncetime=200)
        GPIO.add_event_detect(DOWN_SWITCH,
                              GPIO.RISING,
                              callback=switch_event,
                              bouncetime=200)
        GPIO.add_event_detect(STREAM_SWITCH,
                              GPIO.RISING,
                              callback=switch_event,
                              bouncetime=200)

        # Main processing loop
        count = 0
        while True:
            #log.message("===BEGIN MAIN LOOP===",log.DEBUG)
            ir = pylirc.nextcode(1)
            if (ir):
                ir_event(ir, lcd, radio, rss, tunerknob)
                ir = []

            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, tunerknob)
                radio.setSwitch(0)
            #log.message("=== 0 ===",log.DEBUG)
            display_mode = radio.getDisplayMode()
            dateFormat = radio.getDateFormat()
            todaysdate = strftime(dateFormat)
            lcd.setScrollSpeed(0.3)  # Scroll speed normal
            ipaddr = exec_cmd('hostname -I')

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                displayShutdown(lcd)
                while True:
                    time.sleep(1)

            elif ipaddr is "":
                lcd.line2("No IP network")

            elif display_mode == radio.MODE_TIME:
                #log.message("=== MODE_TIME ===",log.DEBUG)
                displayTime(lcd, radio)
                if radio.muted():
                    msg = "Sound muted"
                    lcd.line2(msg)
                else:
                    display_current(lcd, radio)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                lcd.line2("Radio v" + radio.getVersion())
                if ipaddr is "":
                    lcd.line1("No IP network")
                else:
                    lcd.scroll1("IP " + ipaddr, interrupt)

            elif display_mode == radio.MODE_RSS:
                displayTime(lcd, radio)
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                displayTime(lcd, radio)
                display_sleep(lcd, radio)
            #log.message("=== 1 ===",log.DEBUG)
            # Timer function
            checkTimer(radio)
            #log.message("=== 2 ===",log.DEBUG)
            # Check state (pause or play)
            checkState(radio)
            #log.message("=== 3 ===",log.DEBUG)
            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                unmuteRadio(lcd, radio)
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            if radio.volumeChanged():
                lcd.line2("Volume " + str(radio.getVolume()))
                time.sleep(0.75)
            #log.message("=== 4 ===",log.DEBUG)
            time.sleep(0.1)
	handle_event(event,"Left knob")	
	return

# This is the event callback routine to handle right knob events
def right_knob_event(event):
	handle_event(event,"Right knob")	
	return

# This is the event callback routine to handle events
def handle_event(event, name):
	if event == RotaryEncoder.CLOCKWISE:
		print name, "Clockwise event =", RotaryEncoder.CLOCKWISE
	elif event == RotaryEncoder.ANTICLOCKWISE:
		print name, "Anticlockwise event =", RotaryEncoder.BUTTONDOWN
	elif event == RotaryEncoder.BUTTONDOWN:
		print name, "Button down event =", RotaryEncoder.BUTTONDOWN
	elif event == RotaryEncoder.BUTTONUP:
		print name, "Button up event =", RotaryEncoder.BUTTONUP
	return

# Define the left and right knobs
leftknob = RotaryEncoder(LEFT_A,LEFT_B,LEFT_BUTTON,left_knob_event)
rightknob = RotaryEncoder(RIGHT_A,RIGHT_B,RIGHT_BUTTON,right_knob_event)

# Wait for events
while True:
	time.sleep(0.5)

# End of program

Exemple #23
0
		#print t,"Clockwise"
		camera.annotate_text = camera.annotate_text + ">"
	elif event == RotaryEncoder.ANTICLOCKWISE:
		#print t,"Anticlockwise"
		camera.annotate_text = camera.annotate_text + "<"
	elif event == RotaryEncoder.BUTTONDOWN:
		print t,"Button down"
	elif event == RotaryEncoder.BUTTONUP:
		print t,"Button up"
		in_menu = True
		camera.annotate_text = camera.annotate_text + " [menu]"

	return

# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)

print "Rotary encoder pins: %d %d switch: %d" % ( PIN_A, PIN_B, BUTTON )
last_value = 0
frame_nr = 1

camera.annotate_text = state
print state

zoom_axis = 0
crop_axis = 0

ssocr_val = { 'Rotate': 0, 'Threshold': 90, 'Digits': -1, 'Shear': 0, 'Crop': [ 0,0,1,1 ] }
ssocr_max = { 'Rotate': 360, 'Threshold': 100, 'Digits': 10, 'Shear':100, 'Brightness':100 }
ssocr_val['Brightness'] = camera.brightness
for k in ssocr_val:
Exemple #24
0
    def __init__(self, pause, play, togglePlayPause, toggleNext, togglePrev,
                 toggleUnjoin, toggleVolUp, toggleVolDown, toggleShuffle,
                 rightRotaryTurn, leftRotaryTurn, rotaryTouch):
        self.rightRotaryTurn = rightRotaryTurn
        self.leftRotaryTurn = leftRotaryTurn
        self.rotaryTouch = rotaryTouch
        self.lastTagUid = None
        # set all gpios
        if not self.BUTTON_PAUSE == self.NOGPIO:
            GPIO.setup(self.BUTTON_PAUSE, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PAUSE,
                                  GPIO.RISING,
                                  callback=pause,
                                  bouncetime=200)

        if not self.BUTTON_PLAY == self.NOGPIO:
            GPIO.setup(self.BUTTON_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PLAY,
                                  GPIO.RISING,
                                  callback=play,
                                  bouncetime=200)

        if not self.BUTTON_PLAYPAUSE == self.NOGPIO:
            GPIO.setup(self.BUTTON_PLAYPAUSE,
                       GPIO.IN,
                       pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PLAYPAUSE,
                                  GPIO.RISING,
                                  callback=togglePlayPause,
                                  bouncetime=800)

        if not self.BUTTON_NEXT == self.NOGPIO:
            GPIO.setup(self.BUTTON_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_NEXT,
                                  GPIO.RISING,
                                  callback=toggleNext,
                                  bouncetime=200)

        if not self.BUTTON_PREV == self.NOGPIO:
            GPIO.setup(self.BUTTON_PREV, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PREV,
                                  GPIO.RISING,
                                  callback=togglePrev,
                                  bouncetime=200)

        if not self.BUTTON_UNJOIN == self.NOGPIO:
            GPIO.setup(self.BUTTON_UNJOIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_UNJOIN,
                                  GPIO.RISING,
                                  callback=toggleUnjoin,
                                  bouncetime=200)

        if not self.BUTTON_VOL_UP == self.NOGPIO:
            GPIO.setup(self.BUTTON_VOL_UP, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_VOL_UP,
                                  GPIO.RISING,
                                  callback=toggleVolUp,
                                  bouncetime=200)

        if not self.BUTTON_VOL_DOWN == self.NOGPIO:
            GPIO.setup(self.BUTTON_VOL_DOWN,
                       GPIO.IN,
                       pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_VOL_DOWN,
                                  GPIO.RISING,
                                  callback=toggleVolDown,
                                  bouncetime=200)

        if not self.BUTTON_SHUFFLE == self.NOGPIO:
            GPIO.setup(self.BUTTON_SHUFFLE,
                       GPIO.IN,
                       pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_SHUFFLE,
                                  GPIO.RISING,
                                  callback=toggleShuffle,
                                  bouncetime=200)

        if self.ROTARY_1 != self.NOGPIO & self.ROTARY_2 != self.NOGPIO:
            MusicLogging.Instance().info("Rotary found")
            encoder = RotaryEncoder(self.ROTARY_1, self.ROTARY_2,
                                    self.rotaryEventHandler)
            if self.ROTARY_BUTTON != self.NOGPIO:
                encoder.setupButton(self.ROTARY_BUTTON)

        # To show that everythin is startet: pulse the led for 5 seconds:
        if self.LED1_BCM != self.NOGPIO:
            self.ledPulse = LedPulse(self.LED1_BCM)
            self.ledPulse.startPulseLedForSeconds(10)
            # ledPulse.startPulseLed()

        MusicLogging.Instance().info("Setting up reader...")
        # Create an object of the class MFRC522
        self.reader = MFRC522.MFRC522()

        # Welcome message
        MusicLogging.Instance().info("Welcome to the MFRC522 reader")

        self.lastTagUid = 'stop'
        self.oldStatus = 0
Exemple #25
0
    def run(self):
        global lcd
        global CurrentFile
        global volumeknob, tunerknob
        log.init('radio')

        # Setup signal handlers
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)
        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        i2c_address = radio.getI2Caddress()

        # Load pcf8574 i2c class or Adafruit backpack
        if radio.getBackPackType() == radio.PCF8574:
            log.message("PCF8574 backpack, address: " + hex(i2c_address),
                        log.INFO)
            lcd = lcd_i2c_pcf8574()
        else:
            log.message("Adafruit backpack, address: " + hex(i2c_address),
                        log.INFO)
            lcd = lcd_i2c()

        boardrevision = radio.getBoardRevision()
        lcd.init(board_rev=boardrevision, address=i2c_address)
        lcd.backlight(True)

        # Set up LCD line width
        width = lcd.getWidth()
        if width > 0:
            lcd.setWidth(width)
        else:
            lcd.setWidth(20)

        lcd.line1("Radio vers " + radio.getVersion())
        time.sleep(0.5)

        ipaddr = exec_cmd('hostname -I')
        hostname = exec_cmd('hostname -s')

        # Display daemon pid on the LCD
        message = "Radio pid " + str(os.getpid())
        lcd.line2(message)

        lcd.line3("Starting MPD")
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)
        lcd.line4("IP " + ipaddr)
        radio.start()
        log.message("MPD started", log.INFO)
        time.sleep(0.5)

        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        lcd.scroll3(mpd_version, no_interrupt)
        lcd.scroll4("GPIO version " + str(GPIO.VERSION), no_interrupt)
        time.sleep(2.0)

        # Auto-load music library if no Internet
        if len(ipaddr) < 1 and radio.autoload():
            log.message("Loading music library", log.INFO)
            radio.setSource(radio.PLAYER)

        # Load radio
        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)
        lcd.line3("Radio Station " + str(radio.getCurrentID()))

        # Get rotary switches configuration
        up_switch = radio.getSwitchGpio("up_switch")
        down_switch = radio.getSwitchGpio("down_switch")
        left_switch = radio.getSwitchGpio("left_switch")
        right_switch = radio.getSwitchGpio("right_switch")
        menu_switch = radio.getSwitchGpio("menu_switch")
        mute_switch = radio.getSwitchGpio("mute_switch")

        if radio.getRotaryClass() is radio.ROTARY_STANDARD:
            volumeknob = RotaryEncoder(left_switch, right_switch, mute_switch,
                                       volume_event, boardrevision)
            tunerknob = RotaryEncoder(down_switch, up_switch, menu_switch,
                                      tuner_event, boardrevision)
        elif radio.getRotaryClass() is radio.ROTARY_ALTERNATIVE:
            volumeknob = RotaryEncoderAlternative(left_switch, right_switch,
                                                  mute_switch, volume_event,
                                                  boardrevision)

        log.message("Running", log.INFO)

        # Main processing loop
        count = 0
        toggleScrolling = True  # Toggle scrolling between Line 2 and 3
        while True:

            # See if we have had an interrupt
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, volumeknob, tunerknob)

            display_mode = radio.getDisplayMode()

            lcd.setScrollSpeed(0.3)  # Scroll speed normal
            dateFormat = radio.getDateFormat()
            todaysdate = strftime(dateFormat)

            ipaddr = exec_cmd('hostname -I')

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                log.message("Shutting down", log.DEBUG)
                displayShutdown(lcd)
                while True:
                    time.sleep(1)

            if ipaddr is "":
                lcd.line3("No IP network")

            elif display_mode == radio.MODE_TIME:

                if radio.getReload():
                    log.message("Reload ", log.DEBUG)
                    reload(lcd, radio)
                    radio.setReload(False)

                msg = todaysdate
                if radio.getStreaming():
                    msg = msg + ' *'
                lcd.line1(msg)
                display_current(lcd, radio, toggleScrolling)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                displayInfo(lcd, ipaddr, mpd_version)

            elif display_mode == radio.MODE_RSS:
                lcd.line1(todaysdate)
                input_source = radio.getSource()
                current_id = radio.getCurrentID()
                if input_source == radio.RADIO:
                    station = radio.getRadioStation() + ' (' + str(
                        current_id) + ')'
                    lcd.line2(station)
                else:
                    lcd.line2("Current track:" + str(current_id))
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                lcd.line1(todaysdate)
                display_sleep(lcd, radio)

            # Timer function
            checkTimer(radio)

            # Check state (pause or play)
            checkState(radio)

            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                radio.unmute()
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            # Toggle line 2 & 3 scrolling
            if toggleScrolling:
                toggleScrolling = False
            else:
                toggleScrolling = True

            time.sleep(0.1)
Exemple #26
0
# This is the event callback routine to handle events
def switch_event(event):
    if event == RotaryEncoder.CLOCKWISE:
        tmp = ""
        subprocess.call(['mpc', 'volume', '+2'])
        time.sleep(.2)
    elif event == RotaryEncoder.ANTICLOCKWISE:
        tmp = ""
        subprocess.call(['mpc', 'volume', '-2'])
        time.sleep(.2)
    return


# Define the switch
rswitch = RotaryEncoder(PIN_A, PIN_B, BUTTON, switch_event)
#----------------------------------------------------------
#
#code to manage BUTTONS previous,next,stop,play,shutdown
GPIO.setup(SW_PREV, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_STOP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_SHTDWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_REB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
    try:
        prev_switch = GPIO.input(SW_PREV)
        next_switch = GPIO.input(SW_NEXT)
        stop_switch = GPIO.input(SW_STOP)
        shtdwn_switch = GPIO.input(SW_SHTDWN)
Exemple #27
0

# This is the event callback routine to handle right knob events
def vol_knob_event(event):
    handle_event(event, "Volume knob")
    return


# This is the event callback routine to handle events
def handle_event(event, name):
    if event == RotaryEncoder.CLOCKWISE:
        print(name, "Clockwise event =", RotaryEncoder.CLOCKWISE)
    elif event == RotaryEncoder.ANTICLOCKWISE:
        print(name, "Anticlockwise event =", RotaryEncoder.BUTTONDOWN)
    elif event == RotaryEncoder.BUTTONDOWN:
        print(name, "Button down event =", RotaryEncoder.BUTTONDOWN)
    elif event == RotaryEncoder.BUTTONUP:
        print(name, "Button up event =", RotaryEncoder.BUTTONUP)
    return


# Define the left and right knobs
tuneknob = RotaryEncoder(TUNE_A, TUNE_B, TUNE_BUTTON, tune_knob_event)
volknob = RotaryEncoder(VOLUME_A, VOLUME_B, VOLUME_BUTTON, vol_knob_event)

# Wait for events
while True:
    time.sleep(0.5)

# End of program
Exemple #28
0
    def __init__(self, pause, play, togglePlayPause, toggleNext, togglePrev, toggleUnjoin, toggleVolUp,
                 toggleVolDown, toggleShuffle, rightRotaryTurn, leftRotaryTurn, rotaryTouch):
        self.rightRotaryTurn = rightRotaryTurn
        self.leftRotaryTurn = leftRotaryTurn
        self.rotaryTouch = rotaryTouch
        self.lastTagUid = None
        # set all gpios
        if not self.BUTTON_PAUSE == self.NOGPIO:
            GPIO.setup(self.BUTTON_PAUSE, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PAUSE, GPIO.RISING, callback=pause, bouncetime=200)

        if not self.BUTTON_PLAY == self.NOGPIO:
            GPIO.setup(self.BUTTON_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PLAY, GPIO.RISING, callback=play, bouncetime=200)

        if not self.BUTTON_PLAYPAUSE == self.NOGPIO:
            GPIO.setup(self.BUTTON_PLAYPAUSE, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PLAYPAUSE, GPIO.RISING, callback=togglePlayPause, bouncetime=800)

        if not self.BUTTON_NEXT == self.NOGPIO:
            GPIO.setup(self.BUTTON_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_NEXT, GPIO.RISING, callback=toggleNext, bouncetime=200)

        if not self.BUTTON_PREV == self.NOGPIO:
            GPIO.setup(self.BUTTON_PREV, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_PREV, GPIO.RISING, callback=togglePrev, bouncetime=200)

        if not self.BUTTON_UNJOIN == self.NOGPIO:
            GPIO.setup(self.BUTTON_UNJOIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_UNJOIN, GPIO.RISING, callback=toggleUnjoin, bouncetime=200)

        if not self.BUTTON_VOL_UP == self.NOGPIO:
            GPIO.setup(self.BUTTON_VOL_UP, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_VOL_UP, GPIO.RISING, callback=toggleVolUp, bouncetime=200)

        if not self.BUTTON_VOL_DOWN == self.NOGPIO:
            GPIO.setup(self.BUTTON_VOL_DOWN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_VOL_DOWN, GPIO.RISING, callback=toggleVolDown, bouncetime=200)

        if not self.BUTTON_SHUFFLE == self.NOGPIO:
            GPIO.setup(self.BUTTON_SHUFFLE, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            GPIO.add_event_detect(self.BUTTON_SHUFFLE, GPIO.RISING, callback=toggleShuffle, bouncetime=200)

        if self.ROTARY_1 != self.NOGPIO & self.ROTARY_2 != self.NOGPIO:
            MusicLogging.Instance().info("Rotary found")
            encoder = RotaryEncoder(self.ROTARY_1, self.ROTARY_2, self.rotaryEventHandler)
            if self.ROTARY_BUTTON != self.NOGPIO:
                encoder.setupButton(self.ROTARY_BUTTON)

        # To show that everythin is startet: pulse the led for 5 seconds:
        if self.LED1_BCM != self.NOGPIO:
            self.ledPulse = LedPulse(self.LED1_BCM)
            self.ledPulse.startPulseLedForSeconds(10)
            # ledPulse.startPulseLed()


        MusicLogging.Instance().info("Setting up reader...")
        # Create an object of the class MFRC522
        self.reader = MFRC522.MFRC522()

        # Welcome message
        MusicLogging.Instance().info("Welcome to the MFRC522 reader")

        self.lastTagUid = 'stop'
        self.oldStatus = 0
Exemple #29
0
    def run(self):
        global CurrentFile
        global volumeknob, tunerknob
        log.init('radio')
        signal.signal(signal.SIGTERM, signalHandler)

        progcall = str(sys.argv)

        log.message('Radio running pid ' + str(os.getpid()), log.INFO)
        log.message(
            "Radio " + progcall + " daemon version " + radio.getVersion(),
            log.INFO)
        log.message("GPIO version " + str(GPIO.VERSION), log.INFO)

        boardrevision = radio.getBoardRevision()
        lcd.init(boardrevision)
        lcd.setWidth(8)

        hostname = exec_cmd('hostname')
        ipaddr = exec_cmd('hostname -I')

        # Display daemon pid on the LCD
        message = "PID " + str(os.getpid())
        lcd.line1(message)

        # Wait for the IP network
        ipaddr = ""
        waiting4network = True
        count = 10
        while waiting4network:
            lcd.line2("Connect")
            ipaddr = exec_cmd('hostname -I')
            time.sleep(1)
            count -= 1
            if (count < 0) or (len(ipaddr) > 1):
                waiting4network = False

        if len(ipaddr) < 1:
            lcd.scroll1("No IP network")
        else:
            lcd.scroll2("IP " + ipaddr, no_interrupt)

        time.sleep(2)
        log.message("Starting MPD", log.INFO)
        lcd.line1("Starting")
        lcd.line2("MPD")
        radio.start()
        log.message("MPD started", log.INFO)

        mpd_version = radio.execMpcCommand("version")
        log.message(mpd_version, log.INFO)
        lcd.line1("Ver " + radio.getVersion())
        lcd.scroll2(mpd_version, no_interrupt)
        time.sleep(1)

        # Auto-load music library if no Internet
        if len(ipaddr) < 1 and radio.autoload():
            log.message("Loading music library", log.INFO)
            radio.setSource(radio.PLAYER)

        # Load radio
        reload(lcd, radio)
        radio.play(get_stored_id(CurrentFile))
        log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)

        # Get switches configuration
        up_switch = radio.getSwitchGpio("up_switch")
        down_switch = radio.getSwitchGpio("down_switch")
        left_switch = radio.getSwitchGpio("left_switch")
        right_switch = radio.getSwitchGpio("right_switch")
        menu_switch = radio.getSwitchGpio("menu_switch")
        mute_switch = radio.getSwitchGpio("mute_switch")

        if radio.getRotaryClass() is radio.ROTARY_STANDARD:
            volumeknob = RotaryEncoder(left_switch, right_switch, mute_switch,
                                       volume_event, boardrevision)
            tunerknob = RotaryEncoder(down_switch, up_switch, menu_switch,
                                      tuner_event, boardrevision)
        elif radio.getRotaryClass() is radio.ROTARY_ALTERNATIVE:
            volumeknob = RotaryEncoderAlternative(left_switch, right_switch,
                                                  mute_switch, volume_event,
                                                  boardrevision)
            tunerknob = RotaryEncoderAlternative(down_switch, up_switch,
                                                 menu_switch, tuner_event,
                                                 boardrevision)

        log.message("Running", log.INFO)

        # Main processing loop
        count = 0
        while True:
            switch = radio.getSwitch()
            if switch > 0:
                get_switch_states(lcd, radio, rss, volumeknob, tunerknob)
                radio.setSwitch(0)

            display_mode = radio.getDisplayMode()
            lcd.setScrollSpeed(0.3)  # Scroll speed normal

            ipaddr = exec_cmd('hostname -I')

            # Shutdown command issued
            if display_mode == radio.MODE_SHUTDOWN:
                displayShutdown(lcd)
                while True:
                    time.sleep(1)

            if len(ipaddr) < 1 and radio.getSource() != radio.PLAYER:
                lcd.line2("No IP")

            elif display_mode == radio.MODE_TIME:

                if radio.getReload():
                    log.message("Reload ", log.DEBUG)
                    reload(lcd, radio)
                    radio.setReload(False)

                displayTime(lcd, radio)
                if radio.muted():
                    msg = "Muted"
                    if radio.getStreaming():
                        msg = msg + ' *'
                    lcd.line2(msg)
                else:
                    display_current(lcd, radio)

            elif display_mode == radio.MODE_SEARCH:
                display_search(lcd, radio)

            elif display_mode == radio.MODE_SOURCE:
                display_source_select(lcd, radio)

            elif display_mode == radio.MODE_OPTIONS:
                display_options(lcd, radio)

            elif display_mode == radio.MODE_IP:
                lcd.line2("Ver " + radio.getVersion())
                if len(ipaddr) < 1:
                    lcd.scroll1("No IP network", interrupt)
                else:
                    lcd.scroll1("IP " + ipaddr, interrupt)

            elif display_mode == radio.MODE_RSS:
                displayTime(lcd, radio)
                display_rss(lcd, rss)

            elif display_mode == radio.MODE_SLEEP:
                displayTime(lcd, radio)
                display_sleep(lcd, radio)

            # Timer function
            checkTimer(radio)

            # Check state (pause or play)
            checkState(radio)

            # Alarm wakeup function
            if display_mode == radio.MODE_SLEEP and radio.alarmFired():
                log.message("Alarm fired", log.INFO)
                unmuteRadio(lcd, radio)
                displayWakeUpMessage(lcd)
                radio.setDisplayMode(radio.MODE_TIME)

            if radio.volumeChanged():
                displayVolume(lcd, radio)

            time.sleep(0.1)