#!/usr/bin/env python
#-----------------------------------------------------------------------------
# ex1_bitmap_bender.py
#
# Simple Example for the Qwiic Micro OLED display
#------------------------------------------------------------------------
#
# Written by  SparkFun Electronics, May 2021
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https:# www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2021 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 1 - Simple example to display a bitmap on the Qwiic Micro OLED board.
#

from __future__ import print_function
import qwiic_oled_base
import time
import sys


bender = [ \
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xBF, 0xDF, 0x5F, 0x5F, 0x5F, 0x5F,\
  0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
]

def runExample():

  #  These three lines of code are all you need to initialize the
  #  OLED and print the splash screen.

  #  Before you can start using the OLED, call begin() to init
  #  all of the pins and configure the OLED.


  print("\nSparkFun Micro OLED Bitmap Example\n")
  myOLED = qwiic_oled_base.QwiicOledBase()
  #  By default, the superclass/base Python package loads the parameters 
  #  for the Qwiic Micro OLED board.

  if not myOLED.connected:
    print("The Qwiic Micro OLED device isn't connected to the system. Please check your connection", \
      file=sys.stderr)
    return

  myOLED.begin()

  #  clear(ALL) will clear out the OLED's graphic memory.
  myOLED.clear(myOLED.ALL) #  Clear the display's memory (gets rid of artifacts)
  
  #  Display buffer contents
  myOLED.display()
  time.sleep(3)

  #  clear(PAGE) will clear the Arduino's display buffer.
  myOLED.clear(myOLED.PAGE)  #  Clear the display's buffer

  #  Display buffer contents
  myOLED.display()
  time.sleep(3)

  #  Draw Bitmap
  #  ---------------------------------------------------------------------------
  #  Add bitmap to buffer
  myOLED.draw_bitmap(bender)

  #  To actually draw anything on the display, you must call the display() function.
  myOLED.display()

if __name__ == '__main__':
  try:
    runExample()
  except (KeyboardInterrupt, SystemExit) as exErr:
    print("\nEnding OLED bitmap Example")
    sys.exit(0)
