# seven_segment_demo.py

# Raspberry Pi Pico - Seven Segment I2C Display demo

# Display a number on a four-digit seven-segment display using an I2C interface.

# This uses a particular product from Adafruit:
#   Adafruit 0.56" 4-Digit 7-Segment Display w/I2C Backpack
#   shop:     https://www.adafruit.com/products/881 (blue version)
#   tutorial: https://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack

# IDeATe stocks a small quantity of these devices in Lending for long-term loan.
# The description can be found in the Physical Computing lab database under part
# numbers 2342, 2343, 2345, 2347, and 2349, corresponding to different digit
# colors.

# The board is based on a HT16K33 I2C LED driver chip.  The default address
# (with no jumpers installed) is 0x70.

#---- Adafruit library ----------------------------------------------------
# This sample uses an Adafruit library which is not included in the firmware,
# but which must be copied to the board.  All of these optional libraries are
# distributed in a bundle which must match the CircuitPython firmware version.

# To install, unzip the bundle zip and locate the lib/adafruit_ht16k33 folder.
# This contains several precompiled .mpy files; the folder should be copied to the
# lib folder on the CIRCUITPY device. 
# 
# E.g., the installed files should include the following:
#   CIRCUITPY/lib/adafruit_ht16k33/segments.mpy

#---- electrical connections ---------------------------------------------------
# The Pico has two hardware I2C ports which can each be mapped to a number of
# possible pin pairs.  This example uses pins 6 and 7 for I2C0 SDA
# and SCL.  For alternate choices please see the official pinout diagram.

# Pico                  display         description
# pin 7/GP5/I2C0 SCL    SCL             I2C clock from port 0
# pin 6/GP4/I2C0 SDA    SDA             I2C data from port 0
# pin 36/3.3VOUT        VCC             3.3V to power display
# pin 38/GND            GND             common ground

################################################################
# related CircuitPython module documentation:

# busio             https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/index.html
# time              https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html
# board             https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html
# adafruit_ht16k33  https://circuitpython.readthedocs.io/projects/ht16k33/en/latest/api.html

# Adafruit_CircuitPython_HT16K33 source: https://github.com/adafruit/Adafruit_CircuitPython_HT16K33

################################################################################
# load standard Python modules
import time

# load the CircuitPython hardware definition module for pin definitions
import board

# load the CircuitPython I2C support
import busio

# load the Adafruit HT16K33 library
import adafruit_ht16k33.segments

#---------------------------------------------------------------
# The device is connected to I2C0 on pins 6 and 7.
i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
display = adafruit_ht16k33.segments.Seg7x4(i2c)

#---------------------------------------------------------------
# Run the main loop to generate a counting display.

while True:
    for count in range(10000):
        display.print("%04d" % count)
        print("%04d" % count)
        time.sleep(1)
        
