Merge pull request #244 from ch3lmi/main
Support for WaveShare 12.48" V2 Colour Display
This commit is contained in:
		
							
								
								
									
										503
									
								
								inkycal/display/drivers/epd_12_in_48_colour_V2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										503
									
								
								inkycal/display/drivers/epd_12_in_48_colour_V2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,503 @@ | |||||||
|  | # /***************************************************************************** | ||||||
|  | # * | File        :	  epd_12_in_48_colour.py | ||||||
|  | # * | Author      :   Waveshare electrices, modified by Sebastien Harnist | ||||||
|  | # * | Function    :   Hardware underlying interface | ||||||
|  | # * | Info        : | ||||||
|  | # *---------------- | ||||||
|  | # * | This version:   V1.1 | ||||||
|  | # * | Date        :   2022-11-23 | ||||||
|  | # * | 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 time | ||||||
|  | from inkycal.display.drivers import epdconfig_12_in_48 as epdconfig | ||||||
|  |  | ||||||
|  | EPD_WIDTH       = 1304 | ||||||
|  | EPD_HEIGHT      = 984 | ||||||
|  |  | ||||||
|  | class EPD(object): | ||||||
|  |     def __init__(self): | ||||||
|  |         self.width = EPD_WIDTH | ||||||
|  |         self.height = EPD_HEIGHT | ||||||
|  |          | ||||||
|  |         self.EPD_M1_CS_PIN  = epdconfig.EPD_M1_CS_PIN | ||||||
|  |         self.EPD_S1_CS_PIN  = epdconfig.EPD_S1_CS_PIN | ||||||
|  |         self.EPD_M2_CS_PIN  = epdconfig.EPD_M2_CS_PIN | ||||||
|  |         self.EPD_S2_CS_PIN  = epdconfig.EPD_S2_CS_PIN | ||||||
|  |  | ||||||
|  |         self.EPD_M1S1_DC_PIN  = epdconfig.EPD_M1S1_DC_PIN | ||||||
|  |         self.EPD_M2S2_DC_PIN  = epdconfig.EPD_M2S2_DC_PIN | ||||||
|  |  | ||||||
|  |         self.EPD_M1S1_RST_PIN = epdconfig.EPD_M1S1_RST_PIN | ||||||
|  |         self.EPD_M2S2_RST_PIN = epdconfig.EPD_M2S2_RST_PIN | ||||||
|  |  | ||||||
|  |         self.EPD_M1_BUSY_PIN  = epdconfig.EPD_M1_BUSY_PIN | ||||||
|  |         self.EPD_S1_BUSY_PIN  = epdconfig.EPD_S1_BUSY_PIN | ||||||
|  |         self.EPD_M2_BUSY_PIN  = epdconfig.EPD_M2_BUSY_PIN | ||||||
|  |         self.EPD_S2_BUSY_PIN  = epdconfig.EPD_S2_BUSY_PIN | ||||||
|  |  | ||||||
|  |     def init(self): | ||||||
|  |         print("EPD init...") | ||||||
|  |         epdconfig.module_init() | ||||||
|  |          | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1)  | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 1)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1)  | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 1)  | ||||||
|  |         self.Reset()  | ||||||
|  |  | ||||||
|  |         # panel setting for Display | ||||||
|  |         self.M1_SendCommand(0x00) | ||||||
|  |         self.M1_SendData(0x0f)	#KW-3f   KWR-2F	BWROTP 0f	BWOTP 1f | ||||||
|  |         self.S1_SendCommand(0x00) | ||||||
|  |         self.S1_SendData(0x0f) | ||||||
|  |         self.M2_SendCommand(0x00) | ||||||
|  |         self.M2_SendData(0x03) | ||||||
|  |         self.S2_SendCommand(0x00) | ||||||
|  |         self.S2_SendData(0x03) | ||||||
|  |  | ||||||
|  |         # booster soft start | ||||||
|  |         self.M1_SendCommand(0x06) | ||||||
|  |         self.M1_SendData(0x17)	#A | ||||||
|  |         self.M1_SendData(0x17)	#B | ||||||
|  |         self.M1_SendData(0x39)	#C | ||||||
|  |         self.M1_SendData(0x17) | ||||||
|  |         self.M2_SendCommand(0x06) | ||||||
|  |         self.M2_SendData(0x17) | ||||||
|  |         self.M2_SendData(0x17) | ||||||
|  |         self.M2_SendData(0x39) | ||||||
|  |         self.M2_SendData(0x17) | ||||||
|  |  | ||||||
|  |         #resolution setting | ||||||
|  |         self.M1_SendCommand(0x61) | ||||||
|  |         self.M1_SendData(0x02) | ||||||
|  |         self.M1_SendData(0x88)	#source 648 | ||||||
|  |         self.M1_SendData(0x01)	#gate 492 | ||||||
|  |         self.M1_SendData(0xEC) | ||||||
|  |         self.S1_SendCommand(0x61) | ||||||
|  |         self.S1_SendData(0x02) | ||||||
|  |         self.S1_SendData(0x90)	#source 656 | ||||||
|  |         self.S1_SendData(0x01)	#gate 492 | ||||||
|  |         self.S1_SendData(0xEC) | ||||||
|  |         self.M2_SendCommand(0x61) | ||||||
|  |         self.M2_SendData(0x02) | ||||||
|  |         self.M2_SendData(0x90)	#source 656 | ||||||
|  |         self.M2_SendData(0x01)	#gate 492 | ||||||
|  |         self.M2_SendData(0xEC) | ||||||
|  |         self.S2_SendCommand(0x61) | ||||||
|  |         self.S2_SendData(0x02) | ||||||
|  |         self.S2_SendData(0x88)	#source 648 | ||||||
|  |         self.S2_SendData(0x01)	#gate 492 | ||||||
|  |         self.S2_SendData(0xEC) | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x15)	#DUSPI | ||||||
|  |         self.M1S1M2S2_SendData(0x20) | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x50)	#Vcom and data interval setting | ||||||
|  |         self.M1S1M2S2_SendData(0x11) | ||||||
|  |         self.M1S1M2S2_SendData(0x07) | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x60)#TCON | ||||||
|  |         self.M1S1M2S2_SendData(0x22) | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0xE3) | ||||||
|  |         self.M1S1M2S2_SendData(0x00) | ||||||
|  |  | ||||||
|  |         self.M1_ReadTemperature() | ||||||
|  |          | ||||||
|  |         self.SetLut() | ||||||
|  |  | ||||||
|  |     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() | ||||||
|  |  | ||||||
|  |         if(imwidth == self.width and imheight == self.height):             | ||||||
|  |             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):             | ||||||
|  |             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, blackbuf, redbuf): | ||||||
|  |                          | ||||||
|  |         #S2 part 648*492 | ||||||
|  |         self.S2_SendCommand(0x10) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.S2_SendData(blackbuf[y*163 + x]) | ||||||
|  |         self.S2_SendCommand(0x13) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.S2_SendData(~redbuf[y*163 + x]) | ||||||
|  |                  | ||||||
|  |         #M2 part 656*492 | ||||||
|  |         self.M2_SendCommand(0x10) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.M2_SendData(blackbuf[y*163 + x]) | ||||||
|  |         self.M2_SendCommand(0x13) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.M2_SendData(~redbuf[y*163 + x]) | ||||||
|  |  | ||||||
|  |         #M1 part 648*492     | ||||||
|  |         self.M1_SendCommand(0x10) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.M1_SendData(blackbuf[y*163 + x]) | ||||||
|  |         self.M1_SendCommand(0x13) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.M1_SendData(~redbuf[y*163 + x]) | ||||||
|  |          | ||||||
|  |         #S1 part 656*492 | ||||||
|  |         self.S1_SendCommand(0x10) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.S1_SendData(blackbuf[y*163 + x]) | ||||||
|  |         self.S1_SendCommand(0x13) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.S1_SendData(~redbuf[y*163 + x]) | ||||||
|  |         self.TurnOnDisplay() | ||||||
|  |  | ||||||
|  |     def clear(self): | ||||||
|  |         """Clear contents of image buffer""" | ||||||
|  |          | ||||||
|  |         self.S2_SendCommand(0x10) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.S2_SendData(0xff)         | ||||||
|  |         self.S2_SendCommand(0x13) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.S2_SendData(0x00) | ||||||
|  |                  | ||||||
|  |         self.M2_SendCommand(0x10) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.M2_SendData(0xff) | ||||||
|  |         self.M2_SendCommand(0x13) | ||||||
|  |         for y in  range(0, 492): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.M2_SendData(0x00)        | ||||||
|  |                      | ||||||
|  |         self.M1_SendCommand(0x10) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.M1_SendData(0xff) | ||||||
|  |         self.M1_SendCommand(0x13) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(0, 81): | ||||||
|  |                 self.M1_SendData(0x00) | ||||||
|  |                  | ||||||
|  |         self.S1_SendCommand(0x10) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.S1_SendData(0xff) | ||||||
|  |         self.S1_SendCommand(0x13) | ||||||
|  |         for y in  range(492, 984): | ||||||
|  |             for x in  range(81, 163): | ||||||
|  |                 self.S1_SendData(0x00) | ||||||
|  |          | ||||||
|  |         self.TurnOnDisplay() | ||||||
|  |          | ||||||
|  |     def Reset(self): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_RST_PIN, 1)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_RST_PIN, 1)  | ||||||
|  |         time.sleep(0.2)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_RST_PIN, 0)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_RST_PIN, 0)  | ||||||
|  |         time.sleep(0.01)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_RST_PIN, 1)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_RST_PIN, 1)  | ||||||
|  |         time.sleep(0.2)  | ||||||
|  |      | ||||||
|  |     def sleep(self): | ||||||
|  |         self.M1S1M2S2_SendCommand(0X02)   	 | ||||||
|  |         time.sleep(0.3)  | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0X07)   	 | ||||||
|  |         self.M1S1M2S2_SendData(0xA5)  | ||||||
|  |         time.sleep(0.3)  | ||||||
|  |         print("module_exit") | ||||||
|  |         epdconfig.module_exit() | ||||||
|  |  | ||||||
|  |     def TurnOnDisplay(self): | ||||||
|  |         self.M1M2_SendCommand(0x04)   | ||||||
|  |         time.sleep(0.3)  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x12)  | ||||||
|  |         self.M1_ReadBusy() | ||||||
|  |         self.S1_ReadBusy() | ||||||
|  |         self.M2_ReadBusy() | ||||||
|  |         self.S2_ReadBusy()    | ||||||
|  |          | ||||||
|  |     """   M1S1M2S2 Write register address and data     """ | ||||||
|  |     def M1S1M2S2_SendCommand(self, cmd): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 0) | ||||||
|  |          | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(cmd)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 1) | ||||||
|  |      | ||||||
|  |     def M1S1M2S2_SendData(self, val): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 1) | ||||||
|  |  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(val)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 1) | ||||||
|  |  | ||||||
|  |     """   M1M2 Write register address and data     """ | ||||||
|  |     def M1M2_SendCommand(self, cmd): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(cmd)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1) | ||||||
|  |          | ||||||
|  |     def M1M2_Sendata(self, val):  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(val)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1)    | ||||||
|  |            | ||||||
|  |     """   S2 Write register address and data     """ | ||||||
|  |     def S2_SendCommand(self, cmd): | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(cmd) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 1) | ||||||
|  |     def S2_SendData(self, val): | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(val) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 1) | ||||||
|  |          | ||||||
|  |     """   M2 Write register address and data     """ | ||||||
|  |     def M2_SendCommand(self, cmd): | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(cmd)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1) | ||||||
|  |     def M2_SendData(self, val): | ||||||
|  |         epdconfig.digital_write(self.EPD_M2S2_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(val)  | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1) | ||||||
|  |  | ||||||
|  |     """   S1 Write register address and data     """ | ||||||
|  |     def S1_SendCommand(self, cmd): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(cmd) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 1) | ||||||
|  |     def S1_SendData(self, val): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(val) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 1) | ||||||
|  |          | ||||||
|  |     """   M1 Write register address and data     """ | ||||||
|  |     def M1_SendCommand(self, cmd): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(cmd) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |     def M1_SendData(self, val): | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.spi_writebyte(val) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |  | ||||||
|  |     #Busy | ||||||
|  |     def M1_ReadBusy(self): | ||||||
|  |         self.M1_SendCommand(0x71)  | ||||||
|  |         busy = epdconfig.digital_read(self.EPD_M1_BUSY_PIN)  | ||||||
|  |         busy = not(busy & 0x01)  | ||||||
|  |         while(busy): | ||||||
|  |             self.M1_SendCommand(0x71)  | ||||||
|  |             busy = epdconfig.digital_read(self.EPD_M1_BUSY_PIN)  | ||||||
|  |             busy = not(busy & 0x01)  | ||||||
|  |         time.sleep(0.2) | ||||||
|  |     def M2_ReadBusy(self): | ||||||
|  |         self.M2_SendCommand(0x71)  | ||||||
|  |         busy = epdconfig.digital_read(self.EPD_M2_BUSY_PIN)  | ||||||
|  |         busy = not(busy & 0x01)  | ||||||
|  |         self.M2_SendCommand(0x71)  | ||||||
|  |         while(busy): | ||||||
|  |             self.M2_SendCommand(0x71)  | ||||||
|  |             busy = epdconfig.digital_read(self.EPD_M2_BUSY_PIN)  | ||||||
|  |             busy =not(busy & 0x01)  | ||||||
|  |         time.sleep(0.2) | ||||||
|  |     def S1_ReadBusy(self): | ||||||
|  |         self.S1_SendCommand(0x71)  | ||||||
|  |         busy = epdconfig.digital_read(self.EPD_S1_BUSY_PIN)  | ||||||
|  |         busy = not(busy & 0x01)  | ||||||
|  |         while(busy): | ||||||
|  |             self.S1_SendCommand(0x71)  | ||||||
|  |             busy = epdconfig.digital_read(self.EPD_S1_BUSY_PIN)  | ||||||
|  |             busy = not(busy & 0x01)  | ||||||
|  |         time.sleep(0.2)         | ||||||
|  |     def S2_ReadBusy(self): | ||||||
|  |         self.S2_SendCommand(0x71)  | ||||||
|  |         busy = epdconfig.digital_read(self.EPD_S2_BUSY_PIN)  | ||||||
|  |         busy = not(busy & 0x01)  | ||||||
|  |         while(busy): | ||||||
|  |             self.S2_SendCommand(0x71)  | ||||||
|  |             busy = epdconfig.digital_read(self.EPD_S2_BUSY_PIN)  | ||||||
|  |             busy = not(busy & 0x01)  | ||||||
|  |         time.sleep(0.2)             | ||||||
|  |  | ||||||
|  |     lut_vcom1 = [ | ||||||
|  |         0x00,	0x10,	0x10,	0x01,	0x08,	0x01, | ||||||
|  |         0x00,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x00,	0x08,	0x01,	0x08,	0x01,	0x06, | ||||||
|  |         0x00,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x06, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x01, | ||||||
|  |         0x00,	0x04,	0x05,	0x08,	0x08,	0x01, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |     ] | ||||||
|  |     lut_ww1 = [ | ||||||
|  |         0x91,	0x10,	0x10,	0x01,	0x08,	0x01, | ||||||
|  |         0x04,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x84,	0x08,	0x01,	0x08,	0x01,	0x06, | ||||||
|  |         0x80,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x06, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x01, | ||||||
|  |         0x08,	0x04,	0x05,	0x08,	0x08,	0x01, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |     ] | ||||||
|  |     lut_bw1 = [ | ||||||
|  |         0xA8,	0x10,	0x10,	0x01,	0x08,	0x01, | ||||||
|  |         0x84,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x84,	0x08,	0x01,	0x08,	0x01,	0x06, | ||||||
|  |         0x86,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x8C,	0x05,	0x01,	0x1E,	0x0F,	0x06, | ||||||
|  |         0x8C,	0x05,	0x01,	0x1E,	0x0F,	0x01, | ||||||
|  |         0xF0,	0x04,	0x05,	0x08,	0x08,	0x01, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |     ] | ||||||
|  |     lut_wb1 = [ | ||||||
|  |         0x91,	0x10,	0x10,	0x01,	0x08,	0x01, | ||||||
|  |         0x04,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x84,	0x08,	0x01,	0x08,	0x01,	0x06, | ||||||
|  |         0x80,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x06, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x01, | ||||||
|  |         0x08,	0x04,	0x05,	0x08,	0x08,	0x01, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |     ] | ||||||
|  |     lut_bb1 = [ | ||||||
|  |         0x92,	0x10,	0x10,	0x01,	0x08,	0x01, | ||||||
|  |         0x80,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x84,	0x08,	0x01,	0x08,	0x01,	0x06, | ||||||
|  |         0x04,	0x06,	0x01,	0x06,	0x01,	0x05, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x06, | ||||||
|  |         0x00,	0x05,	0x01,	0x1E,	0x0F,	0x01, | ||||||
|  |         0x01,	0x04,	0x05,	0x08,	0x08,	0x01, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |         0x00,	0x00,	0x00,	0x00,	0x00,	0x00, | ||||||
|  |     ] | ||||||
|  |      | ||||||
|  |     def SetLut(self): | ||||||
|  |         self.M1S1M2S2_SendCommand(0x20) #vcom | ||||||
|  |         for count in range(0, 60): | ||||||
|  |             self.M1S1M2S2_SendData(self.lut_vcom1[count]) | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x21) #red not use | ||||||
|  |         for count in range(0, 60): | ||||||
|  |             self.M1S1M2S2_SendData(self.lut_ww1[count]) | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x22) #bw r | ||||||
|  |         for count in range(0, 60): | ||||||
|  |             self.M1S1M2S2_SendData(self.lut_bw1[count])   # bw=r | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x23) #wb w | ||||||
|  |         for count in range(0, 60): | ||||||
|  |             self.M1S1M2S2_SendData(self.lut_wb1[count])   # wb=w | ||||||
|  |  | ||||||
|  |         self.M1S1M2S2_SendCommand(0x24) #bb b | ||||||
|  |         for count in range(0, 60): | ||||||
|  |             self.M1S1M2S2_SendData(self.lut_bb1[count])   # bb=b | ||||||
|  |              | ||||||
|  |         self.M1S1M2S2_SendCommand(0x25) #bb b | ||||||
|  |         for count in range(0, 60): | ||||||
|  |             self.M1S1M2S2_SendData(self.lut_ww1[count])   # bb=b | ||||||
|  |  | ||||||
|  |     def M1_ReadTemperature(self): | ||||||
|  |         self.M1_SendCommand(0x40) | ||||||
|  |         self.M1_ReadBusy() | ||||||
|  |         time.sleep(0.3) | ||||||
|  |          | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 0) | ||||||
|  |         epdconfig.digital_write(self.EPD_S1_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_M2_CS_PIN, 1) | ||||||
|  |         epdconfig.digital_write(self.EPD_S2_CS_PIN, 1) | ||||||
|  |          | ||||||
|  |         epdconfig.digital_write(self.EPD_M1S1_DC_PIN, 1) | ||||||
|  |         time.sleep(0.05) | ||||||
|  |          | ||||||
|  |         temp = epdconfig.spi_readbyte(0x00) | ||||||
|  |         epdconfig.digital_write(self.EPD_M1_CS_PIN, 1) | ||||||
|  |          | ||||||
|  |         self.M1S1M2S2_SendCommand(0xE0) | ||||||
|  |         self.M1S1M2S2_SendData(0x03) | ||||||
|  |         self.M1S1M2S2_SendCommand(0xE5) | ||||||
|  |         self.M1S1M2S2_SendData(temp) | ||||||
| @@ -1,508 +0,0 @@ | |||||||
| #!python3 |  | ||||||
|  |  | ||||||
| """ |  | ||||||
| Inkycal weather module |  | ||||||
| Copyright by aceisace |  | ||||||
| """ |  | ||||||
|  |  | ||||||
| from inkycal.modules.template import inkycal_module |  | ||||||
| from inkycal.custom import * |  | ||||||
|  |  | ||||||
| import math |  | ||||||
| import decimal |  | ||||||
| import arrow |  | ||||||
|  |  | ||||||
| from pyowm.owm import OWM |  | ||||||
|  |  | ||||||
| logger = logging.getLogger(__name__) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Weather(inkycal_module): |  | ||||||
|     """Weather class |  | ||||||
|     parses weather details from openweathermap |  | ||||||
|     """ |  | ||||||
|     name = "Weather (openweathermap) - Get weather forecasts from openweathermap" |  | ||||||
|  |  | ||||||
|     requires = { |  | ||||||
|  |  | ||||||
|         "api_key": { |  | ||||||
|             "label": "Please enter openweathermap api-key. You can create one for free on openweathermap", |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|         "location": { |  | ||||||
|             "label": "Please enter your location in the following format: City, Country-Code. " + |  | ||||||
|                      "You can also enter the location ID found in the url " + |  | ||||||
|                      "e.g. https://openweathermap.org/city/4893171 -> ID is 4893171" |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     optional = { |  | ||||||
|  |  | ||||||
|         "round_temperature": { |  | ||||||
|             "label": "Round temperature to the nearest degree?", |  | ||||||
|             "options": [True, False], |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|         "round_windspeed": { |  | ||||||
|             "label": "Round windspeed?", |  | ||||||
|             "options": [True, False], |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|         "forecast_interval": { |  | ||||||
|             "label": "Please select the forecast interval", |  | ||||||
|             "options": ["daily", "hourly"], |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|         "units": { |  | ||||||
|             "label": "Which units should be used?", |  | ||||||
|             "options": ["metric", "imperial"], |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|         "hour_format": { |  | ||||||
|             "label": "Which hour format do you prefer?", |  | ||||||
|             "options": [24, 12], |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|         "use_beaufort": { |  | ||||||
|             "label": "Use beaufort scale for windspeed?", |  | ||||||
|             "options": [True, False], |  | ||||||
|         }, |  | ||||||
|  |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     def __init__(self, config): |  | ||||||
|         """Initialize inkycal_weather module""" |  | ||||||
|  |  | ||||||
|         super().__init__(config) |  | ||||||
|  |  | ||||||
|         config = config['config'] |  | ||||||
|  |  | ||||||
|         # Check if all required parameters are present |  | ||||||
|         for param in self.requires: |  | ||||||
|             if not param in config: |  | ||||||
|                 raise Exception(f'config is missing {param}') |  | ||||||
|  |  | ||||||
|         # required parameters |  | ||||||
|         self.api_key = config['api_key'] |  | ||||||
|         self.location = config['location'] |  | ||||||
|  |  | ||||||
|         # optional parameters |  | ||||||
|         self.round_temperature = config['round_temperature'] |  | ||||||
|         self.round_windspeed = config['round_windspeed'] |  | ||||||
|         self.forecast_interval = config['forecast_interval'] |  | ||||||
|         self.units = config['units'] |  | ||||||
|         self.hour_format = int(config['hour_format']) |  | ||||||
|         self.use_beaufort = config['use_beaufort'] |  | ||||||
|  |  | ||||||
|         # additional configuration |  | ||||||
|         self.owm = OWM(self.api_key).weather_manager() |  | ||||||
|         self.timezone = get_system_tz() |  | ||||||
|         self.locale = config['language'] |  | ||||||
|         self.weatherfont = ImageFont.truetype( |  | ||||||
|             fonts['weathericons-regular-webfont'], size=self.fontsize) |  | ||||||
|  |  | ||||||
|         # give an OK message |  | ||||||
|         print(f"{__name__} loaded") |  | ||||||
|  |  | ||||||
|     def generate_image(self): |  | ||||||
|         """Generate image for this module""" |  | ||||||
|  |  | ||||||
|         # Define new image size with respect to padding |  | ||||||
|         im_width = int(self.width - (2 * self.padding_left)) |  | ||||||
|         im_height = int(self.height - (2 * self.padding_top)) |  | ||||||
|         im_size = im_width, im_height |  | ||||||
|         logger.info(f'Image size: {im_size}') |  | ||||||
|  |  | ||||||
|         # Create an image for black pixels and one for coloured pixels |  | ||||||
|         im_black = Image.new('RGB', size=im_size, color='white') |  | ||||||
|         im_colour = Image.new('RGB', size=im_size, color='white') |  | ||||||
|  |  | ||||||
|         # Check if internet is available |  | ||||||
|         if internet_available(): |  | ||||||
|             logger.info('Connection test passed') |  | ||||||
|         else: |  | ||||||
|             raise NetworkNotReachableError |  | ||||||
|  |  | ||||||
|         def get_moon_phase(): |  | ||||||
|             """Calculate the current (approximate) moon phase""" |  | ||||||
|  |  | ||||||
|             dec = decimal.Decimal |  | ||||||
|             diff = now - arrow.get(2001, 1, 1) |  | ||||||
|             days = dec(diff.days) + (dec(diff.seconds) / dec(86400)) |  | ||||||
|             lunations = dec("0.20439731") + (days * dec("0.03386319269")) |  | ||||||
|             position = lunations % dec(1) |  | ||||||
|             index = math.floor((position * dec(8)) + dec("0.5")) |  | ||||||
|             return {0: '\uf095', 1: '\uf099', 2: '\uf09c', 3: '\uf0a0', |  | ||||||
|                     4: '\uf0a3', 5: '\uf0a7', 6: '\uf0aa', 7: '\uf0ae'}[int(index) & 7] |  | ||||||
|  |  | ||||||
|         def is_negative(temp): |  | ||||||
|             """Check if temp is below freezing point of water (0°C/30°F) |  | ||||||
|             returns True if temp below freezing point, else False""" |  | ||||||
|             answer = False |  | ||||||
|  |  | ||||||
|             if temp_unit == 'celsius' and round(float(temp.split('°')[0])) <= 0: |  | ||||||
|                 answer = True |  | ||||||
|             elif temp_unit == 'fahrenheit' and round(float(temp.split('°')[0])) <= 0: |  | ||||||
|                 answer = True |  | ||||||
|             return answer |  | ||||||
|  |  | ||||||
|         # Lookup-table for weather icons and weather codes |  | ||||||
|         weathericons = { |  | ||||||
|             '01d': '\uf00d', '02d': '\uf002', '03d': '\uf013', |  | ||||||
|             '04d': '\uf012', '09d': '\uf01a ', '10d': '\uf019', |  | ||||||
|             '11d': '\uf01e', '13d': '\uf01b', '50d': '\uf014', |  | ||||||
|             '01n': '\uf02e', '02n': '\uf013', '03n': '\uf013', |  | ||||||
|             '04n': '\uf013', '09n': '\uf037', '10n': '\uf036', |  | ||||||
|             '11n': '\uf03b', '13n': '\uf038', '50n': '\uf023' |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         def draw_icon(image, xy, box_size, icon, rotation=None): |  | ||||||
|             """Custom function to add icons of weather font on image |  | ||||||
|             image = on which image should the text be added? |  | ||||||
|             xy = xy-coordinates as tuple -> (x,y) |  | ||||||
|             box_size = size of text-box -> (width,height) |  | ||||||
|             icon = icon-unicode, looks this up in weathericons dictionary |  | ||||||
|             """ |  | ||||||
|             x, y = xy |  | ||||||
|             box_width, box_height = box_size |  | ||||||
|             text = icon |  | ||||||
|             font = self.weatherfont |  | ||||||
|  |  | ||||||
|             # Increase fontsize to fit specified height and width of text box |  | ||||||
|             size = 8 |  | ||||||
|             font = ImageFont.truetype(font.path, size) |  | ||||||
|             text_width, text_height = font.getsize(text) |  | ||||||
|  |  | ||||||
|             while (text_width < int(box_width * 0.9) and |  | ||||||
|                    text_height < int(box_height * 0.9)): |  | ||||||
|                 size += 1 |  | ||||||
|                 font = ImageFont.truetype(font.path, size) |  | ||||||
|                 text_width, text_height = font.getsize(text) |  | ||||||
|  |  | ||||||
|             text_width, text_height = font.getsize(text) |  | ||||||
|  |  | ||||||
|             # Align text to desired position |  | ||||||
|             x = int((box_width / 2) - (text_width / 2)) |  | ||||||
|             y = int((box_height / 2) - (text_height / 2)) |  | ||||||
|  |  | ||||||
|             # Draw the text in the text-box |  | ||||||
|             draw = ImageDraw.Draw(image) |  | ||||||
|             space = Image.new('RGBA', (box_width, box_height)) |  | ||||||
|             ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) |  | ||||||
|  |  | ||||||
|             if rotation != None: |  | ||||||
|                 space.rotate(rotation, expand=True) |  | ||||||
|  |  | ||||||
|             # Update only region with text (add text with transparent background) |  | ||||||
|             image.paste(space, xy, space) |  | ||||||
|  |  | ||||||
|         #   column1    column2    column3    column4    column5    column6    column7 |  | ||||||
|         # |----------|----------|----------|----------|----------|----------|----------| |  | ||||||
|         # |  time    | temperat.| moonphase| forecast1| forecast2| forecast3| forecast4| |  | ||||||
|         # | current  |----------|----------|----------|----------|----------|----------| |  | ||||||
|         # | weather  | humidity |  sunrise |  icon1   |  icon2   |  icon3   |  icon4   | |  | ||||||
|         # |  icon    |----------|----------|----------|----------|----------|----------| |  | ||||||
|         # |          | windspeed|  sunset  | temperat.| temperat.| temperat.| temperat.| |  | ||||||
|         # |----------|----------|----------|----------|----------|----------|----------| |  | ||||||
|  |  | ||||||
|         # Calculate size rows and columns |  | ||||||
|         col_width = im_width // 7 |  | ||||||
|  |  | ||||||
|         # Ratio width height |  | ||||||
|         image_ratio = im_width / im_height |  | ||||||
|  |  | ||||||
|         if image_ratio >= 4: |  | ||||||
|             row_height = im_height // 3 |  | ||||||
|         else: |  | ||||||
|             logger.info('Please consider decreasing the height.') |  | ||||||
|             row_height = int((im_height * (1 - im_height / im_width)) / 3) |  | ||||||
|  |  | ||||||
|         logger.debug(f"row_height: {row_height} | col_width: {col_width}") |  | ||||||
|  |  | ||||||
|         # Calculate spacings for better centering |  | ||||||
|         spacing_top = int((im_width % col_width) / 2) |  | ||||||
|         spacing_left = int((im_height % row_height) / 2) |  | ||||||
|  |  | ||||||
|         # Define sizes for weather icons |  | ||||||
|         icon_small = int(col_width / 3) |  | ||||||
|         icon_medium = icon_small * 2 |  | ||||||
|         icon_large = icon_small * 3 |  | ||||||
|  |  | ||||||
|         # Calculate the x-axis position of each col |  | ||||||
|         col1 = spacing_top |  | ||||||
|         col2 = col1 + col_width |  | ||||||
|         col3 = col2 + col_width |  | ||||||
|         col4 = col3 + col_width |  | ||||||
|         col5 = col4 + col_width |  | ||||||
|         col6 = col5 + col_width |  | ||||||
|         col7 = col6 + col_width |  | ||||||
|  |  | ||||||
|         # Calculate the y-axis position of each row |  | ||||||
|         line_gap = int((im_height - spacing_top - 3 * row_height) // 4) |  | ||||||
|  |  | ||||||
|         row1 = line_gap |  | ||||||
|         row2 = row1 + line_gap + row_height |  | ||||||
|         row3 = row2 + line_gap + row_height |  | ||||||
|  |  | ||||||
|         # Draw lines on each row and border |  | ||||||
|         ############################################################################ |  | ||||||
|         ##    draw = ImageDraw.Draw(im_black) |  | ||||||
|         ##    draw.line((0, 0, im_width, 0), fill='red') |  | ||||||
|         ##    draw.line((0, im_height-1, im_width, im_height-1), fill='red') |  | ||||||
|         ##    draw.line((0, row1, im_width, row1), fill='black') |  | ||||||
|         ##    draw.line((0, row1+row_height, im_width, row1+row_height), fill='black') |  | ||||||
|         ##    draw.line((0, row2, im_width, row2), fill='black') |  | ||||||
|         ##    draw.line((0, row2+row_height, im_width, row2+row_height), fill='black') |  | ||||||
|         ##    draw.line((0, row3, im_width, row3), fill='black') |  | ||||||
|         ##    draw.line((0, row3+row_height, im_width, row3+row_height), fill='black') |  | ||||||
|         ############################################################################ |  | ||||||
|  |  | ||||||
|         # Positions for current weather details |  | ||||||
|         weather_icon_pos = (col1, 0) |  | ||||||
|         temperature_icon_pos = (col2, row1) |  | ||||||
|         temperature_pos = (col2 + icon_small, row1) |  | ||||||
|         humidity_icon_pos = (col2, row2) |  | ||||||
|         humidity_pos = (col2 + icon_small, row2) |  | ||||||
|         windspeed_icon_pos = (col2, row3) |  | ||||||
|         windspeed_pos = (col2 + icon_small, row3) |  | ||||||
|  |  | ||||||
|         # Positions for sunrise, sunset, moonphase |  | ||||||
|         moonphase_pos = (col3, row1) |  | ||||||
|         sunrise_icon_pos = (col3, row2) |  | ||||||
|         sunrise_time_pos = (col3 + icon_small, row2) |  | ||||||
|         sunset_icon_pos = (col3, row3) |  | ||||||
|         sunset_time_pos = (col3 + icon_small, row3) |  | ||||||
|  |  | ||||||
|         # Positions for forecast 1 |  | ||||||
|         stamp_fc1 = (col4, row1) |  | ||||||
|         icon_fc1 = (col4, row1 + row_height) |  | ||||||
|         temp_fc1 = (col4, row3) |  | ||||||
|  |  | ||||||
|         # Positions for forecast 2 |  | ||||||
|         stamp_fc2 = (col5, row1) |  | ||||||
|         icon_fc2 = (col5, row1 + row_height) |  | ||||||
|         temp_fc2 = (col5, row3) |  | ||||||
|  |  | ||||||
|         # Positions for forecast 3 |  | ||||||
|         stamp_fc3 = (col6, row1) |  | ||||||
|         icon_fc3 = (col6, row1 + row_height) |  | ||||||
|         temp_fc3 = (col6, row3) |  | ||||||
|  |  | ||||||
|         # Positions for forecast 4 |  | ||||||
|         stamp_fc4 = (col7, row1) |  | ||||||
|         icon_fc4 = (col7, row1 + row_height) |  | ||||||
|         temp_fc4 = (col7, row3) |  | ||||||
|  |  | ||||||
|         # Create current-weather and weather-forecast objects |  | ||||||
|         if self.location.isdigit(): |  | ||||||
|             logging.debug('looking up location by ID') |  | ||||||
|             weather = self.owm.weather_at_id(int(self.location)).weather |  | ||||||
|             forecast = self.owm.forecast_at_id(int(self.location), '3h') |  | ||||||
|         else: |  | ||||||
|             logging.debug('looking up location by string') |  | ||||||
|             weather = self.owm.weather_at_place(self.location).weather |  | ||||||
|             forecast = self.owm.forecast_at_place(self.location, '3h') |  | ||||||
|  |  | ||||||
|         # Set decimals |  | ||||||
|         dec_temp = None if self.round_temperature == True else 1 |  | ||||||
|         dec_wind = None if self.round_windspeed == True else 1 |  | ||||||
|  |  | ||||||
|         # Set correct temperature units |  | ||||||
|         if self.units == 'metric': |  | ||||||
|             temp_unit = 'celsius' |  | ||||||
|         elif self.units == 'imperial': |  | ||||||
|             temp_unit = 'fahrenheit' |  | ||||||
|  |  | ||||||
|         logging.debug(f'temperature unit: {temp_unit}') |  | ||||||
|         logging.debug(f'decimals temperature: {dec_temp} | decimals wind: {dec_wind}') |  | ||||||
|  |  | ||||||
|         # Get current time |  | ||||||
|         now = arrow.utcnow() |  | ||||||
|  |  | ||||||
|         if self.forecast_interval == 'hourly': |  | ||||||
|  |  | ||||||
|             logger.debug("getting hourly forecasts") |  | ||||||
|  |  | ||||||
|             # Forecasts are provided for every 3rd full hour |  | ||||||
|             # find out how many hours there are until the next 3rd full hour |  | ||||||
|             if (now.hour % 3) != 0: |  | ||||||
|                 hour_gap = 3 - (now.hour % 3) |  | ||||||
|             else: |  | ||||||
|                 hour_gap = 3 |  | ||||||
|  |  | ||||||
|             # Create timings for hourly forcasts |  | ||||||
|             forecast_timings = [now.shift(hours=+ hour_gap + _).floor('hour') |  | ||||||
|                                 for _ in range(0, 12, 3)] |  | ||||||
|  |  | ||||||
|             # Create forecast objects for given timings |  | ||||||
|             forecasts = [forecast.get_weather_at(forecast_time.datetime) for |  | ||||||
|                          forecast_time in forecast_timings] |  | ||||||
|  |  | ||||||
|             # Add forecast-data to fc_data dictionary |  | ||||||
|             fc_data = {} |  | ||||||
|             for forecast in forecasts: |  | ||||||
|                 temp = '{}°'.format(round( |  | ||||||
|                     forecast.temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) |  | ||||||
|  |  | ||||||
|                 icon = forecast.weather_icon_name |  | ||||||
|                 fc_data['fc' + str(forecasts.index(forecast) + 1)] = { |  | ||||||
|                     'temp': temp, |  | ||||||
|                     'icon': icon, |  | ||||||
|                     'stamp': forecast_timings[forecasts.index(forecast)].to( |  | ||||||
|                         get_system_tz()).format('H.00' if self.hour_format == 24 else 'h a') |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|         elif self.forecast_interval == 'daily': |  | ||||||
|  |  | ||||||
|             logger.debug("getting daily forecasts") |  | ||||||
|  |  | ||||||
|             def calculate_forecast(days_from_today): |  | ||||||
|                 """Get temperature range and most frequent icon code for forecast |  | ||||||
|                 days_from_today should be int from 1-4: e.g. 2 -> 2 days from today |  | ||||||
|                 """ |  | ||||||
|  |  | ||||||
|                 # Create a list containing time-objects for every 3rd hour of the day |  | ||||||
|                 time_range = list(arrow.Arrow.range('hour', |  | ||||||
|                                                     now.shift(days=days_from_today).floor('day'), |  | ||||||
|                                                     now.shift(days=days_from_today).ceil('day') |  | ||||||
|                                                     ))[::3] |  | ||||||
|  |  | ||||||
|                 # Get forecasts for each time-object |  | ||||||
|                 forecasts = [forecast.get_weather_at(_.datetime) for _ in time_range] |  | ||||||
|  |  | ||||||
|                 # Get all temperatures for this day |  | ||||||
|                 daily_temp = [round(_.temperature(unit=temp_unit)['temp'], |  | ||||||
|                                     ndigits=dec_temp) for _ in forecasts] |  | ||||||
|                 # Calculate min. and max. temp for this day |  | ||||||
|                 temp_range = f'{max(daily_temp)}°/{min(daily_temp)}°' |  | ||||||
|  |  | ||||||
|                 # Get all weather icon codes for this day |  | ||||||
|                 daily_icons = [_.weather_icon_name for _ in forecasts] |  | ||||||
|                 # Find most common element from all weather icon codes |  | ||||||
|                 status = max(set(daily_icons), key=daily_icons.count) |  | ||||||
|  |  | ||||||
|                 weekday = now.shift(days=days_from_today).format('ddd', locale= |  | ||||||
|                 self.locale) |  | ||||||
|                 return {'temp': temp_range, 'icon': status, 'stamp': weekday} |  | ||||||
|  |  | ||||||
|             forecasts = [calculate_forecast(days) for days in range(1, 5)] |  | ||||||
|  |  | ||||||
|             fc_data = {} |  | ||||||
|             for forecast in forecasts: |  | ||||||
|                 fc_data['fc' + str(forecasts.index(forecast) + 1)] = { |  | ||||||
|                     'temp': forecast['temp'], |  | ||||||
|                     'icon': forecast['icon'], |  | ||||||
|                     'stamp': forecast['stamp'] |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|         for key, val in fc_data.items(): |  | ||||||
|             logger.debug((key, val)) |  | ||||||
|  |  | ||||||
|         # Get some current weather details |  | ||||||
|         temperature = '{}°'.format(round( |  | ||||||
|             weather.temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) |  | ||||||
|  |  | ||||||
|         weather_icon = weather.weather_icon_name |  | ||||||
|         humidity = str(weather.humidity) |  | ||||||
|         sunrise_raw = arrow.get(weather.sunrise_time()).to(self.timezone) |  | ||||||
|         sunset_raw = arrow.get(weather.sunset_time()).to(self.timezone) |  | ||||||
|  |  | ||||||
|         logger.debug(f'weather_icon: {weather_icon}') |  | ||||||
|  |  | ||||||
|         if self.hour_format == 12: |  | ||||||
|             logger.debug('using 12 hour format for sunrise/sunset') |  | ||||||
|             sunrise = sunrise_raw.format('h:mm a') |  | ||||||
|             sunset = sunset_raw.format('h:mm a') |  | ||||||
|  |  | ||||||
|         elif self.hour_format == 24: |  | ||||||
|             logger.debug('using 24 hour format for sunrise/sunset') |  | ||||||
|             sunrise = sunrise_raw.format('H:mm') |  | ||||||
|             sunset = sunset_raw.format('H:mm') |  | ||||||
|  |  | ||||||
|         # Format the windspeed to user preference |  | ||||||
|         if self.use_beaufort: |  | ||||||
|             logger.debug("using beaufort for wind") |  | ||||||
|             wind = str(weather.wind(unit='beaufort')['speed']) |  | ||||||
|  |  | ||||||
|         else: |  | ||||||
|  |  | ||||||
|             if self.units == 'metric': |  | ||||||
|                 logging.debug('getting windspeed in metric unit') |  | ||||||
|                 wind = str(weather.wind(unit='meters_sec')['speed']) + 'm/s' |  | ||||||
|  |  | ||||||
|             elif self.units == 'imperial': |  | ||||||
|                 logging.debug('getting windspeed in imperial unit') |  | ||||||
|                 wind = str(weather.wind(unit='miles_hour')['speed']) + 'miles/h' |  | ||||||
|  |  | ||||||
|         dec = decimal.Decimal |  | ||||||
|         moonphase = get_moon_phase() |  | ||||||
|  |  | ||||||
|         # Fill weather details in col 1 (current weather icon) |  | ||||||
|         draw_icon(im_colour, weather_icon_pos, (col_width, im_height), |  | ||||||
|                   weathericons[weather_icon]) |  | ||||||
|  |  | ||||||
|         # Fill weather details in col 2 (temp, humidity, wind) |  | ||||||
|         draw_icon(im_colour, temperature_icon_pos, (icon_small, row_height), |  | ||||||
|                   '\uf053') |  | ||||||
|  |  | ||||||
|         if is_negative(temperature): |  | ||||||
|             write(im_black, temperature_pos, (col_width - icon_small, row_height), |  | ||||||
|                   temperature, font=self.font) |  | ||||||
|         else: |  | ||||||
|             write(im_black, temperature_pos, (col_width - icon_small, row_height), |  | ||||||
|                   temperature, font=self.font) |  | ||||||
|  |  | ||||||
|         draw_icon(im_colour, humidity_icon_pos, (icon_small, row_height), |  | ||||||
|                   '\uf07a') |  | ||||||
|  |  | ||||||
|         write(im_black, humidity_pos, (col_width - icon_small, row_height), |  | ||||||
|               humidity + '%', font=self.font) |  | ||||||
|  |  | ||||||
|         draw_icon(im_colour, windspeed_icon_pos, (icon_small, icon_small), |  | ||||||
|                   '\uf050') |  | ||||||
|  |  | ||||||
|         write(im_black, windspeed_pos, (col_width - icon_small, row_height), |  | ||||||
|               wind, font=self.font) |  | ||||||
|  |  | ||||||
|         # Fill weather details in col 3 (moonphase, sunrise, sunset) |  | ||||||
|         draw_icon(im_colour, moonphase_pos, (col_width, row_height), moonphase) |  | ||||||
|  |  | ||||||
|         draw_icon(im_colour, sunrise_icon_pos, (icon_small, icon_small), '\uf051') |  | ||||||
|         write(im_black, sunrise_time_pos, (col_width - icon_small, row_height), |  | ||||||
|               sunrise, font=self.font) |  | ||||||
|  |  | ||||||
|         draw_icon(im_colour, sunset_icon_pos, (icon_small, icon_small), '\uf052') |  | ||||||
|         write(im_black, sunset_time_pos, (col_width - icon_small, row_height), sunset, |  | ||||||
|               font=self.font) |  | ||||||
|  |  | ||||||
|         # Add the forecast data to the correct places |  | ||||||
|         for pos in range(1, len(fc_data) + 1): |  | ||||||
|             stamp = fc_data[f'fc{pos}']['stamp'] |  | ||||||
|  |  | ||||||
|             icon = weathericons[fc_data[f'fc{pos}']['icon']] |  | ||||||
|             temp = fc_data[f'fc{pos}']['temp'] |  | ||||||
|  |  | ||||||
|             write(im_black, eval(f'stamp_fc{pos}'), (col_width, row_height), |  | ||||||
|                   stamp, font=self.font) |  | ||||||
|             draw_icon(im_colour, eval(f'icon_fc{pos}'), (col_width, row_height + line_gap * 2), |  | ||||||
|                       icon) |  | ||||||
|             write(im_black, eval(f'temp_fc{pos}'), (col_width, row_height), |  | ||||||
|                   temp, font=self.font) |  | ||||||
|  |  | ||||||
|         border_h = row3 + row_height |  | ||||||
|         border_w = col_width - 3  # leave 3 pixels gap |  | ||||||
|  |  | ||||||
|         # Add borders around each sub-section |  | ||||||
|         draw_border(im_black, (col1, row1), (col_width * 3 - 3, border_h), |  | ||||||
|                     shrinkage=(0, 0)) |  | ||||||
|  |  | ||||||
|         for _ in range(4, 8): |  | ||||||
|             draw_border(im_black, (eval(f'col{_}'), row1), (border_w, border_h), |  | ||||||
|                         shrinkage=(0, 0)) |  | ||||||
|  |  | ||||||
|         # return the images ready for the display |  | ||||||
|         return im_black, im_colour |  | ||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': |  | ||||||
|     print(f'running {__name__} in standalone mode') |  | ||||||
		Reference in New Issue
	
	Block a user