<commit_before>#----------------------------------------------------------------------
# ssd1351.py from https://github.com/boxysean/py-gaugette
# ported by boxysean
# modified to work with BBB and Adafruit_BBIO and *not* gaugette by AKA
#
# This library works with 
#   Adafruit's 128x128 SPI RGB OLED   https://www.adafruit.com/products/1431
#
# The code is based heavily on Adafruit's Arduino library
#   https://github.com/adafruit/Adafruit-SSD1351-library
# written by <NAME>/Ladyada for Adafruit Industries.
#----------------------------------------------------------------------
from __future__ import division
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.SPI as SPI
import font5x8
import time
import sys

class SSD1351:
    
    DELAYS_HWFILL = 3
    DELAYS_HWLINE = 1

    # SSD1351 Commands
    CMD_SETCOLUMN          = 0x15
    CMD_SETROW             = 0x75
    CMD_WRITERAM           = 0x5C
    CMD_READRAM            = 0x5D
    CMD_SETREMAP           = 0xA0
    CMD_STARTLINE          = 0xA1
    CMD_DISPLAYOFFSET      = 0xA2
    CMD_DISPLAYALLOFF      = 0xA4
    CMD_DISPLAYALLON       = 0xA5
    CMD_NORMALDISPLAY      = 0xA6
    CMD_INVERTDISPLAY      = 0xA7
    CMD_FUNCTIONSELECT     = 0xAB
    CMD_DISPLAYOFF         = 0xAE
    CMD_DISPLAYON          = 0xAF
    COM_SCAN_INC           = 0xC0
    COM_SCAN_DEC           = 0xC8
    CMD_PRECHARGE          = 0xB1
    CMD_DISPLAYENHANCE     = 0xB2
    CMD_CLOCKDIV           = 0xB3
    CMD_SETVSL             = 0xB4
    CMD_SETGPIO            = 0xB5
    CMD_PRECHARGE2         = 0xB6
    CMD_SETGRAY            = 0xB8
    CMD_USELUT             = 0xB9
    CMD_PRECHARGELEVEL     = 0xBB
    CMD_VCOMH              = 0xBE
    CMD_CONTRASTABC        = 0xC1
    CMD_CONTRASTMASTER     = 0xC7
    CMD_MUXRATIO           = 0xCA
    CMD_COMMANDLOCK        = 0xFD
    CMD_HORIZSCROLL        = 0x96
    CMD_STOPSCROLL         = 0x9E
    CMD_STARTSCROLL        = 0x9F
    SET_COM_PINS           = 0xDA

    SSD1351WIDTH           = 128
    SSD1351HEIGHT          = 128
    '''
    # the below are ssd1306 commands
    EXTERNAL_VCC   = 0x1
    SWITCH_CAP_VCC = 0x2
        
    SET_LOW_COLUMN        = 0x00
    SET_HIGH_COLUMN       = 0x10
    SET_MEMORY_MODE       = 0x20
    SET_COL_ADDRESS       = 0x21
    SET_PAGE_ADDRESS      = 0x22
    RIGHT_HORIZ_SCROLL    = 0x26
    LEFT_HORIZ_SCROLL     = 0x27
    VERT_AND_RIGHT_HORIZ_SCROLL = 0x29
    VERT_AND_LEFT_HORIZ_SCROLL = 0x2A
    DEACTIVATE_SCROLL     = 0x2E
    ACTIVATE_SCROLL       = 0x2F
    SET_START_LINE        = 0x40
    SET_CONTRAST          = 0x81
    CHARGE_PUMP           = 0x8D
    SEG_REMAP             = 0xA0
    SET_VERT_SCROLL_AREA  = 0xA3
    DISPLAY_ALL_ON_RESUME = 0xA4
    DISPLAY_ALL_ON        = 0xA5
    NORMAL_DISPLAY        = 0xA6
    INVERT_DISPLAY        = 0xA7
    DISPLAY_OFF           = 0xAE
    DISPLAY_ON            = 0xAF
    COM_SCAN_INC          = 0xC0
    COM_SCAN_DEC          = 0xC8
    SET_DISPLAY_OFFSET    = 0xD3
    SET_COM_PINS          = 0xDA
    SET_VCOM_DETECT       = 0xDB
    SET_DISPLAY_CLOCK_DIV = 0xD5
    SET_PRECHARGE         = 0xD9
    SET_MULTIPLEX         = 0xA8

    MEMORY_MODE_HORIZ = 0x00
    MEMORY_MODE_VERT  = 0x01
    MEMORY_MODE_PAGE  = 0x02
    '''
    # Device name will be /dev/spidev-{bus}.{device}
    # dc_pin is the data/commmand pin.  This line is HIGH for data, LOW for command.
    # We will keep d/c low and bump it high only for commands with data
    # reset is normally HIGH, and pulled LOW to reset the display

    def __init__(self, bus=0, device=0, dc_pin="P9_15", reset_pin="P9_13", buffer_rows=128, buffer_cols=128, rows=128, cols=128):
        self.cols = cols
        self.rows = rows
        self.buffer_rows = buffer_rows
        self.mem_bytes = self.buffer_rows * self.cols / 8 # total bytes in SSD1306 display ram
        self.dc_pin = dc_pin
        self.reset_pin = reset_pin
        self.spi = SPI.SPI(bus, device)
        self.spi.mode = 3 # necessary!
        self.gpio = GPIO
        self.gpio.setup(self.reset_pin, self.gpio.OUT)
        self.gpio.output(self.reset_pin, self.gpio.HIGH)
        self.gpio.setup(self.dc_pin, self.gpio.OUT)
        self.gpio.output(self.dc_pin, self.gpio.LOW)
        self.font = font5x8.Font5x8
        self.col_offset = 0
        self.bitmap = self.SimpleBitmap(buffer_cols, buffer_rows)
        self.flipped = False

    def reset(self):
        self.gpio.output(self.reset_pin, self.gpio.LOW)
        time.sleep(0.010) # 10ms
        self.gpio.output(self.reset_pin, self.gpio.HIGH)

    def command(self, cmd, cmddata=None):
        # already low
        #self.gpio.output(self.dc_pin, self.gpio.LOW)

        if type(cmd) == list:
            self.spi.writebytes(cmd)
        else:
            self.spi.writebytes([cmd])

        if cmddata != None:
            if type(cmddata) == list:
                self.data(cmddata)
            else:
                self.data([cmddata])

    def data(self, bytes):
        self.gpio.output(self.dc_pin, self.gpio.HIGH)
        #  chunk data to work around 255 byte limitation in adafruit implementation of writebytes
        # revisit - change to 1024 when Adafruit_BBIO is fixed.
        # max_xfer = 255 if gaugette.platform == 'beaglebone' else 1024
        # max_xfer = 1024 # whatever BBIO error existed seems fine now
        max_xfer = 255
        start = 0
        remaining = len(bytes)
        while remaining>0:
            count = remaining if remaining <= max_xfer else max_xfer
            remaining -= count
            self.spi.writebytes(bytes[start:start+count])
            start += count
        self.gpio.output(self.dc_pin, self.gpio.LOW)
        
    def begin(self):
        time.sleep(0.001) # 1ms
        self.reset()

        self.command(self.CMD_COMMANDLOCK, 0x12) # Unlock OLED driver IC MCU interface from entering command
        self.command(self.CMD_COMMANDLOCK, 0xB1) # Command A2,B1,B3,BB,BE,C1 accessible if in unlock state 
        self.command(self.CMD_DISPLAYOFF)
        self.command([self.CMD_CLOCKDIV, 0xF1]) # 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
        self.command(self.CMD_MUXRATIO, 127)
        self.command(self.CMD_SETREMAP, 0x74)
        self.command(self.CMD_SETCOLUMN, [0x00, 0x7F])
        self.command(self.CMD_SETROW, [0x00, 0x7F])
        self.command(self.CMD_STARTLINE, 0x00)
        self.command(self.CMD_DISPLAYOFFSET, 0x00)
        self.command(self.CMD_SETGPIO, 0x00)
        self.command(self.CMD_FUNCTIONSELECT, 0x01)
        self.command([self.CMD_PRECHARGE, 0x32])
        self.command([self.CMD_VCOMH, 0x05])
        self.command(self.CMD_NORMALDISPLAY)
        self.command(self.CMD_CONTRASTABC, [0xC8, 0x80, 0xC8])
        self.command(self.CMD_CONTRASTMASTER, 0x0F)
        self.command(self.CMD_SETVSL, [0xA0, 0xB5, 0x55])
        self.command(self.CMD_PRECHARGE2, 0x01)
        self.command(self.CMD_DISPLAYON)
        
    def clear_display(self):
        self.bitmap.clear()

    def invert_display(self):
        self.command(self.CMD_INVERTDISPLAY)

    def flip_display(self, flipped=True):
        self.flipped = flipped
        if flipped:
            self.command(self.COM_SCAN_INC)
            self.command(self.CMD_SETREMAP, 0x8B)
        else:
            self.command(self.COM_SCAN_DEC)
            self.command(self.SET_COM_PINS, 0x02)

    def normal_display(self):
        self.command(self.CMD_NORMALDISPLAY)

    def set_contrast(self, contrast=0x7f):
        self.command(self.SET_CONTRAST, contrast)

    def goTo(self, x, y):
        if x >= self.SSD1351WIDTH or y >= self.SSD1351HEIGHT:
            return
  
        # set x and y coordinate
        self.command(self.CMD_SETCOLUMN, [x, self.SSD1351WIDTH-1])
        self.command(self.CMD_SETROW, [y, self.SSD1351HEIGHT-1])
        self.command(self.CMD_WRITERAM)

    def scale(self, x, inLow, inHigh, outLow, outHigh):<commit_msg>trying to get flipping working<commit_after>#----------------------------------------------------------------------
# ssd1351.py from https://github.com/boxysean/py-gaugette
# ported by boxysean
# modified to work with BBB and Adafruit_BBIO and *not* gaugette by AKA
#
# This library works with 
#   Adafruit's 128x128 SPI RGB OLED   https://www.adafruit.com/products/1431
#
# The code is based heavily on Adafruit's Arduino library
#   https://github.com/adafruit/Adafruit-SSD1351-library
# written by <NAME>/Ladyada for Adafruit Industries.
#----------------------------------------------------------------------
from __future__ import division
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.SPI as SPI
import font5x8
import time
import sys

class SSD1351:
    
    DELAYS_HWFILL = 3
    DELAYS_HWLINE = 1

    # SSD1351 Commands
    CMD_SETCOLUMN          = 0x15
    CMD_SETROW             = 0x75
    CMD_WRITERAM           = 0x5C
    CMD_READRAM            = 0x5D
    CMD_SETREMAP           = 0xA0
    CMD_STARTLINE          = 0xA1
    CMD_DISPLAYOFFSET      = 0xA2
    CMD_DISPLAYALLOFF      = 0xA4
    CMD_DISPLAYALLON       = 0xA5
    CMD_NORMALDISPLAY      = 0xA6
    CMD_INVERTDISPLAY      = 0xA7
    CMD_FUNCTIONSELECT     = 0xAB
    CMD_DISPLAYOFF         = 0xAE
    CMD_DISPLAYON          = 0xAF
    COM_SCAN_INC           = 0xC0
    COM_SCAN_DEC           = 0xC8
    CMD_PRECHARGE          = 0xB1
    CMD_DISPLAYENHANCE     = 0xB2
    CMD_CLOCKDIV           = 0xB3
    CMD_SETVSL             = 0xB4
    CMD_SETGPIO            = 0xB5
    CMD_PRECHARGE2         = 0xB6
    CMD_SETGRAY            = 0xB8
    CMD_USELUT             = 0xB9
    CMD_PRECHARGELEVEL     = 0xBB
    CMD_VCOMH              = 0xBE
    CMD_CONTRASTABC        = 0xC1
    CMD_CONTRASTMASTER     = 0xC7
    CMD_MUXRATIO           = 0xCA
    CMD_COMMANDLOCK        = 0xFD
    CMD_HORIZSCROLL        = 0x96
    CMD_STOPSCROLL         = 0x9E
    CMD_STARTSCROLL        = 0x9F
    SET_COM_PINS           = 0xDA

    SSD1351WIDTH           = 128
    SSD1351HEIGHT          = 128
    '''
    # the below are ssd1306 commands
    EXTERNAL_VCC   = 0x1
    SWITCH_CAP_VCC = 0x2
        
    SET_LOW_COLUMN        = 0x00
    SET_HIGH_COLUMN       = 0x10
    SET_MEMORY_MODE       = 0x20
    SET_COL_ADDRESS       = 0x21
    SET_PAGE_ADDRESS      = 0x22
    RIGHT_HORIZ_SCROLL    = 0x26
    LEFT_HORIZ_SCROLL     = 0x27
    VERT_AND_RIGHT_HORIZ_SCROLL = 0x29
    VERT_AND_LEFT_HORIZ_SCROLL = 0x2A
    DEACTIVATE_SCROLL     = 0x2E
    ACTIVATE_SCROLL       = 0x2F
    SET_START_LINE        = 0x40
    SET_CONTRAST          = 0x81
    CHARGE_PUMP           = 0x8D
    SEG_REMAP             = 0xA0
    SET_VERT_SCROLL_AREA  = 0xA3
    DISPLAY_ALL_ON_RESUME = 0xA4
    DISPLAY_ALL_ON        = 0xA5
    NORMAL_DISPLAY        = 0xA6
    INVERT_DISPLAY        = 0xA7
    DISPLAY_OFF           = 0xAE
    DISPLAY_ON            = 0xAF
    COM_SCAN_INC          = 0xC0
    COM_SCAN_DEC          = 0xC8
    SET_DISPLAY_OFFSET    = 0xD3
    SET_COM_PINS          = 0xDA
    SET_VCOM_DETECT       = 0xDB
    SET_DISPLAY_CLOCK_DIV = 0xD5
    SET_PRECHARGE         = 0xD9
    SET_MULTIPLEX         = 0xA8

    MEMORY_MODE_HORIZ = 0x00
    MEMORY_MODE_VERT  = 0x01
    MEMORY_MODE_PAGE  = 0x02
    '''
    # Device name will be /dev/spidev-{bus}.{device}
    # dc_pin is the data/commmand pin.  This line is HIGH for data, LOW for command.
    # We will keep d/c low and bump it high only for commands with data
    # reset is normally HIGH, and pulled LOW to reset the display

    def __init__(self, bus=0, device=0, dc_pin="P9_15", reset_pin="P9_13", buffer_rows=128, buffer_cols=128, rows=128, cols=128):
        self.cols = cols
        self.rows = rows
        self.buffer_rows = buffer_rows
        self.mem_bytes = self.buffer_rows * self.cols / 8 # total bytes in SSD1306 display ram
        self.dc_pin = dc_pin
        self.reset_pin = reset_pin
        self.spi = SPI.SPI(bus, device)
        self.spi.mode = 3 # necessary!
        self.gpio = GPIO
        self.gpio.setup(self.reset_pin, self.gpio.OUT)
        self.gpio.output(self.reset_pin, self.gpio.HIGH)
        self.gpio.setup(self.dc_pin, self.gpio.OUT)
        self.gpio.output(self.dc_pin, self.gpio.LOW)
        self.font = font5x8.Font5x8
        self.col_offset = 0
        self.bitmap = self.SimpleBitmap(buffer_cols, buffer_rows)
        self.flipped = False

    def reset(self):
        self.gpio.output(self.reset_pin, self.gpio.LOW)
        time.sleep(0.010) # 10ms
        self.gpio.output(self.reset_pin, self.gpio.HIGH)

    def command(self, cmd, cmddata=None):
        # already low
        #self.gpio.output(self.dc_pin, self.gpio.LOW)

        if type(cmd) == list:
            self.spi.writebytes(cmd)
        else:
            self.spi.writebytes([cmd])

        if cmddata != None:
            if type(cmddata) == list:
                self.data(cmddata)
            else:
                self.data([cmddata])

    def data(self, bytes):
        self.gpio.output(self.dc_pin, self.gpio.HIGH)
        #  chunk data to work around 255 byte limitation in adafruit implementation of writebytes
        # revisit - change to 1024 when Adafruit_BBIO is fixed.
        # max_xfer = 255 if gaugette.platform == 'beaglebone' else 1024
        # max_xfer = 1024 # whatever BBIO error existed seems fine now
        max_xfer = 255
        start = 0
        remaining = len(bytes)
        while remaining>0:
            count = remaining if remaining <= max_xfer else max_xfer
            remaining -= count
            self.spi.writebytes(bytes[start:start+count])
            start += count
        self.gpio.output(self.dc_pin, self.gpio.LOW)
        
    def begin(self):
        time.sleep(0.001) # 1ms
        self.reset()

        self.command(self.CMD_COMMANDLOCK, 0x12) # Unlock OLED driver IC MCU interface from entering command
        self.command(self.CMD_COMMANDLOCK, 0xB1) # Command A2,B1,B3,BB,BE,C1 accessible if in unlock state 
        self.command(self.CMD_DISPLAYOFF)
        self.command([self.CMD_CLOCKDIV, 0xF1]) # 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
        self.command(self.CMD_MUXRATIO, 127)
        self.command(self.CMD_SETREMAP, 0x74)
        self.command(self.CMD_SETCOLUMN, [0x00, 0x7F])
        self.command(self.CMD_SETROW, [0x00, 0x7F])
        self.command(self.CMD_STARTLINE, 0x00)
        self.command(self.CMD_DISPLAYOFFSET, 0x00)
        self.command(self.CMD_SETGPIO, 0x00)
        self.command(self.CMD_FUNCTIONSELECT, 0x01)
        self.command([self.CMD_PRECHARGE, 0x32])
        self.command([self.CMD_VCOMH, 0x05])
        self.command(self.CMD_NORMALDISPLAY)
        self.command(self.CMD_CONTRASTABC, [0xC8, 0x80, 0xC8])
        self.command(self.CMD_CONTRASTMASTER, 0x0F)
        self.command(self.CMD_SETVSL, [0xA0, 0xB5, 0x55])
        self.command(self.CMD_PRECHARGE2, 0x01)
        self.command(self.CMD_DISPLAYON)
        
    def clear_display(self):
        self.bitmap.clear()

    def invert_display(self):
        self.command(self.CMD_INVERTDISPLAY)

    def flip_display(self, flipped=True):
        self.flipped = flipped
        if flipped:
            self.command(self.COM_SCAN_INC)
            self.command(self.CMD_SETREMAP, 0xA3)
        else:
            self.command(self.COM_SCAN_DEC)
            self.command(self.SET_COM_PINS, 0x02)

    def normal_display(self):
        self.command(self.CMD_NORMALDISPLAY)

    def set_contrast(self, contrast=0x7f):
        self.command(self.SET_CONTRAST, contrast)

    def goTo(self, x, y):
        if x >= self.SSD1351WIDTH or y >= self.SSD1351HEIGHT:
            return
  
        # set x and y coordinate
        self.command(self.CMD_SETCOLUMN, [x, self.SSD1351WIDTH-1])
        self.command(self.CMD_SETROW, [y, self.SSD1351HEIGHT-1])
        self.command(self.CMD_WRITERAM)

    def scale(self, x, inLow, inHigh, outLow, outHigh):
{'fraction_non_alphanumeric': 0.09704934605421116, 'fraction_numerical': 0.04498641561887913, 'mean_word_length': 1.9646001123805956, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '188876', 'n_tokens_mistral': 6122, 'n_tokens_neox': 5522, 'n_words': 1089}