# Path: tft/driver/ugui.py
# class NoTouch:
#     def __init__(self, location, font, height, width, fgcolor, bgcolor, fontcolor, border, value, initial_value):
#         Screen.addobject(self)
#         self.screen = Screen.current_screen
#         self.redraw = True # Force drawing of static part of image
#         self.location = location
#         self._value = value
#         self._initial_value = initial_value # Optionally enables show() method to handle initialisation
#         self.font = font
#         self.height = height
#         self.width = width
#         self.fill = bgcolor is not None
#         self.visible = True # Used by ButtonList class for invisible buttons
#         self._greyed_out = False # Disabled by user code
#         tft = Screen.get_tft(False) # Not greyed out
#         self.fgcolor = fgcolor if fgcolor is not None else tft.getColor()
#         self.bgcolor = bgcolor if bgcolor is not None else tft.getBGColor()
#         self.fontcolor = fontcolor if fontcolor is not None else tft.getColor()
#         self.border = 0 if border is None else int(max(border, 0)) # width
#         self.callback = dolittle # Value change callback
#         self.args = []
#         self.cb_end = dolittle # Touch release callbacks
#         self.cbe_args = []
# 
#     @property
#     def tft(self):
#         return Screen.get_tft(self._greyed_out)
# 
#     def greyed_out(self):
#         return self._greyed_out # Subclass may be greyed out
# 
#     def value(self, val=None, show=True): # User method to get or set value
#         if val is not None:
#             if type(val) is float:
#                 val = min(max(val, 0.0), 1.0)
#             if val != self._value:
#                 self._value = val
#                 self._value_change(show)
#         return self._value
# 
#     def _value_change(self, show): # Optional override in subclass
#         self.callback(self, *self.args) # CB is not a bound method. 1st arg is self
#         if show:
#             self.show_if_current()
# 
#     def show_if_current(self):
#         if self.screen is Screen.current_screen:
#             self.show()
# 
# # Called by Screen.show(). Draw background and bounding box if required
#     def draw_border(self):
#         if self.screen is Screen.current_screen:
#             tft = self.tft
#             x = self.location[0]
#             y = self.location[1]
#             if self.fill:
#                 tft.fill_rectangle(x, y, x + self.width, y + self.height, self.bgcolor)
#             if self.border > 0: # Draw a bounding box
#                 tft.draw_rectangle(x, y, x + self.width, y + self.height, self.fgcolor)
#         return self.border # border width in pixels
# 
#     def overlaps(self, xa, ya, xb, yb): # Args must be sorted: xb > xa and yb > ya
#         x0 = self.location[0]
#         y0 = self.location[1]
#         x1 = x0 + self.width
#         y1 = y0 + self.height
#         if (ya <= y1 and yb >= y0) and (xa <= x1 and xb >= x0):
#             return True
#         return False
# 
# def print_centered(tft, x, y, s, color, font, clip=False, scroll=False):
#     old_style = tft.getTextStyle()
#     length, height = get_stringsize(s, font)
#     tft.setTextStyle(color, None, 2, font)
#     tft.setTextPos(max(x - length // 2, 0), max(y - height // 2, 0), clip, scroll)
#     tft.printString(s)
#     tft.setTextStyle(*old_style)
# 
# Path: tft/driver/TFT_io.py
# RESET  = const(1 << 10)  ## Y9
# RD     = const(1 << 11)  ## Y10
# WR     = const(0x01)  ## Y11
# D_C    = const(0x02)  ## Y12
# LED    = const(1 << 8) ## Y3
# POWER  = const(1 << 9) ## Y4
# PORTRAIT = const(1)
# LANDSCAPE = const(0)
# def displaySCR_charbitmap(bits: ptr8, size: int, control: ptr8, bg_buf: ptr8):
# def displaySCR_bmp(data: ptr8, size: int, bits: int, colortable: ptr8):
# def setXY_L(r0, r1, r2, r3):  
# def setXY_P(r0, r1, r2, r3):  
# def drawPixel_L(r0, r1, r2):  
# def drawPixel_P(r0, r1, r2):  
# def fillSCR_AS(r0, r1):  # r0: ptr to data, r1: number of pixels (3 bytes/pixel)
# def displaySCR_AS(r0, r1):  # r0: ptr to data, r1: is number of pixels (3 bytes/pixel)
# def displaySCR565_AS(r0, r1):  # r0: ptr to data, r1: is number of pixels (3 bytes/pixel)
# def tft_cmd_data(cmd: int, data: ptr8, size: int):
# def tft_cmd_data_AS(r0, r1, r2):  # r0: command, r1: ptr to data, r2 is size in bytes
# def tft_cmd(cmd: int):
# def tft_write_data_AS(r0, r1):  # r0: ptr to data, r1: is size in Bytes
# def tft_read_cmd_data_AS(r0, r1, r2):  
# def swapbytes(r0, r1):               # bytearray, len(bytearray)
# def swapcolors(r0, r1):               # bytearray, len(bytearray)


from tft.driver.ugui import NoTouch, print_centered
from tft.driver import TFT_io
from tft.driver.constants import *
# meter.py For TFT driver.
# Adapted for (and requires) uasyncio V3

# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2016-2020 Peter Hinch


class Meter(NoTouch):
    def __init__(self, location, *, font=None, height=200, width=30,
                 fgcolor=None, bgcolor=None, pointercolor=None, fontcolor=None,
                 divisions=10, legends=None, value=0):
        border = 5 if font is None else 1 + font.height() / 2

# Path: tft/driver/ugui.py
# class NoTouch:
#     def __init__(self, location, font, height, width, fgcolor, bgcolor, fontcolor, border, value, initial_value):
#         Screen.addobject(self)
#         self.screen = Screen.current_screen
#         self.redraw = True # Force drawing of static part of image
#         self.location = location
#         self._value = value
#         self._initial_value = initial_value # Optionally enables show() method to handle initialisation
#         self.font = font
#         self.height = height
#         self.width = width
#         self.fill = bgcolor is not None
#         self.visible = True # Used by ButtonList class for invisible buttons
#         self._greyed_out = False # Disabled by user code
#         tft = Screen.get_tft(False) # Not greyed out
#         self.fgcolor = fgcolor if fgcolor is not None else tft.getColor()
#         self.bgcolor = bgcolor if bgcolor is not None else tft.getBGColor()
#         self.fontcolor = fontcolor if fontcolor is not None else tft.getColor()
#         self.border = 0 if border is None else int(max(border, 0)) # width
#         self.callback = dolittle # Value change callback
#         self.args = []
#         self.cb_end = dolittle # Touch release callbacks
#         self.cbe_args = []
# 
#     @property
#     def tft(self):
#         return Screen.get_tft(self._greyed_out)
# 
#     def greyed_out(self):
#         return self._greyed_out # Subclass may be greyed out
# 
#     def value(self, val=None, show=True): # User method to get or set value
#         if val is not None:
#             if type(val) is float:
#                 val = min(max(val, 0.0), 1.0)
#             if val != self._value:
#                 self._value = val
#                 self._value_change(show)
#         return self._value
# 
#     def _value_change(self, show): # Optional override in subclass
#         self.callback(self, *self.args) # CB is not a bound method. 1st arg is self
#         if show:
#             self.show_if_current()
# 
#     def show_if_current(self):
#         if self.screen is Screen.current_screen:
#             self.show()
# 
# # Called by Screen.show(). Draw background and bounding box if required
#     def draw_border(self):
#         if self.screen is Screen.current_screen:
#             tft = self.tft
#             x = self.location[0]
#             y = self.location[1]
#             if self.fill:
#                 tft.fill_rectangle(x, y, x + self.width, y + self.height, self.bgcolor)
#             if self.border > 0: # Draw a bounding box
#                 tft.draw_rectangle(x, y, x + self.width, y + self.height, self.fgcolor)
#         return self.border # border width in pixels
# 
#     def overlaps(self, xa, ya, xb, yb): # Args must be sorted: xb > xa and yb > ya
#         x0 = self.location[0]
#         y0 = self.location[1]
#         x1 = x0 + self.width
#         y1 = y0 + self.height
#         if (ya <= y1 and yb >= y0) and (xa <= x1 and xb >= x0):
#             return True
#         return False
# 
# def print_centered(tft, x, y, s, color, font, clip=False, scroll=False):
#     old_style = tft.getTextStyle()
#     length, height = get_stringsize(s, font)
#     tft.setTextStyle(color, None, 2, font)
#     tft.setTextPos(max(x - length // 2, 0), max(y - height // 2, 0), clip, scroll)
#     tft.printString(s)
#     tft.setTextStyle(*old_style)
# 
# Path: tft/driver/TFT_io.py
# RESET  = const(1 << 10)  ## Y9
# RD     = const(1 << 11)  ## Y10
# WR     = const(0x01)  ## Y11
# D_C    = const(0x02)  ## Y12
# LED    = const(1 << 8) ## Y3
# POWER  = const(1 << 9) ## Y4
# PORTRAIT = const(1)
# LANDSCAPE = const(0)
# def displaySCR_charbitmap(bits: ptr8, size: int, control: ptr8, bg_buf: ptr8):
# def displaySCR_bmp(data: ptr8, size: int, bits: int, colortable: ptr8):
# def setXY_L(r0, r1, r2, r3):  
# def setXY_P(r0, r1, r2, r3):  
# def drawPixel_L(r0, r1, r2):  
# def drawPixel_P(r0, r1, r2):  
# def fillSCR_AS(r0, r1):  # r0: ptr to data, r1: number of pixels (3 bytes/pixel)
# def displaySCR_AS(r0, r1):  # r0: ptr to data, r1: is number of pixels (3 bytes/pixel)
# def displaySCR565_AS(r0, r1):  # r0: ptr to data, r1: is number of pixels (3 bytes/pixel)
# def tft_cmd_data(cmd: int, data: ptr8, size: int):
# def tft_cmd_data_AS(r0, r1, r2):  # r0: command, r1: ptr to data, r2 is size in bytes
# def tft_cmd(cmd: int):
# def tft_write_data_AS(r0, r1):  # r0: ptr to data, r1: is size in Bytes
# def tft_read_cmd_data_AS(r0, r1, r2):  
# def swapbytes(r0, r1):               # bytearray, len(bytearray)
# def swapcolors(r0, r1):               # bytearray, len(bytearray)


# Path: tft/widgets/meter.py
from tft.driver.ugui import NoTouch, print_centered
from tft.driver import TFT_io
from tft.driver.constants import *

# meter.py For TFT driver.
# Adapted for (and requires) uasyncio V3

# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2016-2020 Peter Hinch


class Meter(NoTouch):
    def __init__(self, location, *, font=None, height=200, width=30,
                 fgcolor=None, bgcolor=None, pointercolor=None, fontcolor=None,
                 divisions=10, legends=None, value=0):
        border = 5 if font is None else 1 + font.height() / 2

        NoTouch.__init__(self, location, font, height, width, fgcolor, bgcolor, fontcolor, border, value, None) # super() provoked Python bug