#!/bin/bash
# SparkFun Qwiic Alphanumeric Display (VK16K33-based)
#
# * VK16K33 based, clone of HT16K33
#   * https://cdn.sparkfun.com/assets/c/8/7/2/5/VK16K33Datasheet.pdf
#   * https://www.szvinka.com/uploadfile/Datasheet/LED/VK16K33/VK16K33_V1.2-EN.pdf
#   * https://cdn-shop.adafruit.com/datasheets/ht16K33v110.pdf
#
# * Default I2C address is 0x70
#   * Can be configured 0x71/0x72/0x73 with jumpers
# * Device oscillator must be enabled by short-write (s-w) to addr. 0x21.
#   * Sleep device by s-w 0x20. No changes possible until wake-up.
# * Display On/Off is bit 0 in 0x80 range
#   * Off: s-w 0x80
#   *  On: s-w 0x81
# * Blink is bits 1 and 2 (OR with bit 0 above):
#   * On,  2.0Hz: s-w 0x83
#   * On,  1.0Hz: s-w 0x85
#   * On,  0.5Hz: s-w 0x87
#   * On, steady: s-w 0x81
#   * Clear LSB to change blink while keeping display off.
# * Brightness: s-w between E0 (dimmest) and EF (brightest)
# * Character bit patterns:
# ```
#            a
#      ============= 
#     | \    |    / |
#     |  \  i|   /  |
#   f |  h\  |  /j  | b
#     |    \ | /    |
#     |  g1  |  g2  |
#      ====== ====== 
#     |      |      |
#     |    / | \    |
#   e |  m/  |  \k  | c
#     |  /  L|   \  |
#     | /    |    \ |
#      ============= 
#            d
# ```
# 
#  |=====|=======|=======|
#  | I2C | Value | Value |
#  | Reg |  MSB  |  LSB  |
#  |=====|=======|=======|
#  | 0x0 |   g2  |   a   |
#  | 0x2 |    h  |   b   |
#  | 0x4 |    i  |   c   |
#  | 0x6 |    j  |   d   |
#  | 0x8 |    k  |   e   |
#  | 0xa |    L  |   f   |
#  | 0xc |    m  |  g1   |
#  |=====|=======|=======|
#
# Within each nibble, the MSB is the left-most character.
#
# Two other addresses exist:
# * colon (between char. 2 and 3):         addr 0x1, bit 0 (LSB)
# * decimal point (between char. 3 and 4): addr 0x3, bit 0 (LSB)

# First, set up the display.
i2cset -y 2 0x70 0x21      # oscillator on
i2cset -y 2 0x70 0x80      # no blink, display off
i2cset -y 2 0x70 0xEF      # full brightness

# Spell out MFME while display is off.
i2cset -y 2 0x70 0x0 0x8a
i2cset -y 2 0x70 0x2 0x55
i2cset -y 2 0x70 0x4 0x05
i2cset -y 2 0x70 0x6 0x58
i2cset -y 2 0x70 0x8 0x0f
i2cset -y 2 0x70 0xa 0x0f
i2cset -y 2 0x70 0xc 0x0a

echo "Hold joystick button to show message. ^C to exit."

while true; do
  BUTTON=`i2cget -y 2 0x20 0x7`
  if [[ $BUTTON == "0x01" ]]
  then
    i2cset -y 2 0x70 0x80 # display off
  else
    i2cset -y 2 0x70 0x81 # display on, no blink
  fi
done
