def feedme(feed): feed = feedparser.parse(feed.encode('utf-8')) for i in range(6): b.graph_set_led_state(i,1) print(feed['entries'][i]['title']) scrollText(feed['entries'][i]['title']) b.graph_off()
def handle_level(self, level_data): peaks = level_data['peak'] bar_height = round((sum(peaks) / len(peaks) + 100.0) / 100.0 * 6.0) for i in range(bar_height): backlight.graph_set_led_state(5 - i, 1) for i in range(bar_height, 6): backlight.graph_set_led_state(5 - i, 0)
def graph_CPUTemp(lednumber): CPUTemp = getCPUtemperature() if float(CPUTemp) > 60.0: #print CPUTemp backlight.graph_set_led_duty(0, 1) backlight.graph_set_led_state(lednumber, 1) else: backlight.graph_set_led_state(lednumber, 0)
def update(): while True: global string, last r = requests.get(URL, verify=False) #uncomment to use bitstamp api #priceFloat = float(json.loads(r.text)['Last']) price = float(json.loads(r.text)['result'][0]['Last']) if price > last: backlight.graph_set_led_state(0, 1) backlight.graph_set_led_state(5, 0) last = price elif price < last: backlight.graph_set_led_state(0, 0) backlight.graph_set_led_state(5, 1) last = price string = "$" + str(price) sleep(0.5)
This example shows you how to take individual control of the bar graph LEDs. You should see the bar graph LEDs count up in binary! Press CTRL+C to exit. """) # Each LED can be either on/off, # and brightness is controlled globally using: # * graph_set_led_duty(min, max) # When you don't need a bar graph, these LEDs could display # remaining lives in a game, the status of different processes, # the hour of the day in binary or anything else you might need! lcd.set_cursor_position(0, 1) lcd.write(" So Graph! ") # Reset the LED states and polarity backlight.graph_off() # Dim the LEDs by setting the max duty to 1 backlight.graph_set_led_duty(0, 1) # Now run a binary counter on the LEDs while True: for x in range(64): for led in range(6): backlight.graph_set_led_state(led, x & (1 << led)) time.sleep(0.1)
#!/usr/bin/python "Import needed libraries" from dothat import backlight, lcd from dot3k.menu import MenuOption, Menu import json, requests from time import sleep import threading "Clear the HAT screen and set static content" lcd.clear() lcd.set_contrast(50) backlight.set_graph(0) backlight.graph_set_led_state(0, 0) backlight.graph_set_led_state(5, 0) lcd.set_cursor_position(0, 0) lcd.write("Bittrex BTC-PAY:") lcd.set_cursor_position(0, 0) lcd.write("I'm rich biatch!") "bitstamp API url for updated price information" "added bittrex prices for pairs" #URL = 'https://www.bitstamp.net/api/ticker/' URL = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-pay" string = "" last = 0 class Ticker(MenuOption): def redraw(self, menu):
illum = sensors['Illuminance'] if illum < 300: illum_msg = "Dark!" os.system("sudo hub-ctrl -b 1 -d 2 -P 2 -p 1") #dothat.backlight.single_rgb(2, 255, 255, 255) backlight.rgb(255, 255, 255) else: illum_msg = "Bright" os.system("sudo hub-ctrl -b 1 -d 2 -P 2 -p 0") #dothat.backlight.single_rgb(2, 0, 0, 255) backlight.rgb(0, 0, 255) human_msg = str(human_count) dothat.backlight.off() for led in range(human_count): backlight.graph_set_led_state(led, 0.2) if human_count > human_check: human_msg += ' Take Rest!' lcd.clear() #dothat.backlight.single_rgb(3, 0, 255, 0) backlight.rgb(0, 255, 0) else: human_msg += ' Work Hard!' lcd.clear() #dothat.backlight.single_rgb(3, 0, 255, 255) backlight.rgb(0, 255, 255) lcd.clear() lcd.set_cursor_position(0, 0) lcd.write('T:{0:1.0f}C {1:1.0f}% {2}'.format(temp,humid,temp_msg)) lcd.set_cursor_position(0, 1)
import dothat.backlight as backlight import dothat.lcd as lcd import time ''' Each LED can be either on/off, and brightness is controlled globally using: * graph_set_led_duty(min, max) When you don't need a bar graph, these LEDs could display remaining lives in a game, the status of different processes, the hour of the day in binary or anything else you might need! ''' lcd.set_cursor_position(0, 1) lcd.write(" So Graph! ") # Reset the LED states and polarity backlight.graph_off() # Dim the LEDs by setting the max duty to 1 backlight.graph_set_led_duty(0, 1) # Now run a binary counter on the LEDs while True: for x in range(64): for led in range(6): backlight.graph_set_led_state(led, x & (1 << led)) time.sleep(0.1)
def redraw(self, menu): """ The output to the LCD looks as follows |Mon 01 Jan 16:35| |Mon 01 Jan 16.35| |[1] Now 0d7h25m | |[1] In 04d5h25m | |Event descriptio| |Event descriptio| Line 1: constantly updating clock. Time seperator blinks between : and . Line 2: Event number in square brackets Ongoing events show "Now", an arrow and time to end of event. Upcoming events show "In", and a countdown to start of event. Line 3: Event description. "All Day:" is appended if it's an all day event. Will scroll if the line doesn't fully fit on the screen. """ # Start by seeing if we need to be in a reminder state reminderactive=0 for reminder in self.reminders: timenow=datetime.datetime.now(self.localtz) if (timenow > reminder['start'] and timenow < reminder['end']): reminderactive=1 # Decide what to do with the graph LEDs and set them graphstates = self.CalculateGraph(reminderactive) for x in range(6): backlight.graph_set_led_state(x,graphstates[x]) # If it's time for a refresh because an event's ended, do a background refresh if (datetime.datetime.now(self.localtz) > self.nextrefresh): thread.start_new_thread(self.BgUpdateCalendar,()) return # If maxevents is zero, we have no events due to an error or an empty calendar if (self.maxevents == 0): menu.write_option( row=0, text="No events!", scroll=False ) menu.write_option( row=1, text="Rechecking at", scroll=False ) menu.write_option( row=2, text=str(self.nextrefresh.time().replace(microsecond=0)), scroll=False ) return # If there's a calendar update happening, say so and do nothing else. # Don't update if the screensave is active.. if (self.updating_calendar == 1 and self.screensave==0): menu.write_option( row=0, text="Please Wait", scroll=False ) menu.write_option( row=1, text="Updating", scroll=False ) menu.write_option( row=2, text="Calendar", scroll=False ) return # Do nothing if the screen is turned off if (self.screensave == 1): return # If the idle timer has been reached, turn the screen off if ((time.time() - self.idletimer) > self.idletimeout): lcd.clear() self.screensave = 1 backlight.rgb(0,0,0) return # A screensave state of 2 means we need to activate the screen/ # Restore the backlight to the last saved RGB values. if (self.screensave == 2): backlight.rgb(self.red,self.green,self.blue) # Either the displayed event has been changed, or the time has moved on a minute # In both cases, we need to recalculate the countdown strings if ((self.d_event != self.c_event) or (datetime.datetime.now().minute != self.last_update)): # Create timediff items so we can see when the start and finish are relative to now self.timetoevent=self.events[self.d_event]['estart_dt'] - datetime.datetime.now(self.localtz) self.timetoend=self.events[self.d_event]['eend_dt'] - datetime.datetime.now(self.localtz) # Calculate days/hours/mins remaining till event start self.tte_days = self.timetoevent.days self.tte_secs = self.timetoevent.seconds self.tte_hours = (self.tte_secs // 3600) # +1 minute because we're not counting seconds self.tte_mins = ((self.tte_secs % 3600) // 60) + 1 # Though this does introduce a kettle of worms that 1h60m is a possible result. if (self.tte_mins == 60): self.tte_hours = self.tte_hours + 1 self.tte_mins = 0 # Calculate days/hours/mins remaining till event finish self.ttee_days = self.timetoend.days self.ttee_secs = self.timetoend.seconds self.ttee_hours = (self.ttee_secs // 3600) # +1 minute because we're not counting seconds self.ttee_mins = ((self.ttee_secs % 3600) // 60) + 1 # Though this does introduce a kettle of worms that 1h60m is a possible result. if (self.ttee_mins == 60): self.ttee_hours = self.ttee_hours + 1 self.ttee_mins = 0 # Update state to reflect the event and the timestamp we've calculated for self.c_event = self.d_event self.last_update = datetime.datetime.now().minute # If the number of days to the event is positive, the event is upcoming. # Work out how long we have till the event starts. # If it's negative, the event has already started, so instead we work out how long till the end if (self.tte_days >= 0): # If it's over a week away, just show days remaining if (self.tte_days > 7): countdown = ("[" + str(self.d_event+1) + "] in " + str(self.tte_days) + "d" ) else: countdown=("[" + str(self.d_event+1) + "] in " + str(self.tte_days) + "d" + str(self.tte_hours) + "h" + str(self.tte_mins) + "m" ) start = self.events[self.d_event]['estart_dt'].astimezone(self.localtz).strftime("%H:%M") else: # Recreate the arrow character when we know we need to use it. Just to be safe. # Just show days if it's not finishing in less than a week :) lcd.create_char(1, self.arrow) if (self.ttee_days > 7): countdown= "["+ str(self.d_event+1) + "] Now\x01" + str(self.ttee_days) + "d" else: countdown= ("["+ str(self.d_event+1) + "] Now\x01" + str(self.ttee_days) + "d" + str(self.ttee_hours) + "h" + str(self.ttee_mins) + "m" ) start = "" # If the event is less than 300 minutes away (5 hours), vary the backlight # Hue values 0.0 -> 0.3 give us a decent green -> red transition. # Take the number of seconds left, divide by 60 to give us minutes remaining # then divide by 1,000 to give us 0.3 at 300 mins left ~~> 0.0 at 0 mins. # Use the provided hue_to_rgb() function to do the maths for us, and set the # backlight accordingly. if (self.tte_days == 0 and (float(self.tte_secs) / 60) < 300): rgb = backlight.hue_to_rgb(float(self.tte_secs)/60000) self.SetRGB(rgb[0], rgb[1], rgb[2]) else: self.SetRGB(self.defaultR, self.defaultG, self.defaultB) # Pick out the event summary if (self.events[self.d_event]['allday_flag'] == True): summary = "All Day:" + self.events[self.d_event]['summary'] else: summary = start + " " + self.events[self.d_event]['summary'] # We don't need to bother scrolling events with short names if (len(summary) < 16): scrollsummary = False else: scrollsummary = True # When the clock's active, we use a custom character for the colon # We redefine this character every other second so the top dot is # missing. Which gives us a nice 'blinking' top dot in the time :) animframe = (datetime.datetime.now().second % 2) lcd.create_char(2, self.clockanim[animframe]) # datetime.now returns local time, so this behaves correctly clockstring=datetime.datetime.now(self.localtz).strftime('%a %d %b %H\x02%M') # Write the menu rows menu.write_option( row=0, text=clockstring, scroll=False ) menu.write_option( row=1, text=countdown, scroll=False ) menu.write_option( row=2, text=summary, scroll=scrollsummary )
def set_leds(self): self.num_leds += 1 for led in range(6): backlight.graph_set_led_state(led, self.num_leds & (1 << led))
def blink_led(self,led,time_on=0.005): backlight.graph_set_led_duty(0,1) backlight.graph_set_led_state(led,1) # time.sleep(time_on) # LED visible even without delay backlight.graph_set_led_state(led,0)
elif command == 'setZoneToRGB': backlight.single_rgb(parameters['zoneIndex'], parameters['red'], parameters['green'], parameters['blue']) elif command == 'setLeftToRGB': backlight.left_rgb(parameters['red'], parameters['green'], parameters['blue']) elif command == 'setMiddleToRGB': backlight.mid_rgb(parameters['red'], parameters['green'], parameters['blue']) elif command == 'setRightToRGB': backlight.right_rgb(parameters['red'], parameters['green'], parameters['blue']) elif component == 'BarGraph': if command == 'turnOff': backlight.graph_off() elif command == 'setByPercentage': backlight.set_graph(float(parameters['percentage']) / 100) elif command == 'setBrightness': backlight.graph_set_led_duty(parameters['minimumBrightness'], parameters['maximumBrightness']) elif command == 'setEnabledStateOfLed': backlight.graph_set_led_polarity(parameters['ledIndex'], 0b00000000) backlight.graph_set_led_state( parameters['ledIndex'], 1 if parameters['enabledState'] else 0) elif component == 'Touch': if command == 'enableHighSensitivityMode': touch.high_sensitivity() elif command == 'enableEventRepeating': touch.enable_repeat(True)
def set_indicator(self, i, status): if status == IndicatorStatus.On: backlight.graph_set_led_state(i, 1) else: backlight.graph_set_led_state(i, 0)
backlight.left_hue(parameters['hue']) elif command == 'setMiddleToHue': backlight.mid_hue(parameters['hue']) elif command == 'setRightToHue': backlight.right_hue(parameters['hue']) elif command == 'setToRGB': backlight.rgb(parameters['red'], parameters['green'], parameters['blue']) elif command == 'setZoneToRGB': backlight.single_rgb(parameters['zoneIndex'], parameters['red'], parameters['green'], parameters['blue']) elif command == 'setLeftToRGB': backlight.left_rgb(parameters['red'], parameters['green'], parameters['blue']) elif command == 'setMiddleToRGB': backlight.mid_rgb(parameters['red'], parameters['green'], parameters['blue']) elif command == 'setRightToRGB': backlight.right_rgb(parameters['red'], parameters['green'], parameters['blue']) elif component == 'BarGraph': if command == 'turnOff': backlight.graph_off() elif command == 'setByPercentage': backlight.set_graph(float(parameters['percentage']) / 100) elif command == 'setBrightness': backlight.graph_set_led_duty(parameters['minimumBrightness'], parameters['maximumBrightness']) elif command == 'setEnabledStateOfLed': backlight.graph_set_led_polarity(parameters['ledIndex'], 0b00000000) backlight.graph_set_led_state(parameters['ledIndex'], 1 if parameters['enabledState'] else 0) elif component == 'Touch': if command == 'enableHighSensitivityMode': touch.high_sensitivity() elif command == 'enableEventRepeating': touch.enable_repeat(True)