# Display Image & text on I2C driven ssd1306 OLED display
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
import math
import utime
WIDTH = 128 # oled display width
HEIGHT = 64 # oled display height
# Explicit Method
sda=machine.Pin(4)
scl=machine.Pin(5)
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
# print(i2c.scan())
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 64, i2c)
# Raspberry Pi logo as 32x32 bytearray
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
# Load the raspberry pi logo into the framebuffer (the image is 32x32)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
buttonPos1 = Pin(0, Pin.OUT)
buttonPush1 = Pin(1,Pin.IN)
buttonPos2 = Pin(2, Pin.OUT)
buttonPush2 = Pin(3,Pin.IN)
buttonPos3 = Pin(6, Pin.OUT)
buttonPush3 = Pin(7,Pin.IN)
buttonPos4 = Pin(8, Pin.OUT)
buttonPush4 = Pin(9,Pin.IN)
def intialAnimation():
oled.fill(0)
oled.blit(fb,48,5)
oled.text("By Any Means",16,40)
oled.text("Necessary",28,49)
oled.show()
utime.sleep(3)
oled.fill(0)
oled.show()
def getFarCoords(outOfOneSixEight):
CentrePoints = ((64,31),(64,32),(63,32),(63,31))
quadrantSects = ((0,0),(1,30),(2,30),(3,30),(4,30),(5,29),(6,29),(7,29),(8,29),(9,28),(10,28),(11,28),(12,27),(13,27),(14,26),(15,26),(16,25),(17,24),(18,23),(19,23),(20,22),(21,21),(22,20),(23,19),(23,18),(24,17),(25,16),(26,15),(26,14),(27,13),(27,12),(28,11),(28,10),(28,9),(29,8),(29,7),(29,6),(29,5),(30,4),(30,3),(30,2),(30,1))
if outOfOneSixEight <= 42:
if outOfOneSixEight == 42: # top-right quarter
return CentrePoints[0][0],CentrePoints[0][1],CentrePoints[1][0]+30,CentrePoints[1][1]
else:
return CentrePoints[0][0],CentrePoints[0][1],quadrantSects[outOfOneSixEight][0]+CentrePoints[0][0],30-quadrantSects[outOfOneSixEight][1]+1
if outOfOneSixEight <= 84:
if outOfOneSixEight == 84:
return CentrePoints[1][0],CentrePoints[1][1],CentrePoints[2][0],CentrePoints[2][1]+30
else:
outOfOneSixEight -= 42
return CentrePoints[1][0],CentrePoints[1][1],quadrantSects[outOfOneSixEight][1]+CentrePoints[1][0],CentrePoints[1][1]+quadrantSects[outOfOneSixEight][0]
if outOfOneSixEight <= 126:
if outOfOneSixEight == 126:
return CentrePoints[2][0],CentrePoints[2][1],CentrePoints[3][0]-30,CentrePoints[3][1]
else:
outOfOneSixEight -= 84
return CentrePoints[2][0],CentrePoints[2][1],CentrePoints[2][0]-quadrantSects[outOfOneSixEight][0],30-quadrantSects[outOfOneSixEight][0]+CentrePoints[2][1]+1
if outOfOneSixEight <= 168:
if outOfOneSixEight == 168:
return CentrePoints[3][0],CentrePoints[3][1],CentrePoints[0][0],CentrePoints[0][1]-30
else:
outOfOneSixEight -= 126
return CentrePoints[3][0],CentrePoints[3][1],CentrePoints[3][0]-quadrantSects[outOfOneSixEight][1],CentrePoints[3][1]-quadrantSects[outOfOneSixEight][0]
def antiError(outOfOneSixEight):
localCoords = getFarCoords(outOfOneSixEight)
arguments = []
if (localCoords[0]-localCoords[2])>=0:
arguments.append(localCoords[2])
length = localCoords[0]-localCoords[2]
else:
arguments.append(localCoords[0])
length = localCoords[2]-localCoords[0]
if (localCoords[1]-localCoords[3])>=0:
arguments.append(localCoords[3])
height = localCoords[1]-localCoords[3]
else:
arguments.append(localCoords[1])
height = localCoords[3]-localCoords[1]
arguments.append(length)
arguments.append(height)
return arguments
def findIdeal(value,valueMax,total):
fraction = (total/valueMax)*value
fraction = int(fraction)
return fraction
def debug(data):
oled.fill(0)
oled.show()
oled.text("stage : "+str(data),0,0)
oled.show()
oled.fill(0)
oled.show()
def prepOutput(time,clockSize,halfDay,weekDay):
if time[0]==0:
second = 60
else:
second = time[0]
if time[1]==0:
minute = 60
else:
minute = time[1]
if time[2]==0:
hour = 12
else:
hour = time[2]
oled.text(halfDay[time[3]],1,1)
oled.text(weekDay[time[4]],1,55)
coordsS = antiError(findIdeal(second,60,clockSize))
oled.rect(coordsS[0],coordsS[1],coordsS[2],coordsS[3],1)
coordsM = antiError(findIdeal(minute,60,clockSize))
oled.fill_rect(coordsM[0],coordsM[1],coordsM[2],coordsM[3],1)
oled.show()
utime.sleep(1)
oled.fill(0)
oled.show()
oled.fill(0)
oled.show()
intialAnimation()
clockSize = 168
time=[0,0,0,0,0]
timeMax = [59,59,23,1,6]
timeChangeSym=["Sec","Min","Hour","Day","Half","Week"]
halfDay = ["A.M.","P.M."]
weekDay = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
while True:
"""
if buttonPush3.value() and buttonPush4.value():
oled.fill(0)
oled.show()
oled.text("+/-",95,1)
menu = 0
while True:
utime.sleep(0.5)
if buttonPush3.value():
if menu == 5:
oled.fill(0)
oled.show()
break
menu+=1
oled.fill(0)
oled.show()
oled.text(timeChangeSym[menu],95,56)
prepOutput(time)
oled.show()
elif buttonPush1.value():
time[menu] += 1
if time[menu]==timeMax[menu]+1:
time[menu]=0
elif buttonPush2.value():
time[menu] -= 1
if time[menu]==-1:
time[menu]=timeMax[menu]
prepOutput(time)
"""
"""
while not(buttonPush3.value()==True and buttonPush4.value()==True):
utime.sleep(1)
"""
#debug(0)
time[0]+=1
#debug(1)
if time[0]==timeMax[0]:
time[1]+=1
time[0]=0
if time[1]==timeMax[1]:
time[2]+=1
time[1]=0
if time[2]==timeMax[2]:
time[3]+=1
time[2]=0
if time[3]==timeMax[3]:
time[3]==0
time[4]+=1
if time[4]==timeMax[4]:
time[4]==0
prepOutput(time,clockSize,halfDay,weekDay)
# Tidy up
oled.fill(0)
oled.show()