def showSecondsOnScrollPhatHD(seconds): scrollphathd.clear() float_sec = (seconds % 60) / 59.0 seconds_progress = float_sec * 15 if DISPLAY_BAR: for y in range(15): current_pixel = min(seconds_progress, 1) scrollphathd.set_pixel(y + 1, 6, current_pixel * BRIGHTNESS) seconds_progress -= 1 if seconds_progress <= 0: break else: scrollphathd.set_pixel(int(seconds_progress), 6, BRIGHTNESS) hours = str(int(seconds) / 60 / 60).zfill(2) minutes = str(int(seconds) / 60 % 60).zfill(2) scrollphathd.write_string( hours + ":" + minutes, x=0, # Align to the left of the buffer y=0, # Align to the top of the buffer font=font5x5, # Use the font5x5 font we imported above brightness=BRIGHTNESS # Use our global brightness value ) if int(seconds) % 2 == 0: scrollphathd.clear_rect(8, 0, 1, 5) scrollphathd.show()
def gauge(self): self.index += 1 y = 6 x_max = int(self.gauge_value * 0.17) for x in range(x_max): scrollphathd.set_pixel(x, y, self.brightness) return
def draw_wind_line(): global wind_speed global wind_gusts wind_multiplier = (17.0 / MAX_WIND_SPEED) if DEBUG: print("Wind multiplier: ", wind_multiplier) wind_calc = wind_multiplier * wind_speed if DEBUG: print("wind calc: ", wind_calc) wind_calc = int(wind_calc) #convert to int if wind_calc > 17: #just in case something goes haywire, like a hurricane :-) wind_calc = 17 gust_calc = wind_multiplier * wind_gusts if DEBUG: print("gust calc: ", gust_calc) gust_calc = int(gust_calc) if gust_calc > 17: gust_calc = 17 if DEBUG: print("Wind speed, calc", wind_speed, wind_calc) print("wind gusts, calc", wind_gusts , gust_calc) # Draw the wind speed first for x in range(0,wind_calc): scrollphathd.set_pixel(x, 6, WIND_BRIGHTNESS) # Now draw the gust indicator as a single pixel if gust_calc: #only draw if non zero scrollphathd.set_pixel(gust_calc-1, 6, GUST_BRIGHTNESS) return;
def _loop(self): while True: self.scroll_remaining -= 1 if self.scroll_remaining == 0: leds.clear() leds.show() elif self.scroll_remaining > 0: leds.scroll() leds.show() elif len(self.phrases) > 0: self.scroll_remaining = leds.write_string("| " + self.phrases.pop(0) + " |") - 20 else: t = self.scroll_remaining fade_in = min(1.0, -t / 30000.0) # fade in over 10 minutes, at sleep(0.02) (50Hz) ox = math.sin(t/50.0) * 2.0 oy = math.cos(t/50.0) * 2.0 for x in range(17): dx = float(x) + ox - 8.0 for y in range(7): dy = float(y) + oy - 3.0 d = math.sqrt(math.pow(dx, 2) + math.pow(dy, 2)) b = math.sin(d + (t/5.0)) b = (b + 1) / 2 b = b * fade_in leds.set_pixel(x, y, b) leds.show() time.sleep(0.02)
def wifi_main_loop(): while True: global count, signal_high, signal_low scr.clear() [id, s] = wifi_info() scr.fill(brightness=bright1, x=0, y=1, width=s - 1, height=height - 2) scr.fill(brightness=bright3, x=s - 1, y=1, width=1, height=height - 2) scr.fill(brightness=bright2, x=signal_high - 1, y=0, width=1, height=height - 1) scr.fill(brightness=bright2, x=signal_low - 1, y=1, width=1, height=height - 1) if (count > 0 and count <= 9): scr.set_pixel(x=0, y=3, brightness=bright) elif (count > 9 and count <= 19): #scr.clear_rect(0, 0, 1, 1) scr.set_pixel(x=0, y=3, brightness=bright2) elif (count >= 19): count = 0 count = count + 1 scr.show() time.sleep(delay)
def draw_circle(x, y, radius): if radius == 1: scrollphathd.set_pixel(x, y, 0.5) return cur_x = x cur_y = y - radius + 1 while y != cur_y: if cur_y >= 0 and cur_x >= 0: scrollphathd.set_pixel(cur_x, cur_y, 0.5) cur_x = cur_x - 1 cur_y = cur_y + 1 while x != cur_x: if cur_y >= 0 and cur_x >= 0: scrollphathd.set_pixel(cur_x, cur_y, 0.5) cur_x = cur_x + 1 cur_y = cur_y + 1 while y != cur_y: if cur_y >= 0 and cur_x >= 0: scrollphathd.set_pixel(cur_x, cur_y, 0.5) cur_x = cur_x + 1 cur_y = cur_y - 1 while x != cur_x: if cur_y >= 0 and cur_x >= 0: scrollphathd.set_pixel(cur_x, cur_y, 0.5) cur_x = cur_x - 1 cur_y = cur_y - 1
def get_lines(): global lengths global lines global line_height lines = [] with open('/home/pi/weather/conditions', 'r') as conditions: lines = conditions.read().splitlines() print(lines) # Determine how far apart each line should be spaced vertically line_height = scrollphathd.DISPLAY_HEIGHT + 2 # Store the left offset for each subsequent line (starts at the end of the last line) offset_left = 0 # Draw each line in lines to the Scroll pHAT HD buffer # scrollphathd.write_string returns the length of the written string in pixels # we can use this length to calculate the offset of the next line # and will also use it later for the scrolling effect. lengths = [0] * len(lines) for line, text in enumerate(lines): lengths[line] = scrollphathd.write_string(text, x=offset_left, y=line_height * line) offset_left += lengths[line] # This adds a little bit of horizontal/vertical padding into the buffer at # the very bottom right of the last line to keep things wrapping nicely. scrollphathd.set_pixel(offset_left - 1, (len(lines) * line_height) - 1, 0)
def main(): brightness = 0 last_time = None show_colon = False ldr = LightSensor(LDR_PIN) if "flip" in sys.argv: sphd.rotate(180) while True: now = datetime.now() current_time = (now.hour, now.minute) old_brightness = brightness brightness = MAX_BRIGHTNESS if ldr.light_detected else 0.1 if current_time != last_time or brightness != old_brightness: sphd.clear() draw_digit(0, 0, now.hour // 10, brightness) draw_digit(4, 0, now.hour % 10, brightness) draw_digit(10, 0, now.minute // 10, brightness) draw_digit(14, 0, now.minute % 10, brightness) last_time = current_time if show_colon != (now.microsecond < 500000) or brightness != old_brightness: show_colon = now.microsecond < 500000 sphd.set_pixel(8, 1, brightness * show_colon) sphd.set_pixel(8, 5, brightness * show_colon) sphd.show() time.sleep(0.01)
def show_clock(): # Display a progress bar for seconds # Displays a dot if False BRIGHTNESS = 0.3 while int((time.time() % 60)) not in [20, 40]: phat.clear() float_sec = (time.time() % 60) / 59.0 seconds_progress = float_sec * 15 for y in range(15): current_pixel = min(seconds_progress, 1) phat.set_pixel(y + 1, 6, current_pixel * BRIGHTNESS) seconds_progress -= 1 if seconds_progress <= 0: break phat.write_string( time.strftime("%H:%M"), x=0, # Align to the left of the buffer y=0, # Align to the top of the buffer font=font5x5, # Use the font5x5 font we imported above brightness=BRIGHTNESS # Use our global brightness value ) if int(time.time()) % 2 == 0: phat.clear_rect(8, 0, 1, 5) phat.show() time.sleep(0.1)
def drawHeart(): buffer1 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ] buffer2 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] animbuffer = [buffer1, buffer2] frames = 2 for loop in range(0, 10): pic = animbuffer[loop % frames] for pos in range(0, len(pic)): xpos = pos % 17 ypos = int(pos / 17) sphd.set_pixel(xpos, ypos, float(pic[pos])) sphd.show() time.sleep(1)
def progress_show(level, per, prev_x, prev_y): #max value for x in the led display max_x = 11 fix_x = prev_x fix_y = prev_y # get the upper value of the percentage e.g. 4.1 = 5 cal_x = math.ceil(per / 10) #print("Cal_x:",cal_x) if cal_x >= max_x: cal_x = max_x for y in range(fix_y, level, 1): x_limit = max_x if y == level - 1: x_limit = cal_x + 1 if x_limit >= max_x: x_limit = max_x for x in range(fix_x, x_limit): if prev_x != 1 or prev_y != 5: scrollphathd.set_pixel(prev_x, 5 - prev_y, 0.1) scrollphathd.set_pixel(x, 5 - y, 0.3) scrollphathd.clear_rect(x=13, y=1, width=3, height=5) scrollphathd.write_string(str(y + 1), x=13, y=1, font=font3x5, brightness=0.3) scrollphathd.show() time.sleep(0.1) prev_x = x prev_y = y fix_x = 1 return prev_x, prev_y
def setPixel(): if request.is_json: schema = td["actions"]["setPixel"]["input"] valid_input = Draft6Validator(schema).is_valid(request.json) if valid_input: x = 5 y = 5 try: x = int(request.json["x"]) except Exception as e: print(e) try: y = int(request.json["y"]) except Exception as e: print(e) try: bright = float(request.json["brightness"]) scrollphathd.clear() scrollphathd.show() scrollphathd.set_pixel(x, y, bright) scrollphathd.show() return "", 204 except Exception as e: print(e) abort(400) else: abort(400) else: abort(415) # Wrong media type.
def internet_on(): try: urllib2.urlopen('http://216.58.192.142', timeout = 1) sphd.set_pixel(0,0,0.3) sphd.show() return True except urllib2.URLError as err: return False
def mainloop(): scrollphathd.rotate(degrees=180) scrollphathd.clear() scrollphathd.show() while True: # grab the tweet string from the queue try: scrollphathd.clear() status = q.get(False) scrollphathd.write_string(status, font=font5x7, brightness=0.1) status_length = scrollphathd.write_string(status, x=0, y=0, font=font5x7, brightness=0.1) time.sleep(0.25) while status_length > 0: scrollphathd.show() scrollphathd.scroll(1) status_length -= 1 time.sleep(0.02) scrollphathd.clear() scrollphathd.show() time.sleep(0.25) q.task_done() except queue.Empty: float_sec = (time.time() % 60) / 59.0 seconds_progress = float_sec * 15 if DISPLAY_BAR: for y in range(15): current_pixel = min(seconds_progress, 1) scrollphathd.set_pixel(y + 1, 6, current_pixel * BRIGHTNESS) seconds_progress -= 1 if seconds_progress <= 0: break else: scrollphathd.set_pixel(int(seconds_progress), 6, BRIGHTNESS) scrollphathd.write_string(time.strftime("%H:%M"), x=0, y=0, font=font5x5, brightness=BRIGHTNESS) if int(time.time()) % 2 == 0: scrollphathd.clear_rect(8, 0, 1, 5) scrollphathd.show() time.sleep(0.25)
def scrollList(output): #accepts a list of strings and displays to the scrollphat #buffer size is limited, suggesting to limit lines < 10, depending on length of each object #setup the display default parameters scrollphathd.rotate(displayrotation) scrollphathd.set_brightness(displaybrightness) # displayRewind = True # rapidly displayRewind after the last line # delay = 0.009 # Delay is the time (in seconds) between each pixel scrolled # Draw each line in lines to the Scroll pHAT HD buffer scrollphathd.clear() #clear the buffer before displaying the next list line_height = scrollphathd.DISPLAY_HEIGHT + 3 # Determine how far apart each line should be spaced vertically offset_left = 0 # Store the left offset for each subsequent line (starts at the end of the last line) lengths = [0] * len(output) # Get the length of each 'line' for the buffer for line, text in enumerate(output): lengths[line] = scrollphathd.write_string( text, x=offset_left, y=line_height * line ) # scrollphathd.write_string returns the length of the written string in pixels offset_left += lengths[ line] # we can use this length to calculate the offset of the next line for scrolling effect lateer scrollphathd.set_pixel( offset_left - 1, (len(output) * line_height) - 1, 0 ) # adds some horizontal/vertical padding to the buffer at the very bottom right of the last line to wrap nice scrollphathd.scroll_to(0, 0) # Reset animation scrollphathd.show() pos_x = 0 # Keep track of the X and Y position for the displayRewind effect pos_y = 0 for current_line, line_length in enumerate(lengths): time.sleep( displayScrollSpeed * 10) # Delay a slightly longer time at the start of each line for y in range(line_length): # Scroll to the end of the current line scrollphathd.scroll(1, 0) pos_x += 1 time.sleep(displayScrollSpeed) scrollphathd.show() if current_line == len( output ) - 1 and displayRewind: # If on the very last line and displayRewind is True, rapidly scroll back to the first line. for y in range(pos_y): scrollphathd.scroll(-int(pos_x / pos_y), -1) scrollphathd.show() time.sleep(displayScrollSpeed) scrollphathd.clear() #Clear the buffer else: # Otherwise, progress to the next line by scrolling upwards for x in range(line_height): scrollphathd.scroll(0, 1) pos_y += 1 scrollphathd.show() time.sleep(displayScrollSpeed)
def display_buf(ibuf): print("Outputting:") for x in range(17): for y in range(7): sphd.set_pixel(x, y, ibuf[x][y]) sys.stdout.write('#' if ibuf[x][y] else ' ') sys.stdout.write('\n') sys.stdout.flush() sphd.show()
def __init__(self): # draw a full moon sphd.clear() # blank the screen brightness = 1 # set initial brightness for x in range(5, 11): for y in range(7): sphd.set_pixel(x, y, brightness) # turn of corners x_rows = [0, 1, 5, 6] y_rows = [5, 6, 10, 11]
def rand_pixel(): x = random.randint(1, 15) y = random.randint(1, 5) b = random.randint(0, 5) / 10 scrollphathd.set_pixel(x, y, b) x = random.randint(1, 15) y = random.randint(1, 5) scrollphathd.set_pixel(x, y, 0) scrollphathd.show()
def show(self): for w in range(self.width): for h in range(self.height): if self.pixels[w][h]: sphd.set_pixel(w, h, self.pixels[w][h].get_brightness()) else: sphd.set_pixel(w, h, 0) sphd.set_brightness(0.1) sphd.show()
def draw_kr_pulse(pos,dir): # clear 5 pixel line (easier than keeping track of where the previous illuminated pixel was) scrollphathd.clear_rect(12,5,5,1) x = pos + 11 #increase position to the actual x offset we need scrollphathd.set_pixel(x, 5, 0.2) #turn on the current pixel scrollphathd.show() time.sleep(KR_PULSE_DELAY) return;
def draw(history): if len(history) < 5: return segments = history[-17:] high = max(history[-60:]) low = min(history[-60:]) range = high - low for i, v in enumerate(segments): y = (v - low) / range sphd.set_pixel(i, 6 - int(y * 6), 0.25)
def display_life(current_game): sphd.clear() # blank the screen for key in current_game: # set our new pixels if current_game[key] == 1: # if 1, then light up the LED # find Y axis value y = key//17 # find X axis x = key - (y * 17) sphd.set_pixel(x, y, 1) # set a brightness of 1 seeing as our default is already .25 and it is cumulative sphd.show() # show the new screen
def tv_main_loop(): while True: #scr.clear() g = random.uniform(0.0, 0.2) for x in range(width): for y in range(height): b = random.uniform(0.2, 0.5) scr.set_pixel(x=x, y=y, brightness=b + g) scr.show() time.sleep(delay)
def hit_paddle(self, value): self.v_x = self.v_x * -1 self.freq = BALL_FREQ if value == 2: self.freq -= 2 if self.x <= 0: #move ball in front of paddle self.x = 1 elif self.x >= 16: self.x = 15 sphd.set_pixel(self.x, self.y, BRIGHTNESS)
def get_tech_headline(): GPIO.output(21, GPIO.HIGH) tech_response = urllib2.urlopen(tech_rss) tech_text = tech_response.read() tech_root = et.fromstring(tech_text) tech_headline = tech_root[0][9][0].text sphd.set_pixel(0,0,0.3) sphd.show() GPIO.output(21, GPIO.LOW) print("Tech: " + tech_headline) return tech_headline
def get_sport_headlines(): GPIO.output(21, GPIO.HIGH) sport_response = urllib2.urlopen(sport_rss) sport_text = sport_response.read() sport_root = et.fromstring(sport_text) sport_headline1 = sport_root[0][9][0].text sport_headlines.append(sport_headline1) sphd.set_pixel(2,0,0.3) sphd.show() GPIO.output(21, GPIO.LOW) print("Sport: " + sport_headlines[0])
def scan(self): y = 6 brightness_range = [0.7, 0.5, 0.3, 0.2, 0.1] dir = 1 if (self.index // 16) % 2 != 0: x = 16 - (self.index % 16) dir = -1 else: x = (self.index % 16) + 1 for i in range(5): if 0 <= x - i * dir <= 16: scrollphathd.set_pixel(x - i * dir, y, brightness_range[i]) self.index += 1
def draw_square(x, y, length): if length == 1: scrollphathd.set_pixel(x, y, 0.5) return for i in range(length + 1): if x + i >= 0 and y >= 0: scrollphathd.set_pixel(x + i, y, 0.5) if x >= 0 and y + i >= 0: scrollphathd.set_pixel(x, y + i, 0.5) if x + length - i >= 0 and y + length >= 0: scrollphathd.set_pixel(x + length - i, y + length, 0.5) if x + length >= 0 and y + length - i >= 0: scrollphathd.set_pixel(x + length, y + length - i, 0.5)
def pacmanClose(xpos, ypos, reverse): pixels = np.asarray([[0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0]]) for y in range(7): for x in range(7): if reverse: if pixels[y][x] == 1: sphd.set_pixel(xpos + (7 - x), ypos + y, brightSet) else: if pixels[y][x] == 1: sphd.set_pixel(xpos + x, ypos + y, brightSet)
def showFace(faceToShow): print("Showing Face") clearScrollPhatHD() for column in range(0, 17): for row in range(0, 7): scrollphathd.set_pixel(column, row, faceToShow[column][row]) scrollphathd.show()