"""
07_oled_test.py
First year hardware project
School of ICT
Metropolia University of Applied Sciences
23.1.2023, Sakari Lukkarinen
This demo
- writes text and draws a rectangle onto the OLED
- uses I2C to control OLED
- uses ssd1306 library
Notes
- You need to install ssd1306 library in Thonny: 
      (Tools > Manage Packages, Search: ssd1306, Install)
- This simulation is based on the instructions and model 
  provided by TechnicalUpdate, see:
    https://www.youtube.com/watch?v=TsakxMANaBQ
    https://wokwi.com/projects/320405630824219218
- The SSD1306 simulation component was changed to the newer 
  version: board-ssd1306  
- To learn how to use the display, please read:
    https://docs.micropython.org/en/latest/esp8266/tutorial/ssd1306.html
"""
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import utime
# Pins for OLED
OLED_SDA = 14 # Data
OLED_SCL = 15 # Clock
# Initialize I2C to use OLED
i2c = I2C(1, scl=Pin(OLED_SCL), sda=Pin(OLED_SDA), freq=400000)
OLED_WIDTH = 128
OLED_HEIGHT = 64
oled = SSD1306_I2C(OLED_WIDTH, OLED_HEIGHT, i2c)
# Wait for a second
utime.sleep(1)
# Fill with light
oled.fill(1)
oled.show()
# Wait for a second
utime.sleep(1)
# Clear the display
oled.fill(0)
# Write text
oled.text('Hardware project', 1, 1)
oled.text('School of ICT', 1, 11)
oled.text('Metropolia UAS', 1, 21)
oled.text('23.1.2023', 1, 31)
#oled.text('----------------', 1, 41)
oled.text('123456789-123456', 1, 48)
# Draw rectangles
oled.rect(0, 41, 128, 23, 1)
oled.rect(0, 42, 128, 21, 1)
oled.show()
# Repeat 10 times
for n in range(10):
    # Wait for 2 seconds
    utime.sleep(2)
    # Invert display
    oled.invert((n+1) % 2)
# Scroll up
for n in range(13):
  oled.scroll(0, -5)
  oled.fill_rect(0,59, 128, 63, 0)
  oled.show()
  utime.sleep(0.1)
# Turn off
oled.poweroff()