示例#1
0
文件: main.py 项目: czenker/60s-radio
def thread_exception_handler(args):
    theLog = str(args.exc_type)
    if args.exc_value:
        theLog += ": " + str(args.exc_value)
    elog(theLog)
    if args.thread and args.thread.is_alive():
        elog("Thread {} crashed.".format(args.thread.name))
示例#2
0
def parseSerial(line):
    currentDict = {}
    try:
        line = line.decode("utf-8").strip()
        segs = line.split("\t")
    except UnicodeDecodeError as e:
        # this typically happens when connection is started in the middle of a message
        elog(e.reason)
        return currentDict

    try:
        for seg in segs:
            [key, val] = seg.split(":")
            key = key.strip()
            val = val.strip()
            if key == "Cap":
                val = float(val)
            else:
                val = int(val)
            currentDict[key] = val
    except ValueError as e:
        elog("ValueError: {}".format(line))

    return currentDict
示例#3
0

lastValues = []


def thread_run():
    global lastValues
    while True:
        value = getValue()

        # keep track of last values to give the feel that the light is actually a coil-based light.
        lastValues = lastValues[0:3]
        lastValues.insert(0, value)
        value = sum(lastValues) / len(lastValues)

        value *= value  # adapt for non-linear dim of LEDs
        led.value = value
        time.sleep(1 / 25)


try:
    led = gpiozero.PWMLED(myUtil.ledPin)

    thread = threading.Thread(target=thread_run, daemon=True)
    thread.name = "led"
    thread.start()
except gpiozero.exc.BadPinFactory as e:
    elog(
        "LED GPIO could not be initialized. You are probably not running on a RasPi, so we disable the LED feature."
    )
示例#4
0
def thread_run():
    global currentDict
    global lastCaps
    global currentFreq
    global currentRadio
    global currentTuneFactor
    global serialObj

    modeDebounce = 0

    while True:
        if not serialObj or not serialObj.is_open:
            serialObj = serial.Serial()
            serialObj.port = serialPort
            serialObj.baudrate = serialBaud
            try:
                serialObj.open()
                slog("Connected to Arduino on {}".format(serialPort))
            except serial.SerialException as e:
                elog(e)
                time.sleep(2)
        else:
            try:
                line = serialObj.readline()
                currentDict = parseSerial(line)
                # log(currentDict)
            except serial.SerialException as e:
                serialObj.close(
                )  # close so that Linux can use the same /dev/ttyUSB*
                elog(e)
                time.sleep(2)

            if "On" in currentDict and "LW" in currentDict and "MW" in currentDict and "KW" in currentDict and "UKW" in currentDict and "Vol" in currentDict and "Tre" in currentDict and "Cap" in currentDict:
                # if valid data

                # check how many band selectors are active
                mode = currentDict["LW"] + currentDict["MW"] + currentDict[
                    "KW"] + currentDict["UKW"]
                if mode == 1:
                    # normal mode
                    maxTuneFactor = 0

                    # iron out spikes in cap values
                    lastCaps = lastCaps[0:4]
                    lastCaps.insert(0, currentDict["Cap"])
                    currentDict["Cap"] = statistics.median(lastCaps)

                    currentFreq = capToFreq(currentDict)
                    isOn = currentDict["On"] == 1
                    vol = currentDict["Vol"] * 100 / 255 if isOn else 0
                    staticVol = vol
                    for radio in myRadios.getRadios():
                        tuneFactor = radio.tuneFactor(currentFreq)
                        maxTuneFactor = max(maxTuneFactor, tuneFactor)
                        # cross-over noise works as follows:
                        if tuneFactor == 0:
                            # full noise. no signal
                            radio.off()
                            staticVol = staticVol
                        else:
                            currentRadio = radio
                            if tuneFactor <= 0.5:
                                # full noise with a little bit of signal
                                myVol = tuneFactor * 2 * vol
                                staticVol = staticVol
                            elif tuneFactor < 1:
                                # full signal with a little bit of noise
                                myVol = vol
                                staticVol = (2 * (1 - tuneFactor)) * staticVol
                            else:
                                # full signal. no noise
                                myVol = vol
                                staticVol = 0
                            radio.setVolume(myVol)

                    myNoise.setVolume(staticVol)
                    currentTuneFactor = maxTuneFactor
                elif mode == 0:
                    # if no channel is selected

                    # @TODO: maybe future use to calibrate the tuner or something
                    myNoise.setVolume(0)
                    if currentRadio != None:
                        currentRadio.off()
                    currentFreq = None
                    currentRadio = None
                    currentTuneFactor = None
                if mode == 2:
                    # if: two buttons are pressed
                    modeDebounce += 1
                    if modeDebounce == 4 and currentRadio:
                        currentRadio.next()
                else:
                    modeDebounce = 0
示例#5
0
文件: main.py 项目: czenker/60s-radio
                            format(idx))
                    equalizerName = radioConfig["equalizer"]
                else:
                    equalizerName = None

                radio = RadioStation(name,
                                     uri,
                                     frequency,
                                     stop=stop,
                                     pause=pause,
                                     shuffle=shuffle,
                                     start_random=start_random,
                                     equalizerName=equalizerName)
                radios.append(radio)
            except ValueError as e:
                elog("Error with radio at index {}: {}".format(idx, e))

        myRadios.setRadios(radios)

    except yaml.YAMLError as e:
        elog(e)
    except ValueError as e:
        elog("Error parsing {}: {}".format(args.confFile, e))

# ####
#
# Work
#
# ####

# everything runs in sub-threads. So, go take a break, drink coffee or do whatever parents do when their kids are occupied.