Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

oled7segment.py

A lightweight 7 segment character generator that allows cutomizable sizing to fit small OLED displays.

Sample Code

Hardware:

All of these tests were run on a HiLetgo ESP32 OLED WiFi Kit ESP-32 using a Thonny editor on a Raspberry 400.

Set up:

Used with all examples:

importsysimportmachinefrommachineimportPin, SoftI2Cimportssd1306importgfximportutimeastimefromoled7segmentimport*i2c=SoftI2C(scl=Pin(15), sda=Pin(4))
pin=Pin(16, Pin.OUT)
pin.value(0) #set GPIO16 low to reset OLEDpin.value(1) #while OLED is running, must set GPIO16 in higholed_width=128oled_height=64oled=ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
graphic=gfx.GFX (oled_width, oled_height, oled.pixel)
seven_segment=OLED7Segment (graphic)
d_t=time.localtime ()
date_str='{:02d}-{:02d}'.format (d_t[1], d_t[2])
time_str='{:02d}:{:02d}'.format (d_t[3], d_t[4])
Small segments
xpos=0ypos=0seven_segment.set_parameters (digit_size="S", spacing=2)
oled.fill (0)
seven_segment.display_string (xpos, ypos, "01234567890")
ypos+=seven_segment.get_character_height () seven_segment.display_string (xpos, ypos, "ABCDEF")
ypos+=seven_segment.get_character_height ()
seven_segment.display_string (xpos, ypos, date_str)
ypos+=seven_segment.get_character_height ()
seven_segment.display_string (xpos, ypos, time_str)
ypos+=seven_segment.get_character_height ()
oled.show ()

Image From code above

Line 1: digits only
Line 2: hexidecimal characters
Line 3: Date with dash (minus sign)
Line 4: Time with colon
Large segments
xpos=0ypos=0seven_segment.set_parameters (digit_size="L")
oled.fill (0)
xpos+=seven_segment.display_string (xpos, ypos, "8")
seven_segment.set_parameters (bold=True)
seven_segment.display_string (xpos, ypos, "76")
oled.show ()

Image From code above

1st character, Bold=False
2nd, 3rd characters, Bold=True
Custom segment sizes
xpos=0ypos=0oled.fill (0)
seven_segment.set_parameters (digit_size="M", spacing=2)
char_height=seven_segment.get_character_height () # save tallest segxpos+=seven_segment.display_string (xpos, ypos, "10")
seven_segment.set_parameters (v_segment_length=4, h_segment_length=4)
seven_segment.display_string (xpos, ypos, "95")
xpos=0ypos+=char_height+2# plus extra spacingseven_segment.set_parameters (bold=True,
spacing=2,
segment_width=2,
v_segment_length=8,
h_segment_length=3)
seven_segment.display_string (xpos, ypos, "87.6")
oled.show ()

Image From code above

line 1: Changes the segment lengths
Line 2: Tall, narrow segment, usefull for displaying a lot of digits
Custom segments
xpos=0ypos=0seven_segment.set_parameters (digit_size="L",bold=True)
oled.fill (0)
seven_segment.TOP_seg (xpos, ypos)
seven_segment.MID_seg (xpos, ypos)
seven_segment.BOT_seg (xpos, ypos)
xpos+=40seven_segment.UR_seg (xpos, ypos)
seven_segment.MID_seg (xpos, ypos)
seven_segment.LL_seg (xpos, ypos)
xpos+=40seven_segment.UR_seg (xpos, ypos)
seven_segment.UL_seg (xpos, ypos)
seven_segment.LR_seg (xpos, ypos)
seven_segment.LL_seg (xpos, ypos)
oled.show ()

Image From code above

You can create custom symbols (Klingon?) by calling the individual segment functions.
Segment positioning is manual.
Busy/Waiting indicator
xpos=0ypos=0seven_segment.set_parameters (digit_size="S",bold=False)
oled.fill (0)
foriinrange (0, 10) : # repeat 10 timesforiterationinrange (1, 5) : # 4 different iterationsifiteration==1 :
seven_segment.UL_seg (xpos, ypos, 0)
seven_segment.TOP_seg (xpos, ypos)
seven_segment.UR_seg (xpos, ypos)
elifiteration==2 :
seven_segment.TOP_seg (xpos, ypos, 0)
seven_segment.UR_seg (xpos, ypos)
seven_segment.MID_seg (xpos, ypos)
elifiteration==3 :
seven_segment.UR_seg (xpos, ypos, 0)
seven_segment.MID_seg (xpos, ypos)
seven_segment.UL_seg (xpos, ypos)
elifiteration==4 :
seven_segment.MID_seg (xpos, ypos, 0)
seven_segment.UL_seg (xpos, ypos)
seven_segment.TOP_seg (xpos, ypos)
oled.show ()
time.sleep (0.1)
seven_segment.TOP_seg (xpos, ypos) # doneseven_segment.UR_seg (xpos, ypos)
seven_segment.MID_seg (xpos, ypos)
seven_segment.UL_seg (xpos, ypos)
oled.show ()

Couldn't provide a video. The segments rotate 10 times

Shows that something is happening

About

Simple 7 segment emulator for oled display

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages