|
| 1 | + |
| 2 | +import time |
| 3 | +from . import lcdconfig |
| 4 | + |
| 5 | +class LCD_0inch96(lcdconfig.RaspberryPi): |
| 6 | + |
| 7 | + width = 160 |
| 8 | + height = 80 |
| 9 | + def command(self, cmd): |
| 10 | + self.digital_write(self.DC_PIN, False) |
| 11 | + self.spi_writebyte([cmd]) |
| 12 | + |
| 13 | + def data(self, val): |
| 14 | + self.digital_write(self.DC_PIN, True) |
| 15 | + self.spi_writebyte([val]) |
| 16 | + |
| 17 | + def reset(self): |
| 18 | + """Reset the display""" |
| 19 | + self.digital_write(self.RST_PIN,True) |
| 20 | + time.sleep(0.01) |
| 21 | + self.digital_write(self.RST_PIN,False) |
| 22 | + time.sleep(0.01) |
| 23 | + self.digital_write(self.RST_PIN,True) |
| 24 | + time.sleep(0.01) |
| 25 | + |
| 26 | + def Init(self): |
| 27 | + """Initialize dispaly""" |
| 28 | + self.module_init() |
| 29 | + self.reset() |
| 30 | + |
| 31 | + self.command(0x11) |
| 32 | + time.sleep(0.1) |
| 33 | + self.command(0x21) |
| 34 | + self.command(0x21) |
| 35 | + |
| 36 | + self.command(0xB1) |
| 37 | + self.data(0x05) |
| 38 | + self.data(0x3A) |
| 39 | + self.data(0x3A) |
| 40 | + |
| 41 | + self.command(0xB2) |
| 42 | + self.data(0x05) |
| 43 | + self.data(0x3A) |
| 44 | + self.data(0x3A) |
| 45 | + |
| 46 | + self.command(0xB3) |
| 47 | + self.data(0x05) |
| 48 | + self.data(0x3A) |
| 49 | + self.data(0x3A) |
| 50 | + self.data(0x05) |
| 51 | + self.data(0x3A) |
| 52 | + self.data(0x3A) |
| 53 | + |
| 54 | + self.command(0xB4) |
| 55 | + self.data(0x03) |
| 56 | + |
| 57 | + self.command(0xC0) |
| 58 | + self.data(0x62) |
| 59 | + self.data(0x02) |
| 60 | + self.data(0x04) |
| 61 | + |
| 62 | + self.command(0xC1) |
| 63 | + self.data(0xC0) |
| 64 | + |
| 65 | + self.command(0xC2) |
| 66 | + self.data(0x0D) |
| 67 | + self.data(0x00) |
| 68 | + |
| 69 | + self.command(0xC3) |
| 70 | + self.data(0x8D) |
| 71 | + self.data(0x6A) |
| 72 | + |
| 73 | + self.command(0xC4) |
| 74 | + self.data(0x8D) |
| 75 | + self.data(0xEE) |
| 76 | + |
| 77 | + self.command(0xC5) |
| 78 | + self.data(0x0E) |
| 79 | + |
| 80 | + self.command(0xE0) |
| 81 | + self.data(0x10) |
| 82 | + self.data(0x0E) |
| 83 | + self.data(0x02) |
| 84 | + self.data(0x03) |
| 85 | + self.data(0x0E) |
| 86 | + self.data(0x07) |
| 87 | + self.data(0x02) |
| 88 | + self.data(0x07) |
| 89 | + self.data(0x0A) |
| 90 | + self.data(0x12) |
| 91 | + self.data(0x27) |
| 92 | + self.data(0x37) |
| 93 | + self.data(0x00) |
| 94 | + self.data(0x0D) |
| 95 | + self.data(0x0E) |
| 96 | + self.data(0x10) |
| 97 | + |
| 98 | + self.command(0xE1) |
| 99 | + self.data(0x10) |
| 100 | + self.data(0x0E) |
| 101 | + self.data(0x03) |
| 102 | + self.data(0x03) |
| 103 | + self.data(0x0F) |
| 104 | + self.data(0x06) |
| 105 | + self.data(0x02) |
| 106 | + self.data(0x08) |
| 107 | + self.data(0x0A) |
| 108 | + self.data(0x13) |
| 109 | + self.data(0x26) |
| 110 | + self.data(0x36) |
| 111 | + self.data(0x00) |
| 112 | + self.data(0x0D) |
| 113 | + self.data(0x0E) |
| 114 | + self.data(0x10) |
| 115 | + |
| 116 | + self.command(0x3A) |
| 117 | + self.data(0x05) |
| 118 | + |
| 119 | + self.command(0x36) |
| 120 | + self.data(0xA8) |
| 121 | + |
| 122 | + self.command(0x29) |
| 123 | + |
| 124 | + def SetWindows(self, Xstart, Ystart, Xend, Yend): |
| 125 | + #set the X coordinates |
| 126 | + Xstart=Xstart+1 |
| 127 | + Xend=Xend+1 |
| 128 | + Ystart=Ystart+26 |
| 129 | + Yend=Yend+26 |
| 130 | + self.command(0x2A) |
| 131 | + self.data(0x00) #Set the horizontal starting point to the high octet |
| 132 | + self.data(Xstart & 0xff) #Set the horizontal starting point to the low octet |
| 133 | + self.data(0x00) #Set the horizontal end to the high octet |
| 134 | + self.data((Xend - 1) & 0xff) #Set the horizontal end to the low octet |
| 135 | + |
| 136 | + #set the Y coordinates |
| 137 | + self.command(0x2B) |
| 138 | + self.data(0x00) |
| 139 | + self.data((Ystart & 0xff)) |
| 140 | + self.data(0x00) |
| 141 | + self.data((Yend - 1) & 0xff ) |
| 142 | + |
| 143 | + self.command(0x2C) |
| 144 | + |
| 145 | + def ShowImage(self,Image): |
| 146 | + """Set buffer to value of Python Imaging Library image.""" |
| 147 | + """Write display buffer to physical display""" |
| 148 | + imwidth, imheight = Image.size |
| 149 | + if imwidth != self.width or imheight != self.height: |
| 150 | + if imwidth != self.height or imheight != self.width: |
| 151 | + raise ValueError('Image must be same dimensions as display \ |
| 152 | + ({0}x{1}).' .format(self.height,self.width)) |
| 153 | + else: |
| 154 | + img = self.np.asarray(Image) |
| 155 | + pix = self.np.zeros((self.width,self.hight,2), dtype = self.np.uint8) |
| 156 | + pix[...,[0]] = self.np.add(self.np.bitwise_and(img[...,[0]],0xF8), self.np.right_shift(img[...,[1]],5)) |
| 157 | + pix[...,[1]] = self.np.add(self.np.bitwise_and(self.np.left_shift(img[...,[1]],3),0xE0), self.np.right_shift(img[...,[2]],3)) |
| 158 | + else: |
| 159 | + img = self.np.asarray(Image) |
| 160 | + pix = self.np.zeros((self.height,self.width,2), dtype = self.np.uint8) |
| 161 | + pix[...,[0]] = self.np.add(self.np.bitwise_and(img[...,[0]],0xF8), self.np.right_shift(img[...,[1]],5)) |
| 162 | + pix[...,[1]] = self.np.add(self.np.bitwise_and(self.np.left_shift(img[...,[1]],3),0xE0), self.np.right_shift(img[...,[2]],3)) |
| 163 | + |
| 164 | + pix = pix.flatten().tolist() |
| 165 | + self.SetWindows ( 0, 0, self.width, self.height) |
| 166 | + self.digital_write(self.DC_PIN,True) |
| 167 | + |
| 168 | + for i in range(0,len(pix),4096):#The length is 160*160*2 Actually 160*80*2 So take 2 |
| 169 | + self.spi_writebyte(pix[i:i+4096]) |
| 170 | + |
| 171 | + |
| 172 | + def clear(self): |
| 173 | + """Clear contents of image buffer""" |
| 174 | + _buffer = [0xff]*(self.width * self.height ) |
| 175 | + self.SetWindows ( 0, 0, self.width, self.height) |
| 176 | + self.digital_write(self.DC_PIN,True) |
| 177 | + for i in range(0,len(_buffer),4096): |
| 178 | + self.spi_writebyte(_buffer[i:i+4096]) |
0 commit comments