Skip to content

Contribution graph on badge #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions preload/contribution_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import badger2040
import badger_os

CONTRIBUTION_GRAPH_PAGE = 0 #page 0 = latest 6 months; page 1 = previous 6 months



class ContributionPage(object):
# Each contribution page contains title, subtitle and graph data
def __init__(self, title, subtitle, contribution_data):
self.title = title
self.subtitle = subtitle
self.contribution_data = contribution_data


#Drawing contribution graph
def draw_contribution_graph(display):
#Clearing the display
display.set_pen(15)
display.clear()

display.set_pen(0) # Black
display.set_thickness(2)
display.set_font("sans")

page_data = contribution_pages[CONTRIBUTION_GRAPH_PAGE]

# Drawing title text
display.text(page_data.title, 8,16,badger2040.WIDTH-16,0.6)
# Drawing subtitle text
display.text(page_data.subtitle, 8,32,badger2040.WIDTH-16,0.5)

# Drawing graph

contributions_of_day = page_data.contribution_data

# 26 weeks (6 months)
for week in range(26):

# 7 days each week
for day_of_week in range(7):

# Calculating position of each cell
x = 8 + (week * 11)
y = 50 + (day_of_week * 11)
index = (week*7)+day_of_week


# Cell value can be 0-5;
# 0 being no contributions, color with lightest pen (15)
# 5 being highest contributions, color with darkest pen (0)
display.set_pen(15 - ((contributions_of_day[index]+1) * 3))
display.rectangle(x, y, 9, 9)


# Open the contributions data files corresponding to the pages
contribution_files = ["contribution_page_1.txt","contribution_page_2.txt"]
contribution_pages = []

# Load file data
for filename in contribution_files:
file = open(filename, "r")
page_title = file.readline()
page_subtitle = file.readline()
contribution_data = []

# Loading 182 days (26 weeks / 6 months)
for x in range(182):
# Read each line for each day's value
contribution_of_day_str = file.readline()
# If day's data does not exist, it may be partial week, set the value to -1
contribution_of_day = int(contribution_of_day_str) if len(contribution_of_day_str)>0 else -1
# Append the day's data
contribution_data.append(contribution_of_day)

# Append the page data
contribution_pages.append(ContributionPage(page_title, page_subtitle, contribution_data))
72 changes: 68 additions & 4 deletions preload/examples/badge.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import badger2040
import jpegdec
import contribution_graph
import badger_os
import json
import machine
import time
import os

# Global Constants
WIDTH = badger2040.WIDTH
Expand All @@ -17,6 +23,9 @@
BADGE_BACKGROUND = "/badges/back.jpg"


CURRENT_PAGE = 0 # 0=Badge; 1=contribution graph


# Will be replaced with badge.txt
# "Universe 2023", first_name, lastname_name, company, title, pronouns to the file on separate lines.
DEFAULT_TEXT = """Universe 2023
Expand Down Expand Up @@ -92,6 +101,39 @@ def draw_badge():

display.update()

def draw_page():
# match case not available, using if-else
if CURRENT_PAGE == 0:
draw_badge()
elif CURRENT_PAGE == 1 or CURRENT_PAGE == 2:
contribution_graph.draw_contribution_graph(display)
display.update()


def exit_to_launcher():
# # Changing state to set running application as launcher

badger_os.state_clear_running()
# state = {"running": "launcher", "page": 0}
# # # Saving the state file
# try:
# with open("/state/launcher.json", "w") as f:
# f.write(json.dumps(state))
# f.flush()

# except OSError:
# # State file does not exist, create it
# import os
# try:
# os.stat("/state")
# except OSError:
# os.mkdir("/state")
# badger_os.state_save("launcher", state)

# # Clear the display and reset device
display.clear()
display.update()
machine.reset()

# ------------------------------
# Program setup
Expand Down Expand Up @@ -145,12 +187,34 @@ def draw_badge():
# Main program
# ------------------------------

draw_badge()
draw_page()

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()
# Buttons UP / DOWN to view previous / next page of contribution graph
if CURRENT_PAGE == 1 and (display.pressed(badger2040.BUTTON_DOWN) or display.pressed(badger2040.BUTTON_UP)):
contribution_graph.CONTRIBUTION_GRAPH_PAGE = 1 if contribution_graph.CONTRIBUTION_GRAPH_PAGE == 0 else 0
draw_page()

# Button A opens launcher
elif display.pressed(badger2040.BUTTON_A):
exit_to_launcher()

# Button B sets current page to Badge
elif CURRENT_PAGE != 0 and display.pressed(badger2040.BUTTON_B):
CURRENT_PAGE = 0
draw_page()

# Button C sets current page to Contribution Graph
elif CURRENT_PAGE != 1 and display.pressed(badger2040.BUTTON_C):
CURRENT_PAGE = 1
contribution_graph.CONTRIBUTION_GRAPH_PAGE = 0
draw_page()

# if incorrect button pressed, show popup info for 5 seconds
elif display.pressed(badger2040.BUTTON_A) or display.pressed(badger2040.BUTTON_B) or display.pressed(badger2040.BUTTON_C) or display.pressed(badger2040.BUTTON_UP) or display.pressed(badger2040.BUTTON_DOWN):
badger_os.warning(display, "a = launcher b = badge c = contributions")
time.sleep(5)
draw_page()

# If on battery, halt the Badger to save power, it will wake up if any of the front buttons are pressed
display.halt()
177 changes: 0 additions & 177 deletions scripts/getdata.py

This file was deleted.

Loading