From 786564ebb86f21aa90261f3d9d19d9a8a11a2f02 Mon Sep 17 00:00:00 2001
From: Nikhil Jha <hi@nikhiljha.com>
Date: Sun, 27 Oct 2019 12:47:15 -0700
Subject: [PATCH 01/12] add waveshare 213d display

Signed-off-by: Nikhil Jha <hi@nikhiljha.com>
---
 pwnagotchi/ui/display.py                      |   3 +
 pwnagotchi/ui/hw/__init__.py                  |   4 +
 .../ui/hw/libs/waveshare/v213d/epd2in13d.py   | 358 ++++++++++++++++++
 .../ui/hw/libs/waveshare/v213d/epdconfig.py   | 154 ++++++++
 pwnagotchi/ui/hw/waveshare213d.py             |  69 ++++
 pwnagotchi/utils.py                           |   3 +
 6 files changed, 591 insertions(+)
 create mode 100644 pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py
 create mode 100644 pwnagotchi/ui/hw/libs/waveshare/v213d/epdconfig.py
 create mode 100644 pwnagotchi/ui/hw/waveshare213d.py

diff --git a/pwnagotchi/ui/display.py b/pwnagotchi/ui/display.py
index b0ff106..25aed67 100644
--- a/pwnagotchi/ui/display.py
+++ b/pwnagotchi/ui/display.py
@@ -54,6 +54,9 @@ class Display(View):
     def is_waveshare154inch(self):
         return self._implementation.name == 'waveshare154inch'
 
+    def is_waveshare213d(self):
+        return self._implementation.name == 'waveshare213d'
+
     def is_waveshare_any(self):
         return self.is_waveshare_v1() or self.is_waveshare_v2()
 
diff --git a/pwnagotchi/ui/hw/__init__.py b/pwnagotchi/ui/hw/__init__.py
index 3be215f..b7db3bd 100644
--- a/pwnagotchi/ui/hw/__init__.py
+++ b/pwnagotchi/ui/hw/__init__.py
@@ -7,6 +7,7 @@ from pwnagotchi.ui.hw.waveshare1 import WaveshareV1
 from pwnagotchi.ui.hw.waveshare2 import WaveshareV2
 from pwnagotchi.ui.hw.waveshare27inch import Waveshare27inch
 from pwnagotchi.ui.hw.waveshare154inch import Waveshare154inch
+from pwnagotchi.ui.hw.waveshare213d import Waveshare213d
 
 
 def display_for(config):
@@ -37,3 +38,6 @@ def display_for(config):
 
     elif config['ui']['display']['type'] == 'waveshare154inch':
         return Waveshare154inch(config)
+
+    elif config['ui']['display']['type'] == 'waveshare213d':
+        return Waveshare213d(config)
\ No newline at end of file
diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py b/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py
new file mode 100644
index 0000000..de02f86
--- /dev/null
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py
@@ -0,0 +1,358 @@
+# *****************************************************************************
+# * | File        :	  epd2in13d.py
+# * | Author      :   Waveshare team
+# * | Function    :   Electronic paper driver
+# * | Info        :
+# *----------------
+# * | This version:   V4.0
+# * | Date        :   2019-06-20
+# # | Info        :   python demo
+# -----------------------------------------------------------------------------
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documnetation 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
+# furished 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 OR 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.
+#
+
+
+import logging
+from . import epdconfig
+from PIL import Image
+import RPi.GPIO as GPIO
+
+# Display resolution
+EPD_WIDTH       = 104
+EPD_HEIGHT      = 212
+
+class EPD:
+    def __init__(self):
+        self.reset_pin = epdconfig.RST_PIN
+        self.dc_pin = epdconfig.DC_PIN
+        self.busy_pin = epdconfig.BUSY_PIN
+        self.cs_pin = epdconfig.CS_PIN
+        self.width = EPD_WIDTH
+        self.height = EPD_HEIGHT
+
+    lut_vcomDC = [  
+        0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00,
+    ]
+
+    lut_ww = [  
+        0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+
+    lut_bw = [  
+        0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
+        0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
+        0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+
+    lut_wb = [
+        0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+
+    lut_bb = [ 
+        0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+    
+    lut_vcom1 = [  
+        0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00,
+    ]
+
+    lut_ww1 = [  
+        0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+
+    lut_bw1 = [  
+        0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+
+    lut_wb1 = [
+        0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+
+    lut_bb1 = [ 
+        0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ]
+        
+    # Hardware reset
+    def reset(self):
+        epdconfig.digital_write(self.reset_pin, 1)
+        epdconfig.delay_ms(200) 
+        epdconfig.digital_write(self.reset_pin, 0)
+        epdconfig.delay_ms(10)
+        epdconfig.digital_write(self.reset_pin, 1)
+        epdconfig.delay_ms(200)   
+
+    def send_command(self, command):
+        epdconfig.digital_write(self.dc_pin, 0)
+        epdconfig.digital_write(self.cs_pin, 0)
+        epdconfig.spi_writebyte([command])
+        epdconfig.digital_write(self.cs_pin, 1)
+
+    def send_data(self, data):
+        epdconfig.digital_write(self.dc_pin, 1)
+        epdconfig.digital_write(self.cs_pin, 0)
+        epdconfig.spi_writebyte([data])
+        epdconfig.digital_write(self.cs_pin, 1)
+        
+    def ReadBusy(self):
+        logging.debug("e-Paper busy")
+        while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
+            self.send_command(0x71)
+            epdconfig.delay_ms(100)  
+        logging.debug("e-Paper busy release")
+        
+    def TurnOnDisplay(self):
+        self.send_command(0x12)
+        epdconfig.delay_ms(10)
+        self.ReadBusy()
+        
+    def init(self):
+        if (epdconfig.module_init() != 0):
+            return -1
+        # EPD hardware init start
+        self.reset()
+        
+        self.send_command(0x01)	# POWER SETTING
+        self.send_data(0x03)
+        self.send_data(0x00)
+        self.send_data(0x2b)
+        self.send_data(0x2b)
+        self.send_data(0x03)
+
+        self.send_command(0x06)	# boost soft start
+        self.send_data(0x17) # A
+        self.send_data(0x17) # B
+        self.send_data(0x17) # C
+
+        self.send_command(0x04)
+        self.ReadBusy()
+
+        self.send_command(0x00)	# panel setting
+        self.send_data(0xbf) # LUT from OTP,128x296
+        self.send_data(0x0d) # VCOM to 0V fast
+
+        self.send_command(0x30)	# PLL setting
+        self.send_data(0x3a) # 3a 100HZ   29 150Hz 39 200HZ	31 171HZ
+
+        self.send_command(0x61)	# resolution setting
+        self.send_data(self.width)
+        self.send_data((self.height >> 8) & 0xff)
+        self.send_data(self.height& 0xff)
+
+        self.send_command(0x82)	# vcom_DC setting
+        self.send_data(0x28)
+        return 0
+        
+    def SetFullReg(self):
+        self.send_command(0x82)
+        self.send_data(0x00)
+        self.send_command(0X50)
+        self.send_data(0x97)
+        
+        self.send_command(0x20) # vcom
+        for count in range(0, 44):
+            self.send_data(self.lut_vcomDC[count])
+        self.send_command(0x21) # ww --
+        for count in range(0, 42):
+            self.send_data(self.lut_ww[count])
+        self.send_command(0x22) # bw r
+        for count in range(0, 42):
+            self.send_data(self.lut_bw[count])
+        self.send_command(0x23) # wb w
+        for count in range(0, 42):
+            self.send_data(self.lut_wb[count])
+        self.send_command(0x24) # bb b
+        for count in range(0, 42):
+            self.send_data(self.lut_bb[count])
+    
+    def SetPartReg(self):
+        self.send_command(0x82)
+        self.send_data(0x03)
+        self.send_command(0X50)
+        self.send_data(0x47)
+        
+        self.send_command(0x20) # vcom
+        for count in range(0, 44):
+            self.send_data(self.lut_vcom1[count])
+        self.send_command(0x21) # ww --
+        for count in range(0, 42):
+            self.send_data(self.lut_ww1[count])
+        self.send_command(0x22) # bw r
+        for count in range(0, 42):
+            self.send_data(self.lut_bw1[count])
+        self.send_command(0x23) # wb w
+        for count in range(0, 42):
+            self.send_data(self.lut_wb1[count])
+        self.send_command(0x24) # bb b
+        for count in range(0, 42):
+            self.send_data(self.lut_bb1[count])
+
+    def getbuffer(self, image):
+        # logging.debug("bufsiz = ",int(self.width/8) * self.height)
+        buf = [0xFF] * (int(self.width/8) * self.height)
+        image_monocolor = image.convert('1')
+        imwidth, imheight = image_monocolor.size
+        pixels = image_monocolor.load()
+        # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
+        if(imwidth == self.width and imheight == self.height):
+            logging.debug("Vertical")
+            for y in range(imheight):
+                for x in range(imwidth):
+                    # Set the bits for the column of pixels at the current position.
+                    if pixels[x, y] == 0:
+                        buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
+        elif(imwidth == self.height and imheight == self.width):
+            logging.debug("Horizontal")
+            for y in range(imheight):
+                for x in range(imwidth):
+                    newx = y
+                    newy = self.height - x - 1
+                    if pixels[x, y] == 0:
+                        buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
+        return buf
+
+    def display(self, image):
+        if (Image == None):
+            return
+            
+        self.send_command(0x10)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(0x00)
+        epdconfig.delay_ms(10)
+        
+        self.send_command(0x13)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(image[i])
+        epdconfig.delay_ms(10)
+        
+        self.SetFullReg()
+        self.TurnOnDisplay()
+        
+    def DisplayPartial(self, image):   
+        if (Image == None):
+            return
+            
+        self.SetPartReg()
+        self.send_command(0x91)
+        self.send_command(0x90)
+        self.send_data(0)
+        self.send_data(self.width - 1)
+
+        self.send_data(0)
+        self.send_data(0)
+        self.send_data(int(self.height / 256))
+        self.send_data(self.height % 256 - 1)
+        self.send_data(0x28)
+            
+        self.send_command(0x10)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(image[i])
+        epdconfig.delay_ms(10)
+        
+        self.send_command(0x13)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(~image[i])
+        epdconfig.delay_ms(10)
+          
+        self.TurnOnDisplay()
+        
+    def Clear(self, color):
+        self.send_command(0x10)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(0x00)
+        epdconfig.delay_ms(10)
+        
+        self.send_command(0x13)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(0xFF)
+        epdconfig.delay_ms(10)
+        
+        self.SetFullReg()
+        self.TurnOnDisplay()
+
+    def sleep(self):
+        self.send_command(0X50)
+        self.send_data(0xf7)
+        self.send_command(0X02) # power off
+        self.send_command(0X07) # deep sleep  
+        self.send_data(0xA5)
+
+        epdconfig.module_exit()
+
+### END OF FILE ###
+
diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213d/epdconfig.py b/pwnagotchi/ui/hw/libs/waveshare/v213d/epdconfig.py
new file mode 100644
index 0000000..861f43d
--- /dev/null
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213d/epdconfig.py
@@ -0,0 +1,154 @@
+# /*****************************************************************************
+# * | File        :	  epdconfig.py
+# * | Author      :   Waveshare team
+# * | Function    :   Hardware underlying interface
+# * | Info        :
+# *----------------
+# * | This version:   V1.0
+# * | Date        :   2019-06-21
+# * | Info        :   
+# ******************************************************************************
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documnetation 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
+# furished 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 OR 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.
+#
+
+import os
+import logging
+import sys
+import time
+
+
+class RaspberryPi:
+    # Pin definition
+    RST_PIN         = 17
+    DC_PIN          = 25
+    CS_PIN          = 8
+    BUSY_PIN        = 24
+
+    def __init__(self):
+        import spidev
+        import RPi.GPIO
+
+        self.GPIO = RPi.GPIO
+
+        # SPI device, bus = 0, device = 0
+        self.SPI = spidev.SpiDev(0, 0)
+
+    def digital_write(self, pin, value):
+        self.GPIO.output(pin, value)
+
+    def digital_read(self, pin):
+        return self.GPIO.input(pin)
+
+    def delay_ms(self, delaytime):
+        time.sleep(delaytime / 1000.0)
+
+    def spi_writebyte(self, data):
+        self.SPI.writebytes(data)
+
+    def module_init(self):
+        self.GPIO.setmode(self.GPIO.BCM)
+        self.GPIO.setwarnings(False)
+        self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
+        self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
+        self.GPIO.setup(self.CS_PIN, self.GPIO.OUT)
+        self.GPIO.setup(self.BUSY_PIN, self.GPIO.IN)
+        self.SPI.max_speed_hz = 4000000
+        self.SPI.mode = 0b00
+        return 0
+
+    def module_exit(self):
+        logging.debug("spi end")
+        self.SPI.close()
+
+        logging.debug("close 5V, Module enters 0 power consumption ...")
+        self.GPIO.output(self.RST_PIN, 0)
+        self.GPIO.output(self.DC_PIN, 0)
+
+        self.GPIO.cleanup()
+
+
+class JetsonNano:
+    # Pin definition
+    RST_PIN         = 17
+    DC_PIN          = 25
+    CS_PIN          = 8
+    BUSY_PIN        = 24
+
+    def __init__(self):
+        import ctypes
+        find_dirs = [
+            os.path.dirname(os.path.realpath(__file__)),
+            '/usr/local/lib',
+            '/usr/lib',
+        ]
+        self.SPI = None
+        for find_dir in find_dirs:
+            so_filename = os.path.join(find_dir, 'sysfs_software_spi.so')
+            if os.path.exists(so_filename):
+                self.SPI = ctypes.cdll.LoadLibrary(so_filename)
+                break
+        if self.SPI is None:
+            raise RuntimeError('Cannot find sysfs_software_spi.so')
+
+        import Jetson.GPIO
+        self.GPIO = Jetson.GPIO
+
+    def digital_write(self, pin, value):
+        self.GPIO.output(pin, value)
+
+    def digital_read(self, pin):
+        return self.GPIO.input(self.BUSY_PIN)
+
+    def delay_ms(self, delaytime):
+        time.sleep(delaytime / 1000.0)
+
+    def spi_writebyte(self, data):
+        self.SPI.SYSFS_software_spi_transfer(data[0])
+
+    def module_init(self):
+        self.GPIO.setmode(self.GPIO.BCM)
+        self.GPIO.setwarnings(False)
+        self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
+        self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
+        self.GPIO.setup(self.CS_PIN, self.GPIO.OUT)
+        self.GPIO.setup(self.BUSY_PIN, self.GPIO.IN)
+        self.SPI.SYSFS_software_spi_begin()
+        return 0
+
+    def module_exit(self):
+        logging.debug("spi end")
+        self.SPI.SYSFS_software_spi_end()
+
+        logging.debug("close 5V, Module enters 0 power consumption ...")
+        self.GPIO.output(self.RST_PIN, 0)
+        self.GPIO.output(self.DC_PIN, 0)
+
+        self.GPIO.cleanup()
+
+
+if os.path.exists('/sys/bus/platform/drivers/gpiomem-bcm2835'):
+    implementation = RaspberryPi()
+else:
+    implementation = JetsonNano()
+
+for func in [x for x in dir(implementation) if not x.startswith('_')]:
+    setattr(sys.modules[__name__], func, getattr(implementation, func))
+
+
+### END OF FILE ###
diff --git a/pwnagotchi/ui/hw/waveshare213d.py b/pwnagotchi/ui/hw/waveshare213d.py
new file mode 100644
index 0000000..fe03611
--- /dev/null
+++ b/pwnagotchi/ui/hw/waveshare213d.py
@@ -0,0 +1,69 @@
+import logging
+
+import pwnagotchi.ui.fonts as fonts
+from pwnagotchi.ui.hw.base import DisplayImpl
+
+
+class Waveshare213d(DisplayImpl):
+    def __init__(self, config):
+        super(Waveshare213d, self).__init__(config, 'waveshare213d')
+        self._display = None
+
+    def layout(self):
+        if self.config['color'] == 'black':
+            fonts.setup(10, 9, 10, 35)
+            self._layout['width'] = 250
+            self._layout['height'] = 122
+            self._layout['face'] = (0, 40)
+            self._layout['name'] = (5, 20)
+            self._layout['channel'] = (0, 0)
+            self._layout['aps'] = (28, 0)
+            self._layout['uptime'] = (185, 0)
+            self._layout['line1'] = [0, 14, 250, 14]
+            self._layout['line2'] = [0, 108, 250, 108]
+            self._layout['friend_face'] = (0, 92)
+            self._layout['friend_name'] = (40, 94)
+            self._layout['shakes'] = (0, 109)
+            self._layout['mode'] = (225, 109)
+            self._layout['status'] = {
+                'pos': (125, 20),
+                'font': fonts.Medium,
+                'max': 20
+            }
+        else:
+            fonts.setup(10, 8, 10, 25)
+            self._layout['width'] = 212
+            self._layout['height'] = 104
+            self._layout['face'] = (0, 26)
+            self._layout['name'] = (5, 15)
+            self._layout['channel'] = (0, 0)
+            self._layout['aps'] = (28, 0)
+            self._layout['status'] = (91, 15)
+            self._layout['uptime'] = (147, 0)
+            self._layout['line1'] = [0, 12, 212, 12]
+            self._layout['line2'] = [0, 92, 212, 92]
+            self._layout['friend_face'] = (0, 76)
+            self._layout['friend_name'] = (40, 78)
+            self._layout['shakes'] = (0, 93)
+            self._layout['mode'] = (187, 93)
+            self._layout['status'] = {
+                'pos': (125, 20),
+                'font': fonts.Medium,
+                'max': 14
+            }
+        return self._layout
+
+    def initialize(self):
+        logging.info("initializing waveshare 213d display")
+        from pwnagotchi.ui.hw.libs.waveshare.v213d.epd2in13d import EPD
+        self._display = EPD()
+        self._display.init(self._display.FULL_UPDATE)
+        self._display.Clear(0xff)
+        self._display.init(self._display.PART_UPDATE)
+
+    def render(self, canvas):
+        buf = self._display.getbuffer(canvas)
+        self._display.displayPartial(buf)
+
+    def clear(self):
+        self._display.Clear(0xff)
diff --git a/pwnagotchi/utils.py b/pwnagotchi/utils.py
index c474ae2..86da38e 100644
--- a/pwnagotchi/utils.py
+++ b/pwnagotchi/utils.py
@@ -100,6 +100,9 @@ def load_config(args):
     elif config['ui']['display']['type'] in ('ws_154inch', 'ws154inch', 'waveshare_154inch', 'waveshare154inch'):
         config['ui']['display']['type'] = 'waveshare154inch'
 
+    elif config['ui']['display']['type'] in ('ws_213d', 'ws213d', 'waveshare_213d', 'waveshare213d'):
+        config['ui']['display']['type'] = 'waveshare213d'
+
     else:
         print("unsupported display type %s" % config['ui']['display']['type'])
         exit(1)

From dfcc2f6bf28528bcb659c1deece7140999c9c90a Mon Sep 17 00:00:00 2001
From: Justin Richards <ratmandu@gmail.com>
Date: Sun, 27 Oct 2019 15:48:14 -0500
Subject: [PATCH 02/12] Adding original screen clearing LUT to help prevent
 burn-in

Signed-off-by: Justin Richards <ratmandu@gmail.com>
---
 .../ui/hw/libs/waveshare/v1/epd2in13bcFAST.py | 91 +++++++++++--------
 1 file changed, 55 insertions(+), 36 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py b/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py
index b770454..5804bdf 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py
@@ -58,10 +58,10 @@ class EPD:
         self.height = EPD_HEIGHT
 
     lut_vcomDC = [  
-        0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
-        0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
-        0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
-        0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x99, 0x20, 0x20, 0x20, 0x20, 0x06,
+        0x00, 0x20, 0x20, 0x00, 0x00, 0x02,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -69,44 +69,46 @@ class EPD:
     ]
 
     lut_ww = [  
-        0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
-        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
-        0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
-        0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x66, 0x06, 0x06, 0x06, 0x06, 0x04,
+        0xAA, 0x32, 0x32, 0x00, 0x00, 0x02,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     ]
 
     lut_bw = [  
-        0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
-        0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
-        0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
-        0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
+        0x66, 0x06, 0x06, 0x06, 0x06, 0x04,
+        0xAA, 0x32, 0x32, 0x00, 0x00, 0x02,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     ]
 
     lut_wb = [
-        0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
-        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
-        0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
-        0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x99, 0x06, 0x06, 0x06, 0x06, 0x04,
+        0x55, 0x42, 0x42, 0x00, 0x00, 0x02,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     ]
 
     lut_bb = [ 
-        0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
-        0x90, 0x28, 0x28, 0x00, 0x00, 0x00,
-        0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
-        0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
+        0x99, 0x06, 0x06, 0x06, 0x06, 0x04,
+        0x55, 0x42, 0x42, 0x00, 0x00, 0x02,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    ]
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ] 
     
     lut_vcom1 = [  
         0xA0, 0x10, 0x10, 0x00, 0x00, 0x02,
@@ -233,16 +235,32 @@ class EPD:
         return 0
         
     def SetFullReg(self):
-        self.send_command(0x82)
-        self.send_data(0x00)
-        self.send_command(0X50)
-        self.send_data(0x97)
-#        self.send_command(0x00)	# panel setting
-#        self.send_data(0x9f) # LUT from OTP,128x296
+        self.send_command(0x00)	# panel setting
+        self.send_data(0x9f) # LUT from OTP,128x296
+        #self.send_command(0x82)
+        #self.send_data(0x00)
+        #self.send_command(0X50)
+        #self.send_data(0x97)
+        
+        #self.send_command(0x20) # vcom
+        #for count in range(0, 44):
+        #    self.send_data(self.lut_vcomDC[count])
+        #self.send_command(0x21) # ww --
+        #for count in range(0, 42):
+        #    self.send_data(self.lut_ww[count])
+        #self.send_command(0x22) # bw r
+        #for count in range(0, 42):
+        #    self.send_data(self.lut_bw[count])
+        #self.send_command(0x23) # wb w
+        #for count in range(0, 42):
+        #    self.send_data(self.lut_wb[count])
+        #self.send_command(0x24) # bb b
+        #for count in range(0, 42):
+        #    self.send_data(self.lut_bb[count])
 
     def SetPartReg(self):
-#        self.send_command(0x00)	# panel setting
-#        self.send_data(0xbf) # LUT from OTP,128x296
+        self.send_command(0x00)	# panel setting
+        self.send_data(0xbf) # LUT from OTP,128x296
         self.send_command(0x82)
         self.send_data(0x03)
         self.send_command(0X50)
@@ -336,16 +354,17 @@ class EPD:
     def Clear(self):
         self.send_command(0x10)
         for i in range(0, int(self.width * self.height / 8)):
-            self.send_data(0x00)
-        epdconfig.delay_ms(10)
-        
+            self.send_data(0xFF)
+        self.send_command(0x92)
+
         self.send_command(0x13)
         for i in range(0, int(self.width * self.height / 8)):
-            self.send_data(0x00)
-        epdconfig.delay_ms(10)
-        
+            self.send_data(0xFF)
+        self.send_command(0x92)
+
         self.SetFullReg()
-        self.TurnOnDisplay()
+        self.send_command(0x12) # REFRESH
+        self.ReadBusy()
 
     def sleep(self):
         self.send_command(0X50)

From 4814e10940cbc6c4bf3ba2f9cf2fae63a121ed4c Mon Sep 17 00:00:00 2001
From: Nikhil Jha <hi@nikhiljha.com>
Date: Sun, 27 Oct 2019 14:14:42 -0700
Subject: [PATCH 03/12] attempt to fix init for 213d

Signed-off-by: Nikhil Jha <hi@nikhiljha.com>
---
 pwnagotchi/ui/hw/waveshare213d.py | 43 +++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/pwnagotchi/ui/hw/waveshare213d.py b/pwnagotchi/ui/hw/waveshare213d.py
index fe03611..0d9dfcc 100644
--- a/pwnagotchi/ui/hw/waveshare213d.py
+++ b/pwnagotchi/ui/hw/waveshare213d.py
@@ -38,7 +38,6 @@ class Waveshare213d(DisplayImpl):
             self._layout['name'] = (5, 15)
             self._layout['channel'] = (0, 0)
             self._layout['aps'] = (28, 0)
-            self._layout['status'] = (91, 15)
             self._layout['uptime'] = (147, 0)
             self._layout['line1'] = [0, 12, 212, 12]
             self._layout['line2'] = [0, 92, 212, 92]
@@ -47,23 +46,45 @@ class Waveshare213d(DisplayImpl):
             self._layout['shakes'] = (0, 93)
             self._layout['mode'] = (187, 93)
             self._layout['status'] = {
-                'pos': (125, 20),
+                'pos': (91, 15),
                 'font': fonts.Medium,
-                'max': 14
+                'max': 20
             }
         return self._layout
 
     def initialize(self):
-        logging.info("initializing waveshare 213d display")
-        from pwnagotchi.ui.hw.libs.waveshare.v213d.epd2in13d import EPD
-        self._display = EPD()
-        self._display.init(self._display.FULL_UPDATE)
-        self._display.Clear(0xff)
-        self._display.init(self._display.PART_UPDATE)
+        if self.config['color'] == 'black':
+            logging.info("initializing waveshare v1 display in monochromatic mode")
+            from pwnagotchi.ui.hw.libs.waveshare.v1.epd2in13 import EPD
+            self._display = EPD()
+            self._display.init(self._display.lut_full_update)
+            self._display.Clear(0xFF)
+            self._display.init(self._display.lut_partial_update)
+        elif self.config['color'] == 'fastAndFurious':
+            logging.info("initializing waveshare v1 3-color display in FAST MODE")
+            logging.info("THIS MAY BE POTENTIALLY DANGEROUS. NO WARRANTY IS PROVIDED")
+            logging.info("USE THIS DISPLAY IN THIS MODE AT YOUR OWN RISK")
+            from pwnagotchi.ui.hw.libs.waveshare.v1.epd2in13bcFAST import EPD
+            self._display = EPD()
+            self._display.init()
+            self._display.Clear()
+        else:
+            logging.info("initializing waveshare v1 display 3-color mode")
+            from pwnagotchi.ui.hw.libs.waveshare.v1.epd2in13bc import EPD
+            self._display = EPD()
+            self._display.init()
+            self._display.Clear()
 
     def render(self, canvas):
-        buf = self._display.getbuffer(canvas)
-        self._display.displayPartial(buf)
+        if self.config['color'] == 'black':
+            buf = self._display.getbuffer(canvas)
+            self._display.display(buf)
+        elif self.config['color'] == 'fastAndFurious':
+            buf_black = self._display.getbuffer(canvas)
+            self._display.DisplayPartial(buf_black)
+        else:
+            buf_black = self._display.getbuffer(canvas)
+            self._display.displayBlack(buf_black)
 
     def clear(self):
         self._display.Clear(0xff)

From 4d5bfc2adf630e42fdbd223c8febe0501af58fdb Mon Sep 17 00:00:00 2001
From: Nikhil Jha <hi@nikhiljha.com>
Date: Sun, 27 Oct 2019 14:20:54 -0700
Subject: [PATCH 04/12] maybe actually fix now

Signed-off-by: Nikhil Jha <hi@nikhiljha.com>
---
 .../ui/hw/libs/waveshare/v213d/epd2in13d.py   |  2 +-
 pwnagotchi/ui/hw/waveshare213d.py             | 40 +++++--------------
 2 files changed, 10 insertions(+), 32 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py b/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py
index de02f86..81199c7 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213d/epd2in13d.py
@@ -331,7 +331,7 @@ class EPD:
           
         self.TurnOnDisplay()
         
-    def Clear(self, color):
+    def Clear(self):
         self.send_command(0x10)
         for i in range(0, int(self.width * self.height / 8)):
             self.send_data(0x00)
diff --git a/pwnagotchi/ui/hw/waveshare213d.py b/pwnagotchi/ui/hw/waveshare213d.py
index 0d9dfcc..a00d548 100644
--- a/pwnagotchi/ui/hw/waveshare213d.py
+++ b/pwnagotchi/ui/hw/waveshare213d.py
@@ -53,38 +53,16 @@ class Waveshare213d(DisplayImpl):
         return self._layout
 
     def initialize(self):
-        if self.config['color'] == 'black':
-            logging.info("initializing waveshare v1 display in monochromatic mode")
-            from pwnagotchi.ui.hw.libs.waveshare.v1.epd2in13 import EPD
-            self._display = EPD()
-            self._display.init(self._display.lut_full_update)
-            self._display.Clear(0xFF)
-            self._display.init(self._display.lut_partial_update)
-        elif self.config['color'] == 'fastAndFurious':
-            logging.info("initializing waveshare v1 3-color display in FAST MODE")
-            logging.info("THIS MAY BE POTENTIALLY DANGEROUS. NO WARRANTY IS PROVIDED")
-            logging.info("USE THIS DISPLAY IN THIS MODE AT YOUR OWN RISK")
-            from pwnagotchi.ui.hw.libs.waveshare.v1.epd2in13bcFAST import EPD
-            self._display = EPD()
-            self._display.init()
-            self._display.Clear()
-        else:
-            logging.info("initializing waveshare v1 display 3-color mode")
-            from pwnagotchi.ui.hw.libs.waveshare.v1.epd2in13bc import EPD
-            self._display = EPD()
-            self._display.init()
-            self._display.Clear()
+        logging.info("initializing waveshare 213d display")
+        from pwnagotchi.ui.hw.libs.waveshare.v213d.epd2in13d import EPD
+        self._display = EPD()
+        self._display.init()
+        self._display.Clear()
 
     def render(self, canvas):
-        if self.config['color'] == 'black':
-            buf = self._display.getbuffer(canvas)
-            self._display.display(buf)
-        elif self.config['color'] == 'fastAndFurious':
-            buf_black = self._display.getbuffer(canvas)
-            self._display.DisplayPartial(buf_black)
-        else:
-            buf_black = self._display.getbuffer(canvas)
-            self._display.displayBlack(buf_black)
+        buf = self._display.getbuffer(canvas)
+        self._display.display(buf)
 
     def clear(self):
-        self._display.Clear(0xff)
+        pass
+        #self._display.Clear()

From c0434b7ddef0f729baabc8a2851b1b79fd156d6d Mon Sep 17 00:00:00 2001
From: Dinh Bao Dang <chillinPanda@gmail.com>
Date: Sun, 27 Oct 2019 22:29:16 +0100
Subject: [PATCH 05/12] add waveshare154inch config possibility to
 defaults.yml, since ...

support was introduced in #434
---
 pwnagotchi/defaults.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml
index 8016174..bf6380e 100644
--- a/pwnagotchi/defaults.yml
+++ b/pwnagotchi/defaults.yml
@@ -204,7 +204,7 @@ ui:
     display:
         enabled: true
         rotation: 180
-        # Possible options inkyphat/inky, papirus/papi, waveshare_1/ws_1 or waveshare_2/ws_2, oledhat, lcdhat, waveshare27inch, dfrobot/df
+        # Possible options inkyphat/inky, papirus/papi, waveshare_1/ws_1 or waveshare_2/ws_2, oledhat, lcdhat, waveshare154inch, waveshare27inch, dfrobot/df
         type: 'waveshare_2'
         # Possible options red/yellow/black (black used for monocromatic displays)
         # Waveshare tri-color 2.13in display can be over-driven with color set as 'fastAndFurious'

From 7fd31e14f900353b9c49ea6fef434e9698a54bec Mon Sep 17 00:00:00 2001
From: Nikhil Jha <hi@nikhiljha.com>
Date: Sun, 27 Oct 2019 14:32:23 -0700
Subject: [PATCH 06/12] fix resolution

Signed-off-by: Nikhil Jha <hi@nikhiljha.com>
---
 pwnagotchi/ui/hw/waveshare213d.py | 63 +++++++++++--------------------
 1 file changed, 21 insertions(+), 42 deletions(-)

diff --git a/pwnagotchi/ui/hw/waveshare213d.py b/pwnagotchi/ui/hw/waveshare213d.py
index a00d548..9aa8ff8 100644
--- a/pwnagotchi/ui/hw/waveshare213d.py
+++ b/pwnagotchi/ui/hw/waveshare213d.py
@@ -10,46 +10,25 @@ class Waveshare213d(DisplayImpl):
         self._display = None
 
     def layout(self):
-        if self.config['color'] == 'black':
-            fonts.setup(10, 9, 10, 35)
-            self._layout['width'] = 250
-            self._layout['height'] = 122
-            self._layout['face'] = (0, 40)
-            self._layout['name'] = (5, 20)
-            self._layout['channel'] = (0, 0)
-            self._layout['aps'] = (28, 0)
-            self._layout['uptime'] = (185, 0)
-            self._layout['line1'] = [0, 14, 250, 14]
-            self._layout['line2'] = [0, 108, 250, 108]
-            self._layout['friend_face'] = (0, 92)
-            self._layout['friend_name'] = (40, 94)
-            self._layout['shakes'] = (0, 109)
-            self._layout['mode'] = (225, 109)
-            self._layout['status'] = {
-                'pos': (125, 20),
-                'font': fonts.Medium,
-                'max': 20
-            }
-        else:
-            fonts.setup(10, 8, 10, 25)
-            self._layout['width'] = 212
-            self._layout['height'] = 104
-            self._layout['face'] = (0, 26)
-            self._layout['name'] = (5, 15)
-            self._layout['channel'] = (0, 0)
-            self._layout['aps'] = (28, 0)
-            self._layout['uptime'] = (147, 0)
-            self._layout['line1'] = [0, 12, 212, 12]
-            self._layout['line2'] = [0, 92, 212, 92]
-            self._layout['friend_face'] = (0, 76)
-            self._layout['friend_name'] = (40, 78)
-            self._layout['shakes'] = (0, 93)
-            self._layout['mode'] = (187, 93)
-            self._layout['status'] = {
-                'pos': (91, 15),
-                'font': fonts.Medium,
-                'max': 20
-            }
+        fonts.setup(10, 8, 10, 25)
+        self._layout['width'] = 212
+        self._layout['height'] = 104
+        self._layout['face'] = (0, 26)
+        self._layout['name'] = (5, 15)
+        self._layout['channel'] = (0, 0)
+        self._layout['aps'] = (28, 0)
+        self._layout['uptime'] = (147, 0)
+        self._layout['line1'] = [0, 12, 212, 12]
+        self._layout['line2'] = [0, 92, 212, 92]
+        self._layout['friend_face'] = (0, 76)
+        self._layout['friend_name'] = (40, 78)
+        self._layout['shakes'] = (0, 93)
+        self._layout['mode'] = (187, 93)
+        self._layout['status'] = {
+            'pos': (91, 15),
+            'font': fonts.Medium,
+            'max': 20
+        }
         return self._layout
 
     def initialize(self):
@@ -64,5 +43,5 @@ class Waveshare213d(DisplayImpl):
         self._display.display(buf)
 
     def clear(self):
-        pass
-        #self._display.Clear()
+        #pass
+        self._display.Clear()

From d3c6194e0fdc985748b558b76fd1734d2e35c288 Mon Sep 17 00:00:00 2001
From: Justin Richards <ratmandu@gmail.com>
Date: Sun, 27 Oct 2019 19:11:36 -0500
Subject: [PATCH 07/12] adding gpio plugin

Signed-off-by: Justin Richards <ratmandu@gmail.com>
---
 pwnagotchi/plugins/default/gpio_buttons.py | 38 ++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 pwnagotchi/plugins/default/gpio_buttons.py

diff --git a/pwnagotchi/plugins/default/gpio_buttons.py b/pwnagotchi/plugins/default/gpio_buttons.py
new file mode 100644
index 0000000..d9860d1
--- /dev/null
+++ b/pwnagotchi/plugins/default/gpio_buttons.py
@@ -0,0 +1,38 @@
+__author__ = 'ratmandu@gmail.com'
+__version__ = '1.0.0'
+__name__ = 'gpio_buttons'
+__license__ = 'GPL3'
+__description__ = 'GPIO Button support plugin'
+
+import logging
+import RPi.GPIO as GPIO
+import subprocess
+
+running = False
+OPTIONS = dict()
+GPIOs = {}
+COMMANDs = None 
+
+def runCommand(channel):
+  command = GPIOs[channel]
+  logging.info(f"Button Pressed! Running command: {command}")
+  process = subprocess.Popen(command, shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
+  process.wait()
+
+
+def on_loaded():
+  logging.info("GPIO Button plugin loaded.")
+  
+  #get list of GPIOs
+  gpios = OPTIONS['gpios']
+
+  #set gpio numbering
+  GPIO.setmode(GPIO.BCM)
+
+  for i in gpios:
+    gpio = list(i)[0]
+    command = i[gpio]
+    GPIOs[gpio] = command
+    GPIO.setup(gpio, GPIO.IN, GPIO.PUD_UP)
+    GPIO.add_event_detect(gpio, GPIO.FALLING, callback=runCommand, bouncetime=300)
+    logging.info("Added command: %s to GPIO #%d", command, gpio)

From 92773a2b37331f228f0c1ef657607134ed2256b4 Mon Sep 17 00:00:00 2001
From: Justin Richards <ratmandu@gmail.com>
Date: Sun, 27 Oct 2019 19:20:43 -0500
Subject: [PATCH 08/12] adding to defaults.yml and shortening debounce a little
 bit

Signed-off-by: Justin Richards <ratmandu@gmail.com>
---
 pwnagotchi/defaults.yml                    | 6 ++++++
 pwnagotchi/plugins/default/gpio_buttons.py | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml
index 8016174..eb389ac 100644
--- a/pwnagotchi/defaults.yml
+++ b/pwnagotchi/defaults.yml
@@ -83,6 +83,12 @@ main:
             enabled: false
             #The IP Address of your phone with Paw Server running, default (option is empty) is 192.168.44.1
             ip: ''
+        gpio_buttons:
+            enabled: false
+            #The following is a list of the GPIO number for your button, and the command you want to run when it is pressed
+            gpios:
+            - 20: 'sudo touch /root/.pwnagotchi-auto && sudo systemctl restart pwnagotchi'
+            - 21: 'shutdown -h now'
     # monitor interface to use
     iface: mon0
     # command to run to bring the mon interface up in case it's not up already
diff --git a/pwnagotchi/plugins/default/gpio_buttons.py b/pwnagotchi/plugins/default/gpio_buttons.py
index d9860d1..c0f6ceb 100644
--- a/pwnagotchi/plugins/default/gpio_buttons.py
+++ b/pwnagotchi/plugins/default/gpio_buttons.py
@@ -34,5 +34,5 @@ def on_loaded():
     command = i[gpio]
     GPIOs[gpio] = command
     GPIO.setup(gpio, GPIO.IN, GPIO.PUD_UP)
-    GPIO.add_event_detect(gpio, GPIO.FALLING, callback=runCommand, bouncetime=300)
+    GPIO.add_event_detect(gpio, GPIO.FALLING, callback=runCommand, bouncetime=250)
     logging.info("Added command: %s to GPIO #%d", command, gpio)

From 7676b55b87e3a4c7d5199b8d1742920f3c573db7 Mon Sep 17 00:00:00 2001
From: Justin Richards <ratmandu@gmail.com>
Date: Sun, 27 Oct 2019 19:36:29 -0500
Subject: [PATCH 09/12] Revert "Adding original screen clearing LUT to help
 prevent burn-in"

This reverts commit dfcc2f6bf28528bcb659c1deece7140999c9c90a.
---
 .../ui/hw/libs/waveshare/v1/epd2in13bcFAST.py | 91 ++++++++-----------
 1 file changed, 36 insertions(+), 55 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py b/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py
index 5804bdf..b770454 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v1/epd2in13bcFAST.py
@@ -58,10 +58,10 @@ class EPD:
         self.height = EPD_HEIGHT
 
     lut_vcomDC = [  
-        0x99, 0x20, 0x20, 0x20, 0x20, 0x06,
-        0x00, 0x20, 0x20, 0x00, 0x00, 0x02,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -69,46 +69,44 @@ class EPD:
     ]
 
     lut_ww = [  
-        0x66, 0x06, 0x06, 0x06, 0x06, 0x04,
-        0xAA, 0x32, 0x32, 0x00, 0x00, 0x02,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     ]
 
     lut_bw = [  
-        0x66, 0x06, 0x06, 0x06, 0x06, 0x04,
-        0xAA, 0x32, 0x32, 0x00, 0x00, 0x02,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
+        0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
+        0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     ]
 
     lut_wb = [
-        0x99, 0x06, 0x06, 0x06, 0x06, 0x04,
-        0x55, 0x42, 0x42, 0x00, 0x00, 0x02,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
+        0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     ]
 
     lut_bb = [ 
-        0x99, 0x06, 0x06, 0x06, 0x06, 0x04,
-        0x55, 0x42, 0x42, 0x00, 0x00, 0x02,
+        0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
+        0x90, 0x28, 0x28, 0x00, 0x00, 0x00,
+        0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
+        0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    ] 
+    ]
     
     lut_vcom1 = [  
         0xA0, 0x10, 0x10, 0x00, 0x00, 0x02,
@@ -235,32 +233,16 @@ class EPD:
         return 0
         
     def SetFullReg(self):
-        self.send_command(0x00)	# panel setting
-        self.send_data(0x9f) # LUT from OTP,128x296
-        #self.send_command(0x82)
-        #self.send_data(0x00)
-        #self.send_command(0X50)
-        #self.send_data(0x97)
-        
-        #self.send_command(0x20) # vcom
-        #for count in range(0, 44):
-        #    self.send_data(self.lut_vcomDC[count])
-        #self.send_command(0x21) # ww --
-        #for count in range(0, 42):
-        #    self.send_data(self.lut_ww[count])
-        #self.send_command(0x22) # bw r
-        #for count in range(0, 42):
-        #    self.send_data(self.lut_bw[count])
-        #self.send_command(0x23) # wb w
-        #for count in range(0, 42):
-        #    self.send_data(self.lut_wb[count])
-        #self.send_command(0x24) # bb b
-        #for count in range(0, 42):
-        #    self.send_data(self.lut_bb[count])
+        self.send_command(0x82)
+        self.send_data(0x00)
+        self.send_command(0X50)
+        self.send_data(0x97)
+#        self.send_command(0x00)	# panel setting
+#        self.send_data(0x9f) # LUT from OTP,128x296
 
     def SetPartReg(self):
-        self.send_command(0x00)	# panel setting
-        self.send_data(0xbf) # LUT from OTP,128x296
+#        self.send_command(0x00)	# panel setting
+#        self.send_data(0xbf) # LUT from OTP,128x296
         self.send_command(0x82)
         self.send_data(0x03)
         self.send_command(0X50)
@@ -354,17 +336,16 @@ class EPD:
     def Clear(self):
         self.send_command(0x10)
         for i in range(0, int(self.width * self.height / 8)):
-            self.send_data(0xFF)
-        self.send_command(0x92)
-
+            self.send_data(0x00)
+        epdconfig.delay_ms(10)
+        
         self.send_command(0x13)
         for i in range(0, int(self.width * self.height / 8)):
-            self.send_data(0xFF)
-        self.send_command(0x92)
-
+            self.send_data(0x00)
+        epdconfig.delay_ms(10)
+        
         self.SetFullReg()
-        self.send_command(0x12) # REFRESH
-        self.ReadBusy()
+        self.TurnOnDisplay()
 
     def sleep(self):
         self.send_command(0X50)

From 0ad6e887acc55f46e08a9e20122d37ae8d076718 Mon Sep 17 00:00:00 2001
From: Ciara Brennan <ciara.brennan@gmail.com>
Date: Mon, 28 Oct 2019 11:49:24 +0000
Subject: [PATCH 10/12] check zip is installed first

Signed-off-by: Ciara Brennan <ciara.brennan@gmail.com>
---
 scripts/backup.sh | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/scripts/backup.sh b/scripts/backup.sh
index 85b53e0..281a2bb 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -19,6 +19,11 @@ FILES_TO_BACKUP=(
   /home/pi/.bashrc
 )
 
+if ! type "zip" >/dev/null 2>&1; then
+  echo "This script requires zip, please resolve and try again"
+  exit 1
+fi
+
 ping -c 1 "${UNIT_HOSTNAME}" >/dev/null || {
   echo "@ unit ${UNIT_HOSTNAME} can't be reached, make sure it's connected and a static IP assigned to the USB interface."
   exit 1

From 3b1d90baefa3daec734e8d21ecd59d4e66bc38e6 Mon Sep 17 00:00:00 2001
From: Alex Muthmann <deveth0@users.noreply.github.com>
Date: Mon, 28 Oct 2019 12:52:14 +0100
Subject: [PATCH 11/12] Fix _display.clear()

There is a typo (uppercase / lowercase) which prevents the display from initializing.
---
 pwnagotchi/ui/hw/lcdhat.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pwnagotchi/ui/hw/lcdhat.py b/pwnagotchi/ui/hw/lcdhat.py
index 75779a3..92e45a7 100644
--- a/pwnagotchi/ui/hw/lcdhat.py
+++ b/pwnagotchi/ui/hw/lcdhat.py
@@ -37,7 +37,7 @@ class LcdHat(DisplayImpl):
         from pwnagotchi.ui.hw.libs.waveshare.lcdhat.epd import EPD
         self._display = EPD()
         self._display.init()
-        self._display.Clear()
+        self._display.clear()
 
     def render(self, canvas):
         self._display.display(canvas)

From 18a41f3e292f41dfd0150e068c031ec04348646c Mon Sep 17 00:00:00 2001
From: Lorenzo Milesi <io@maxxer.it>
Date: Tue, 29 Oct 2019 08:32:55 +0100
Subject: [PATCH 12/12] Fix eth0 connection check for MANU mode

Issue #460 - Originally noted by @ZeroCool-Dade
https://github.com/evilsocket/pwnagotchi/commit/a78a4b0b3e5cfbdfea481e140e6c02e0bdb1f72d#commitcomment-35683998

Signed-off-by: Lorenzo Milesi <io@maxxer.it>
---
 builder/pwnagotchi.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builder/pwnagotchi.yml b/builder/pwnagotchi.yml
index daa9070..31dd7e9 100644
--- a/builder/pwnagotchi.yml
+++ b/builder/pwnagotchi.yml
@@ -304,7 +304,7 @@
         # blink 10 times to signal ready state
         /usr/bin/bootblink 10 &
         # start a detached screen session with bettercap
-        if [[ $(ifconfig | grep usb0 | grep RUNNING) ]] || [[ $(cat /sys/class/net/eth0/carrier) ]]; then
+        if [[ $(ifconfig | grep usb0 | grep RUNNING) ]] || [[ !$(grep '1' /sys/class/net/eth0/carrier) ]]; then
           # if override file exists, go into auto mode
           if [ -f /root/.pwnagotchi-auto ]; then
             rm /root/.pwnagotchi-auto
@@ -323,7 +323,7 @@
       content: |
         #!/usr/bin/env bash
         /usr/bin/monstart
-        if [[ $(ifconfig | grep usb0 | grep RUNNING) ]] || [[ $(cat /sys/class/net/eth0/carrier) ]]; then
+        if [[ $(ifconfig | grep usb0 | grep RUNNING) ]] || [[ !$(grep '1' /sys/class/net/eth0/carrier) ]]; then
           # if override file exists, go into auto mode
           if [ -f /root/.pwnagotchi-auto ]; then
             /usr/bin/bettercap -no-colors -caplet pwnagotchi-auto -iface mon0