From 6117235c52e37cc6517cfd3ab27beaf421771dba Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Wed, 11 Dec 2019 23:08:29 +0400
Subject: [PATCH 01/10] added 213bc support

---
 pwnagotchi/ui/display.py                      |   3 +
 .../ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 159 ++++++++++++++++++
 .../ui/hw/libs/waveshare/v213bc/epdconfig.py  | 154 +++++++++++++++++
 pwnagotchi/ui/hw/waveshare213bc.py            |  49 ++++++
 4 files changed, 365 insertions(+)
 create mode 100644 pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
 create mode 100644 pwnagotchi/ui/hw/libs/waveshare/v213bc/epdconfig.py
 create mode 100644 pwnagotchi/ui/hw/waveshare213bc.py

diff --git a/pwnagotchi/ui/display.py b/pwnagotchi/ui/display.py
index 9ef8774..181ed76 100644
--- a/pwnagotchi/ui/display.py
+++ b/pwnagotchi/ui/display.py
@@ -61,6 +61,9 @@ class Display(View):
     def is_waveshare213d(self):
         return self._implementation.name == 'waveshare213d'
 
+    def is_waveshare213bc(self):
+        return self._implementation.name == 'waveshare213bc'
+
     def is_spotpear24inch(self):
         return self._implementation.name == 'spotpear24inch'
 
diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
new file mode 100644
index 0000000..864d8c5
--- /dev/null
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -0,0 +1,159 @@
+# *****************************************************************************
+# * | File        :	  epd2in13bc.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
+
+# 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
+
+    # 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
+            epdconfig.delay_ms(100)
+        logging.debug("e-Paper busy release")
+
+    def init(self):
+        if (epdconfig.module_init() != 0):
+            return -1
+            
+        self.reset()
+
+        self.send_command(0x06) # BOOSTER_SOFT_START
+        self.send_data(0x17)
+        self.send_data(0x17)
+        self.send_data(0x17)
+        
+        self.send_command(0x04) # POWER_ON
+        self.ReadBusy()
+        
+        self.send_command(0x00) # PANEL_SETTING
+        self.send_data(0x8F)
+        
+        self.send_command(0x50) # VCOM_AND_DATA_INTERVAL_SETTING
+        self.send_data(0xF0)
+        
+        self.send_command(0x61) # RESOLUTION_SETTING
+        self.send_data(self.width & 0xff)
+        self.send_data(self.height >> 8)
+        self.send_data(self.height & 0xff)
+        return 0
+
+    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, imageblack, imagered):
+        self.send_command(0x10)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(imageblack[i])
+        self.send_command(0x92)
+        
+        self.send_command(0x13)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(imagered[i])
+        self.send_command(0x92)
+        
+        self.send_command(0x12) # REFRESH
+        self.ReadBusy()
+        
+    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_command(0x13)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(0xFF)
+        self.send_command(0x92)
+        
+        self.send_command(0x12) # REFRESH
+        self.ReadBusy()
+
+    def sleep(self):
+        self.send_command(0x02) # POWER_OFF
+        self.ReadBusy()
+        self.send_command(0x07) # DEEP_SLEEP
+        self.send_data(0xA5) # check code
+        
+        epdconfig.module_exit()
+### END OF FILE ###
+
diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epdconfig.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epdconfig.py
new file mode 100644
index 0000000..861f43d
--- /dev/null
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/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/waveshare213bc.py b/pwnagotchi/ui/hw/waveshare213bc.py
new file mode 100644
index 0000000..6ceebb7
--- /dev/null
+++ b/pwnagotchi/ui/hw/waveshare213bc.py
@@ -0,0 +1,49 @@
+import logging
+import time
+
+import pwnagotchi.ui.fonts as fonts
+from pwnagotchi.ui.hw.base import DisplayImpl
+
+
+class Waveshare213bc(DisplayImpl):
+    def __init__(self, config):
+        super(Waveshare213bc, self).__init__(config, 'waveshare213bc')
+        self._display = None
+
+    def layout(self):
+        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):
+        logging.info("initializing waveshare 213bc display")
+        from pwnagotchi.ui.hw.libs.waveshare.v213bc.epd2in13bc import EPD
+        self._display = EPD()
+        self._display.init()
+        self._display.Clear()
+        time.sleep(1)
+
+    def render(self, canvas):
+        buf = self._display.getbuffer(canvas)
+        self._display.display(buf)
+
+    def clear(self):
+        #pass
+        self._display.Clear()

From eddcf32b6277699ddfdef503c17f23d862a31f0d Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Wed, 11 Dec 2019 23:47:14 +0400
Subject: [PATCH 02/10] 213bc support additions

---
 pwnagotchi/ui/hw/__init__.py | 10 +++++++---
 pwnagotchi/utils.py          |  3 +++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/pwnagotchi/ui/hw/__init__.py b/pwnagotchi/ui/hw/__init__.py
index 696a41f..432cb54 100644
--- a/pwnagotchi/ui/hw/__init__.py
+++ b/pwnagotchi/ui/hw/__init__.py
@@ -10,6 +10,7 @@ from pwnagotchi.ui.hw.waveshare29inch import Waveshare29inch
 from pwnagotchi.ui.hw.waveshare144lcd import Waveshare144lcd
 from pwnagotchi.ui.hw.waveshare154inch import Waveshare154inch
 from pwnagotchi.ui.hw.waveshare213d import Waveshare213d
+from pwnagotchi.ui.hw.waveshare213bc import Waveshare213bc
 from pwnagotchi.ui.hw.spotpear24inch import Spotpear24inch
 
 def display_for(config):
@@ -37,10 +38,10 @@ def display_for(config):
 
     elif config['ui']['display']['type'] == 'waveshare27inch':
         return Waveshare27inch(config)
-    
+
     elif config['ui']['display']['type'] == 'waveshare29inch':
         return Waveshare29inch(config)
-    
+
     elif config['ui']['display']['type'] == 'waveshare144lcd':
         return Waveshare144lcd(config)
 
@@ -50,5 +51,8 @@ def display_for(config):
     elif config['ui']['display']['type'] == 'waveshare213d':
         return Waveshare213d(config)
 
+    elif config['ui']['display']['type'] == 'waveshare213bc':
+        return Waveshare213bc(config)
+
     elif config['ui']['display']['type'] == 'spotpear24inch':
-        return Spotpear24inch(config)
\ No newline at end of file
+        return Spotpear24inch(config)
diff --git a/pwnagotchi/utils.py b/pwnagotchi/utils.py
index 4d0ba90..d645155 100644
--- a/pwnagotchi/utils.py
+++ b/pwnagotchi/utils.py
@@ -115,6 +115,9 @@ def load_config(args):
     elif config['ui']['display']['type'] in ('ws_213d', 'ws213d', 'waveshare_213d', 'waveshare213d'):
         config['ui']['display']['type'] = 'waveshare213d'
 
+    elif config['ui']['display']['type'] in ('ws_213bc', 'ws213bc', 'waveshare_213bc', 'waveshare213bc'):
+        config['ui']['display']['type'] = 'waveshare213bc'
+
     elif config['ui']['display']['type'] in ('spotpear24inch'):
         config['ui']['display']['type'] = 'spotpear24inch'
 

From a4daf4af61d27643f8f8fb94dc3baea75ee6522d Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Thu, 12 Dec 2019 00:37:24 +0400
Subject: [PATCH 03/10] waveshare213b and waveshare213c support bug fixes

---
 .../ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 41 +++++++++++--------
 pwnagotchi/ui/hw/waveshare213bc.py            |  2 +-
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 864d8c5..1d321eb 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -46,11 +46,11 @@ class EPD:
     # Hardware reset
     def reset(self):
         epdconfig.digital_write(self.reset_pin, 1)
-        epdconfig.delay_ms(200) 
+        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)   
+        epdconfig.delay_ms(200)
 
     def send_command(self, command):
         epdconfig.digital_write(self.dc_pin, 0)
@@ -63,7 +63,7 @@ class EPD:
         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
@@ -73,23 +73,23 @@ class EPD:
     def init(self):
         if (epdconfig.module_init() != 0):
             return -1
-            
+
         self.reset()
 
         self.send_command(0x06) # BOOSTER_SOFT_START
         self.send_data(0x17)
         self.send_data(0x17)
         self.send_data(0x17)
-        
+
         self.send_command(0x04) # POWER_ON
         self.ReadBusy()
-        
+
         self.send_command(0x00) # PANEL_SETTING
         self.send_data(0x8F)
-        
+
         self.send_command(0x50) # VCOM_AND_DATA_INTERVAL_SETTING
         self.send_data(0xF0)
-        
+
         self.send_command(0x61) # RESOLUTION_SETTING
         self.send_data(self.width & 0xff)
         self.send_data(self.height >> 8)
@@ -125,26 +125,36 @@ class EPD:
         for i in range(0, int(self.width * self.height / 8)):
             self.send_data(imageblack[i])
         self.send_command(0x92)
-        
+
         self.send_command(0x13)
         for i in range(0, int(self.width * self.height / 8)):
             self.send_data(imagered[i])
         self.send_command(0x92)
-        
+
         self.send_command(0x12) # REFRESH
         self.ReadBusy()
-        
+
+    def pwndisplay(self, imageblack):
+        self.send_command(0x10)
+        for i in range(0, int(self.width * self.height / 8)):
+            self.send_data(imageblack[i])
+        self.send_command(0x92)
+
+        self.send_command(0x12) # REFRESH
+        self.ReadBusy()
+
+
     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_command(0x92)
+
         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_command(0x12) # REFRESH
         self.ReadBusy()
 
@@ -153,7 +163,6 @@ class EPD:
         self.ReadBusy()
         self.send_command(0x07) # DEEP_SLEEP
         self.send_data(0xA5) # check code
-        
+
         epdconfig.module_exit()
 ### END OF FILE ###
-
diff --git a/pwnagotchi/ui/hw/waveshare213bc.py b/pwnagotchi/ui/hw/waveshare213bc.py
index 6ceebb7..81b5770 100644
--- a/pwnagotchi/ui/hw/waveshare213bc.py
+++ b/pwnagotchi/ui/hw/waveshare213bc.py
@@ -42,7 +42,7 @@ class Waveshare213bc(DisplayImpl):
 
     def render(self, canvas):
         buf = self._display.getbuffer(canvas)
-        self._display.display(buf)
+        self._display.pwndisplay(buf)
 
     def clear(self):
         #pass

From 704d7ceaa1e78839de317708570689792025f277 Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Thu, 12 Dec 2019 00:46:14 +0400
Subject: [PATCH 04/10] waveshare213b and waveshare213c support bug fixes

---
 pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 9 +++++++++
 pwnagotchi/ui/hw/waveshare213bc.py                   | 4 +---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 1d321eb..d542dc9 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -158,6 +158,15 @@ class EPD:
         self.send_command(0x12) # REFRESH
         self.ReadBusy()
 
+    def pwnclear(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_command(0x12) # REFRESH
+        self.ReadBusy()
+
     def sleep(self):
         self.send_command(0x02) # POWER_OFF
         self.ReadBusy()
diff --git a/pwnagotchi/ui/hw/waveshare213bc.py b/pwnagotchi/ui/hw/waveshare213bc.py
index 81b5770..70309a8 100644
--- a/pwnagotchi/ui/hw/waveshare213bc.py
+++ b/pwnagotchi/ui/hw/waveshare213bc.py
@@ -1,5 +1,4 @@
 import logging
-import time
 
 import pwnagotchi.ui.fonts as fonts
 from pwnagotchi.ui.hw.base import DisplayImpl
@@ -38,7 +37,6 @@ class Waveshare213bc(DisplayImpl):
         self._display = EPD()
         self._display.init()
         self._display.Clear()
-        time.sleep(1)
 
     def render(self, canvas):
         buf = self._display.getbuffer(canvas)
@@ -46,4 +44,4 @@ class Waveshare213bc(DisplayImpl):
 
     def clear(self):
         #pass
-        self._display.Clear()
+        self._display.pwnclear()

From cdd4c133366aac2749467bf4061d4460a849ba8e Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Thu, 12 Dec 2019 01:09:03 +0400
Subject: [PATCH 05/10] waveshare213b and waveshare213c support bug fixes

---
 .../ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 143 +++++++++++++++++-
 1 file changed, 137 insertions(+), 6 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index d542dc9..6c7467b 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -43,6 +43,109 @@ class EPD:
         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)
@@ -70,6 +173,11 @@ class EPD:
             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
@@ -96,6 +204,29 @@ class EPD:
         self.send_data(self.height & 0xff)
         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 getbuffer(self, image):
         # logging.debug("bufsiz = ",int(self.width/8) * self.height)
         buf = [0xFF] * (int(self.width/8) * self.height)
@@ -138,10 +269,10 @@ class EPD:
         self.send_command(0x10)
         for i in range(0, int(self.width * self.height / 8)):
             self.send_data(imageblack[i])
-        self.send_command(0x92)
+        epdconfig.delay_ms(10)
 
-        self.send_command(0x12) # REFRESH
-        self.ReadBusy()
+        self.SetFullReg()
+        self.TurnOnDisplay()
 
 
     def Clear(self):
@@ -162,10 +293,10 @@ class EPD:
         self.send_command(0x10)
         for i in range(0, int(self.width * self.height / 8)):
             self.send_data(0xFF)
-        self.send_command(0x92)
+        epdconfig.delay_ms(10)
 
-        self.send_command(0x12) # REFRESH
-        self.ReadBusy()
+        self.SetFullReg()
+        self.TurnOnDisplay()
 
     def sleep(self):
         self.send_command(0x02) # POWER_OFF

From 819146f83ac2924ee1096148e60b8093201afe8a Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Thu, 12 Dec 2019 01:56:52 +0400
Subject: [PATCH 06/10] waveshare213b and waveshare213c support bug fixes

---
 .../ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 43 +++++++++++++++----
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 6c7467b..6f3e685 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -29,6 +29,7 @@
 
 import logging
 from . import epdconfig
+from PIL import Image
 
 # Display resolution
 EPD_WIDTH       = 104
@@ -192,16 +193,29 @@ class EPD:
         self.send_command(0x04) # POWER_ON
         self.ReadBusy()
 
-        self.send_command(0x00) # PANEL_SETTING
-        self.send_data(0x8F)
+#        self.send_command(0x00) # PANEL_SETTING
+#        self.send_data(0x8F)
+#        self.send_command(0x50) # VCOM_AND_DATA_INTERVAL_SETTING
+#        self.send_data(0xF0)
+#        self.send_command(0x61) # RESOLUTION_SETTING
+#        self.send_data(self.width & 0xff)
+#        self.send_data(self.height >> 8)
+#        self.send_data(self.height & 0xff)
 
-        self.send_command(0x50) # VCOM_AND_DATA_INTERVAL_SETTING
-        self.send_data(0xF0)
+        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(0x61) # RESOLUTION_SETTING
-        self.send_data(self.width & 0xff)
-        self.send_data(self.height >> 8)
-        self.send_data(self.height & 0xff)
+        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):
@@ -266,7 +280,15 @@ class EPD:
         self.ReadBusy()
 
     def pwndisplay(self, imageblack):
+        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(imageblack[i])
         epdconfig.delay_ms(10)
@@ -291,6 +313,11 @@ class EPD:
 
     def pwnclear(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_command(0x13)
         for i in range(0, int(self.width * self.height / 8)):
             self.send_data(0xFF)
         epdconfig.delay_ms(10)

From e06480e474c5e1704ec56911fbe02996396dd0e0 Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Fri, 13 Dec 2019 10:33:41 +0400
Subject: [PATCH 07/10] Waveshare213bc hung issues workaround

---
 .../ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 32 +++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 6f3e685..39e986a 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -44,6 +44,7 @@ class EPD:
         self.width = EPD_WIDTH
         self.height = EPD_HEIGHT
 
+
     lut_vcomDC = [
         0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
         0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
@@ -183,15 +184,42 @@ class EPD:
         if (epdconfig.module_init() != 0):
             return -1
 
+        logging.debug("e-Paper 2.13bc preboot hang check")
+        while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
+            epdconfig.delay_ms(100)
+            self.reset()
+            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.GPIO.output(epdconfig.RST_PIN, 0)
+            epdconfig.GPIO.output(epdconfig.DC_PIN, 0)
+            epdconfig.GPIO.output(epdconfig.CS_PIN, 0)
+            logging.debug("Reset, powerdown, voltage off done")
+        logging.debug("e-Paper did not hungup")
+
+
         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) # BOOSTER_SOFT_START
         self.send_data(0x17)
         self.send_data(0x17)
         self.send_data(0x17)
 
         self.send_command(0x04) # POWER_ON
-        self.ReadBusy()
+        logging.debug("e-Paper 2.13bc bootup busy")
+        while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
+            epdconfig.delay_ms(100)
+        logging.debug("e-Paper booted")
+
 
 #        self.send_command(0x00) # PANEL_SETTING
 #        self.send_data(0x8F)
@@ -210,7 +238,7 @@ class EPD:
         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.width & 0xff)
         self.send_data((self.height >> 8) & 0xff)
         self.send_data(self.height& 0xff)
 

From 91447a2a3105ed26b0050d6d1208a9957c356448 Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Fri, 13 Dec 2019 11:01:40 +0400
Subject: [PATCH 08/10] Waveshare213bc hung issues workaround - optimizations

---
 .../ui/hw/libs/waveshare/v213bc/epd2in13bc.py       | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 39e986a..11451d3 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -184,7 +184,7 @@ class EPD:
         if (epdconfig.module_init() != 0):
             return -1
 
-        logging.debug("e-Paper 2.13bc preboot hang check")
+        logging.debug("e-Paper 2.13bc preboot Freeze recovery")
         while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
             epdconfig.delay_ms(100)
             self.reset()
@@ -196,8 +196,8 @@ class EPD:
             epdconfig.GPIO.output(epdconfig.RST_PIN, 0)
             epdconfig.GPIO.output(epdconfig.DC_PIN, 0)
             epdconfig.GPIO.output(epdconfig.CS_PIN, 0)
-            logging.debug("Reset, powerdown, voltage off done")
-        logging.debug("e-Paper did not hungup")
+            #logging.debug("Reset, powerdown, voltage off done")
+        logging.debug("e-Paper is not frozen now :)")
 
 
         self.reset()
@@ -218,8 +218,6 @@ class EPD:
         logging.debug("e-Paper 2.13bc bootup busy")
         while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
             epdconfig.delay_ms(100)
-        logging.debug("e-Paper booted")
-
 
 #        self.send_command(0x00) # PANEL_SETTING
 #        self.send_data(0x8F)
@@ -244,6 +242,9 @@ class EPD:
 
         self.send_command(0x82)	# vcom_DC setting
         self.send_data(0x28)
+
+        #self.Clear()
+        logging.debug("e-Paper booted")
         return 0
 
     def SetFullReg(self):
@@ -342,7 +343,7 @@ class EPD:
     def pwnclear(self):
         self.send_command(0x10)
         for i in range(0, int(self.width * self.height / 8)):
-            self.send_data(0x00)
+            self.send_data(0xFF)
         epdconfig.delay_ms(10)
 
         self.send_command(0x13)

From 6d71bcd9650858dae95593e18e94677394d22bab Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Fri, 13 Dec 2019 21:08:56 +0400
Subject: [PATCH 09/10] Display freeze recover enhancements

---
 pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 11451d3..350db9d 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -188,6 +188,11 @@ class EPD:
         while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
             epdconfig.delay_ms(100)
             self.reset()
+            self.send_command(0x06) # BOOSTER_SOFT_START
+            self.send_data(0x17)
+            self.send_data(0x17)
+            self.send_data(0x17)
+            self.send_command(0x04) # POWER_ON
             self.send_command(0X50)
             self.send_data(0xf7)
             self.send_command(0X02) # power off

From a0bc911c0e670a588fba21ae15699bd6bacc1416 Mon Sep 17 00:00:00 2001
From: "mohesh.mohan" <moheshmohan@gmail.com>
Date: Fri, 13 Dec 2019 21:32:52 +0400
Subject: [PATCH 10/10] Display freeze recover enhancements - delay between
 poweron and off

---
 pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
index 350db9d..71386fc 100644
--- a/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
+++ b/pwnagotchi/ui/hw/libs/waveshare/v213bc/epd2in13bc.py
@@ -188,11 +188,20 @@ class EPD:
         while(epdconfig.digital_read(self.busy_pin) == 0):      # 0: idle, 1: busy
             epdconfig.delay_ms(100)
             self.reset()
+            epdconfig.delay_ms(200)
+            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)
+            epdconfig.delay_ms(200)
             self.send_command(0x06) # BOOSTER_SOFT_START
             self.send_data(0x17)
             self.send_data(0x17)
             self.send_data(0x17)
             self.send_command(0x04) # POWER_ON
+            epdconfig.delay_ms(200)
             self.send_command(0X50)
             self.send_data(0xf7)
             self.send_command(0X02) # power off