示例#1
0
class Display(object):

    segment = SevenSegment(address=0x70)

    def showNumber(self, number, menu):
        print number, " menu: ", menu
        fourth = int(number % 1000) / 1000
        third = int(number % 1000) / 100
        second = int(number % 100) / 10
        first = number % 10
        self.segment.writeDigit(0, fourth)
        self.segment.writeDigit(1, third, menu == 2)
        self.segment.writeDigit(3, second)
        self.segment.writeDigit(4, first)

    def showTime(self, hour, minute, alarm, menu):
        self.segment.writeDigit(0, int(hour / 10), menu == 1)
        self.segment.writeDigit(1, hour % 10)
        self.segment.writeDigit(3, int(minute / 10))
        self.segment.writeDigit(4, minute % 10, alarm)

    def blink(self, second):
        self.segment.setColon(second % 2)

    def showMenu(self, number):
        if number == 0:
            print "0 Uhrzeit"
        elif number == 1:
            print "1 Weckzeit"
        elif number == 2:
            print "2 Lautstärke"

    def showAlarm(self, alarm):
        print "Alarm:", alarm
        self.segment.writeDot(4, alarm)
示例#2
0
 def __init__(self, settings):
     threading.Thread.__init__(self)
     self.segment = SevenSegment(address=0x70)
     self.stopping = False
     # self.settings = Settings.Settings()
     self.settings = settings
     self.colon = 0
示例#3
0
    display.writeDigit(3, int(temp / 10), decimalPoint)  # Tens

    #	Set the second digit of the decimal portion
    digitCount += 1
    decimalPoint = ((digitCount + 1) == decimal)

    display.writeDigit(4, temp % 10, decimalPoint)  # Ones


#
# ======================================================================================================
# Clock using the DS1307 RTC, 7-segment and matrix 8x8 displays, and 10DOF IMU for temperature readings
# ======================================================================================================
#
#	Yellow 4 digit 7-Segment Display at address 0x70
sevenSeg = SevenSegment(address=0x70)

#	Yellow Matrix 8x8 Display at address 0x71
matrix8x8 = EightByEight(address=0x71)

#	Bi-Color Matrix 8x8 Display at address 0x73
#bicolor8x8 = ColorEightByEight(address=0x73)

matrix8x8.setRotation(3)
#bicolor8x8.setRotation(3)

bmp180 = BMP180(address=0x77)

print "Press CTRL+C to exit"

#
示例#4
0
class Pomodoro:

    # Button to start / pause the timer
    BUTTON_START = 18

    # Button to reset everything
    BUTTON_RESET = 25

    # LED pins that show the number of completed Pomodoros
    LED_1 = 23
    LED_2 = 17
    LED_3 = 22
    LED_4 = 4

    # Two states: running or paused
    STATUS_RUNNING = 0
    STATUS_STANDBY = 1

    POMODORO_LENGTH = 1500  # 25 minutes

    # State variables
    current_status = STATUS_STANDBY
    elapsed_seconds = 0
    pomodoros_completed = 0

    # 7-segment display (run i2cdetect -y 1 to get the addr)
    segment = SevenSegment(address=0x70)

    def __init__(self):
        self.output("Running the program - press Ctrl + C to exit.")

        GPIO.setmode(GPIO.BCM)

        GPIO.setup(self.BUTTON_START, GPIO.IN)
        GPIO.setup(self.BUTTON_RESET, GPIO.IN)

        GPIO.setup(self.LED_1, GPIO.OUT)
        GPIO.setup(self.LED_2, GPIO.OUT)
        GPIO.setup(self.LED_3, GPIO.OUT)
        GPIO.setup(self.LED_4, GPIO.OUT)

        self.clear_LEDs()
        self.output("Initialized. Press START/PAUSE to begin.")

    def is_button_pressed(self, pin_number):
        return GPIO.input(pin_number)

    def clear_LEDs(self):
        GPIO.output(self.LED_1, False)
        GPIO.output(self.LED_2, False)
        GPIO.output(self.LED_3, False)
        GPIO.output(self.LED_4, False)

    # Write a 4-digit number to the LCD
    def write_number(self, value):
        value = int(value)
        if value > 9999:
            print "Value %s is out of bounds, setting to 9999" % value
            value = 9999
        value = str(value).zfill(4)
        self.segment.writeDigit(0, int(value[0]))
        self.segment.writeDigit(1, int(value[1]))
        self.segment.writeDigit(3, int(value[2]))
        self.segment.writeDigit(4, int(value[3]))
        return self

    def get_pressed_button(self):
        if self.is_button_pressed(self.BUTTON_START):
            return self.BUTTON_START
        if self.is_button_pressed(self.BUTTON_RESET):
            return self.BUTTON_RESET
        return None

    def output(self, text):
        current_time = time.strftime("%H:%M:%S", time.gmtime())
        print "[%s] %s" % (current_time, text)

    def reset(self):
        self.output("Reset - press START/PAUSE to run.")
        self.current_status = self.STATUS_STANDBY
        self.pomodoros_completed = 0
        self.elapsed_seconds = 0
        self.clear_LEDs()
        self.segment.disp.clear()
        self.segment.setColon(0)

    def start(self):
        self.output("Starting...")
        self.current_status = self.STATUS_RUNNING

    def standby(self):
        if self.current_status is self.STATUS_STANDBY:
            self.output("Continuing")
            self.current_status = self.STATUS_RUNNING
        else:
            self.output("Standby mode - press START/PAUSE to continue.")
            self.current_status = self.STATUS_STANDBY
        self.segment.setColon(GPIO.IN)

    def process_button_presses(self):
        pressed_btn = self.get_pressed_button()
        if pressed_btn is None:
            return

        if pressed_btn is self.BUTTON_RESET:
            self.reset()
        else:
            if self.current_status is self.STATUS_STANDBY:
                self.start()
            else:
                self.standby()

        # Debounce
        time.sleep(0.05)

        # Wait for the button to be released
        while self.get_pressed_button() is not None:
            time.sleep(0.05)

    def seconds_to_hm(self, seconds):
        time_string = str(datetime.timedelta(seconds=seconds))
        return time_string[2:4] + time_string[5:7]

    # Light N number of LEDs
    def activate_n_LEDs(self, no_of_LEDs):
        self.clear_LEDs()
        if no_of_LEDs is 1:
            GPIO.output(self.LED_1, True)
        elif no_of_LEDs is 2:
            GPIO.output(self.LED_1, True)
            GPIO.output(self.LED_2, True)
        elif no_of_LEDs is 3:
            GPIO.output(self.LED_1, True)
            GPIO.output(self.LED_2, True)
            GPIO.output(self.LED_3, True)
        else:
            GPIO.output(self.LED_1, True)
            GPIO.output(self.LED_2, True)
            GPIO.output(self.LED_3, True)
            GPIO.output(self.LED_4, True)

    def complete_pomodoro(self):
        if self.pomodoros_completed == 4:
            self.output("Four Pomodoros completed! Time for a longer break.")
            self.reset()
            return

        self.pomodoros_completed += 1
        self.elapsed_seconds = 0
        self.output(
            "Pomodoro no #%s completed! Take a short break and press the START/PAUSE when you return."
            % self.pomodoros_completed)
        self.activate_n_LEDs(self.pomodoros_completed)
        self.segment.disp.clear()

        self.current_status = self.STATUS_STANDBY
        self.segment.setColon(0)

    # Main loop
    def run(self):

        elapsed_seconds = 0

        while True:

            # Check for reset / start / pause btn press
            self.process_button_presses()

            # Whether we need to complete the current Pomodoro
            if self.elapsed_seconds >= self.POMODORO_LENGTH:
                self.complete_pomodoro()
                continue

            # Increase timer when the status is running
            if self.current_status is self.STATUS_RUNNING:
                self.elapsed_seconds += 1
                display_value = self.seconds_to_hm(self.elapsed_seconds)
                self.write_number(display_value)
                self.output(display_value)

                if self.elapsed_seconds % 2 is 0:
                    self.segment.setColon(GPIO.OUT)
                else:
                    self.segment.setColon(GPIO.IN)

            time.sleep(1)

# Cleanup on exit

    def __del__(self):
        self.segment.disp.clear()
        GPIO.cleanup()
class SHControlCombo7SegColourRotary(SHControl):

    #Adafruit I2C 7-segment
    segment = SevenSegment(address=0x70)
    lookup7segchar = {
        '0': 0x3F,
        '1': 0x06,
        '2': 0x5B,
        '3': 0x4F,
        '4': 0x66,
        '5': 0x6D,
        '6': 0x7D,
        '7': 0x07,
        '8': 0x7F,
        '9': 0x6F,
        ' ': 0x00,
        '_': 0x08,
        'a': 0x5F,
        'A': 0x77,
        'b': 0x7C,
        'B': 0x7C,
        'c': 0x58,
        'C': 0x39,
        'd': 0x5E,
        'D': 0x5E,
        'e': 0x7B,
        'E': 0x79,
        'f': 0x71,
        'F': 0x71,
        'g': 0x6F,
        'G': 0x3D,
        'h': 0x74,
        'H': 0x76,
        'i': 0x04,
        'I': 0x06,
        'j': 0x1E,
        'J': 0x1E,
        'k': 0x08,
        'K': 0x08,
        'l': 0x06,
        'L': 0x38,
        'm': 0x08,
        'M': 0x08,
        'n': 0x54,
        'N': 0x37,
        'o': 0x5C,
        'O': 0x3F,
        'p': 0x73,
        'P': 0x73,
        'q': 0x67,
        'Q': 0x67,
        'r': 0x50,
        'R': 0x31,
        's': 0x6D,
        'S': 0x6D,
        't': 0x78,
        'T': 0x78,
        'u': 0x1C,
        'U': 0x3E,
        'v': 0x08,
        'V': 0x07,
        'w': 0x08,
        'W': 0x08,
        'x': 0x08,
        'X': 0x08,
        'y': 0x6E,
        'Y': 0x6E,
        'z': 0x5B,
        'Z': 0x5B,
        '-': 0x40
    }

    #Print to the 7-seg
    def __displayDigits(self, digits):
        """Print to the 7-seg"""
        disp = -len(digits) % 4 * ' ' + digits
        for i in range(4):
            digit = disp[i]
            if i < 2:
                idx = i
            else:
                idx = i + 1
            self.segment.writeDigitRaw(idx, self.lookup7segchar[digit])

    def __init__(self, controlconfig, ctrlid):
        SHControl.__init__(self, controlconfig, ctrlid)
        #segment defined at module scope
        GPIO.setup(self.pins['BTN'], GPIO.IN, GPIO.PUD_DOWN)
        GPIO.setup(self.pins['RGB_R'], GPIO.OUT)
        GPIO.setup(self.pins['RGB_G'], GPIO.OUT)
        GPIO.setup(self.pins['RGB_B'], GPIO.OUT)
        GPIO.output(self.pins['RGB_R'], GPIO.LOW)
        GPIO.output(self.pins['RGB_G'], GPIO.LOW)
        GPIO.output(self.pins['RGB_B'], GPIO.LOW)
        SHControlCombo7SegColourRotary.__displayDigits(self, "    ")

    def __prepType__(self, ctrldef, ctrltype, ctrlid):
        if ctrltype == 'button':
            SHControlCombo7SegColourRotary.__displayDigits(self, "PUSH")

    def poll(self, controlsetup, ctrldef, ctrltype, ctrlstate, ctrlvalue):
        #Do the rotary encoder init on the first poll
        global rotaryInit, rotaryQueue
        if not rotaryInit:
            self.rotary = RotaryEncoder(
                rotaryQueue, "Rotary",
                [self.pins['ROT_A'], self.pins['ROT_B']], GPIO)
            self.rotary.setDaemon(True)
            self.rotary.start()
            rotaryInit = True
        value = ctrlvalue
        btn = GPIO.input(self.pins['BTN'])

        if ctrltype in ['button', 'toggle']:
            state = btn
            if ctrlstate != state:
                if ctrltype == 'button':
                    value = state
                elif ctrltype == 'toggle':
                    if state:
                        value = 1 - ctrlvalue
        else:
            state = 'still'
            while not rotaryQueue.empty():
                try:
                    qdir = rotaryQueue.get(False)
                except Empty:
                    qdir = 'still'

                if 'invert' in self.controlsetup:
                    if qdir == 'cw':
                        qdir = 'ccw'
                    elif qdir == 'ccw':
                        qdir = 'cw'

                if ctrltype == 'selector':
                    value = ctrlvalue
                    if qdir == 'ccw':
                        if ctrlvalue > ctrldef['min']:
                            value = ctrlvalue - 1
                    elif qdir == 'cw':
                        if ctrlvalue < ctrldef['max']:
                            value = ctrlvalue + 1
                elif ctrltype == 'colour':
                    try:
                        idx = ctrldef['values'].index(ctrlvalue)
                    except ValueError:
                        idx = 0
                    if qdir == 'cw':
                        if idx < len(ctrldef['values']) - 1:
                            idx += 1
                        else:
                            idx = 0
                    elif qdir == 'ccw':
                        if idx > 0:
                            idx -= 1
                        else:
                            idx = len(ctrldef['values']) - 1
                    value = str(ctrldef['values'][idx])
                elif ctrltype == 'words':
                    idx = ctrldef['pool'].index(ctrlvalue)
                    if qdir == 'cw':
                        if idx < len(ctrldef['pool']) - 1:
                            idx += 1
                        else:
                            idx = 0
                    elif qdir == 'ccw':
                        if idx > 0:
                            idx -= 1
                        else:
                            idx = len(ctrldef['pool']) - 1
                    value = str(ctrldef['pool'][idx])

        return value, state

    def processValueAssignment(self,
                               roundconfig,
                               value,
                               ctrlid,
                               override=False):
        print("combo process value - value = '" + str(value) + "'")
        if SHControl.processValueAssignment(self, roundconfig, value, ctrlid,
                                            override):
            print("ebabled = '" + str(self.roundsetup['enabled']) +
                  "' type = '" + str(self.ctrltype) + "'")
            RGB = [0, 0, 0]
            if self.roundsetup['enabled']:
                if self.ctrltype == 'toggle':
                    if value:
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, 'On')
                        RGB = [1, 0, 0]
                    else:
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, 'Off')
                        #Switch off LED
                elif self.ctrltype == 'button':
                    if value:
                        RGB = [1, 0, 0]
                elif self.ctrltype == 'selector':
                    SHControlCombo7SegColourRotary.__displayDigits(
                        self, str(value))
                    #Switch off LED
                elif self.ctrltype == 'colour':
                    #Light LED appropriate colour
                    if value == 'red':
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, "RED")
                    elif value == 'green':
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, "GREN")
                    elif value == 'blue':
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, "BLUE")
                    elif value == 'yellow':
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, "YELO")
                    elif value == 'cyan':
                        SHControlCombo7SegColourRotary.__displayDigits(
                            self, "CYAN")
                    RGB = self.controlsetup['colours'][str(value)]
                elif self.ctrltype == 'words':
                    #Switch off LED
                    SHControlCombo7SegColourRotary.__displayDigits(
                        self, value.upper())
            else:
                SHControlCombo7SegColourRotary.__displayDigits(self, "    ")

            GPIO.output(self.pins['RGB_R'], RGB[0])
            GPIO.output(self.pins['RGB_G'], RGB[1])
            GPIO.output(self.pins['RGB_B'], RGB[2])
        else:
            print("Combo reports not valid for processing control value")
示例#6
0
 def __init__(self):
     threading.Thread.__init__(self)
     self.segment = SevenSegment(address=0x70)
     self.stopping = False
示例#7
0
#!/usr/bin/python

import os
import logging
import math
import requests
import signal
import sys
import time
import datetime
from Adafruit_7Segment import SevenSegment
from astral import Astral

segment = SevenSegment(address=0x77)

APP_HOME = os.path.dirname(os.path.realpath(__file__))
FORMAT = '%(asctime)-15s %(levelname)s:%(message)s'
logging.basicConfig(format=FORMAT,
                    filename=APP_HOME + '/log/clock.err',
                    level=logging.ERROR)
# format=FORMAT, filename=APP_HOME + '/log/clock.log', level=logging.DEBUG)


def receive_signal(signal, frame):
    logging.debug('received signal: %s', signal)
    segment.clear()
    sys.exit(0)


signal.signal(signal.SIGUSR1, receive_signal)
signal.signal(signal.SIGUSR2, receive_signal)
from Adafruit_7Segment import SevenSegment
import time, datetime
import RPi.GPIO as GPIO

switch_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
disp = SevenSegment(address=0x70)

time_mode, seconds_mode, date_mode = range(3)
disp_mode = time_mode


def display_time():
    # Get the time by separate parts for the clock display
    now = datetime.datetime.now()
    hour = now.hour
    minute = now.minute
    second = now.second
    # Set hours
    disp.writeDigit(0, int(hour / 10))  # Tens
    disp.writeDigit(1, hour % 10)  # Ones
    # Set minutes
    disp.writeDigit(3, int(minute / 10))  # Tens
    disp.writeDigit(4, minute % 10)  # Ones
    # Toggle colon
    disp.setColon(second % 2)  # Toggle colon at 1Hz


def disply_date():
    now = datetime.datetime.now()
示例#9
0
import Adafruit_BBIO.ADC as ADC
import time
import datetime
from Adafruit_7Segment import SevenSegment
segment = SevenSegment(address=0x70)
sensor_pin = 'P9_40'
ADC.setup()
while(True):
    reading = ADC.read(sensor_pin)
    millivolts = reading * 1800
    temp_c = (millivolts - 500) / 10
    segment.writeDigit(0, int(temp_c / 10))
    segment.writeDigit(1, int(temp_c % 10))
    segment.writeDigit(3, 12)
    segment.setColon(1)
    time.sleep(1)
示例#10
0
	def __init__(self):
		threading.Thread.__init__(self)
		self._segment = SevenSegment(address=0x70)
		self._segment.disp.clear()
		self.ended = False