Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8026

MicroPython • Re: Help with a Waveshare 1.8inch LCD Display Module

$
0
0
I tried to run the glyph coding, but it doesn't run: NameError: name 'display' isn't defined

Here is the code. Please assit in making this work; this is good solution to the problem Thanks

Code:

# Rui Santos & Sara Santos - Random Nerd Tutorials# Complete project details: https://RandomNerdTutorials.com/raspberry-pi-pico-bme680-micropython/# Adding waveShare 1.8" LCD# Modified S. Stein 9/7/25 R4       from machine import Pin, I2C, SPI,PWMfrom time import sleepimport timeimport utime as timefrom bme680R1 import *import os# Test 10 July 2021 Mod 8/27/25import framebufimport utime, time# Source: Electrocredible.com, Language: MicroPythonfrom machine import ADCadc = machine.ADC(4)BL = 13DC = 8RST = 12MOSI = 11SCK = 10CS = 91 # New driver received from WaveShare on 10th July 2021 for 1.8" LCDclass LCD_1inch8(framebuf.FrameBuffer):    def __init__(self):        self.width = 160        self.height = 128                self.cs = Pin(CS,Pin.OUT)        self.rst = Pin(RST,Pin.OUT)                self.cs(1)        self.spi = SPI(1)        self.spi = SPI(1,1000_000)        self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)        self.dc = Pin(DC,Pin.OUT)        self.dc(1)        self.buffer = bytearray(self.height * self.width * 2)        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)        self.init_display()                self.WHITE =   0xFFFF        self.BLACK  =  0x0000        self.GREEN   =  0x001F        self.BLUE    =  0xF800        self.RED   = 0x07E0                def write_cmd(self, cmd):        self.cs(1)        self.dc(0)        self.cs(0)        self.spi.write(bytearray([cmd]))        self.cs(1)    def write_data(self, buf):        self.cs(1)        self.dc(1)        self.cs(0)        self.spi.write(bytearray([buf]))        self.cs(1)    def init_display(self):        """Initialize dispaly"""          self.rst(1)        self.rst(0)        self.rst(1)                self.write_cmd(0x36);        self.write_data(0x70);                self.write_cmd(0x3A);        self.write_data(0x05);         #ST7735R Frame Rate        self.write_cmd(0xB1);        self.write_data(0x01);        self.write_data(0x2C);        self.write_data(0x2D);        self.write_cmd(0xB2);        self.write_data(0x01);        self.write_data(0x2C);        self.write_data(0x2D);        self.write_cmd(0xB3);        self.write_data(0x01);        self.write_data(0x2C);        self.write_data(0x2D);        self.write_data(0x01);        self.write_data(0x2C);        self.write_data(0x2D);        self.write_cmd(0xB4); #Column inversion        self.write_data(0x07);        #ST7735R Power Sequence        self.write_cmd(0xC0);        self.write_data(0xA2);        self.write_data(0x02);        self.write_data(0x84);        self.write_cmd(0xC1);        self.write_data(0xC5);        self.write_cmd(0xC2);        self.write_data(0x0A);        self.write_data(0x00);        self.write_cmd(0xC3);        self.write_data(0x8A);        self.write_data(0x2A);        self.write_cmd(0xC4);        self.write_data(0x8A);        self.write_data(0xEE);        self.write_cmd(0xC5); #VCOM        self.write_data(0x0E);        #ST7735R Gamma Sequence        self.write_cmd(0xe0);        self.write_data(0x0f);        self.write_data(0x1a);        self.write_data(0x0f);        self.write_data(0x18);        self.write_data(0x2f);        self.write_data(0x28);        self.write_data(0x20);        self.write_data(0x22);        self.write_data(0x1f);        self.write_data(0x1b);        self.write_data(0x23);        self.write_data(0x37);        self.write_data(0x00);        self.write_data(0x07);        self.write_data(0x02);        self.write_data(0x10);        self.write_cmd(0xe1);        self.write_data(0x0f);        self.write_data(0x1b);        self.write_data(0x0f);        self.write_data(0x17);        self.write_data(0x33);        self.write_data(0x2c);        self.write_data(0x29);        self.write_data(0x2e);        self.write_data(0x30);        self.write_data(0x30);        self.write_data(0x39);        self.write_data(0x3f);        self.write_data(0x00);        self.write_data(0x07);        self.write_data(0x03);        self.write_data(0x10);        self.write_cmd(0xF0); #Enable test command        self.write_data(0x01);        self.write_cmd(0xF6); #Disable ram power save mode        self.write_data(0x00);            #sleep out        self.write_cmd(0x11);        #DEV_Delay_ms(120);        #Turn on the LCD display        self.write_cmd(0x29);    def show(self):        self.write_cmd(0x2A)        self.write_data(0x00)        self.write_data(0x01)        self.write_data(0x00)        self.write_data(0xA0)                                self.write_cmd(0x2B)        self.write_data(0x00)        self.write_data(0x02)        self.write_data(0x00)        self.write_data(0x82)                self.write_cmd(0x2C)                self.cs(1)        self.dc(1)        self.cs(0)        self.spi.write(self.buffer)        self.cs(1)# ==============End of driver setup==============================def colour(R,G,B):# Get RED value    rp = int(R*31/255) # range 0 to 31    if rp < 0: rp = 0    r = rp *8# Get Green value - more complicated!    gp = int(G*63/255) # range 0 - 63    if gp < 0: gp = 0    g = 0    if gp & 1:  g = g + 8192    if gp & 2:  g = g + 16384    if gp & 4:  g = g + 32768    if gp & 8:  g = g + 1    if gp & 16: g = g + 2    if gp & 32: g = g + 4# Get BLUE value           bp =int(B*31/255) # range 0 - 31    if bp < 0: bp = 0    b = bp *256    colour = r+g+b    return colourif __name__=='__main__':    pwm = PWM(Pin(BL))    pwm.freq(1000)    pwm.duty_u16(32768)#max 65535    LCD = LCD_1inch8()    #color BRG    LCD.fill(0x4)    LCD.show() # All pixels lit dark green    LCD.pixel(0,0,0xFFFF)     # Left Top - OK    LCD.pixel(0,152,0xFFFF)   # Left Bottom - OK    LCD.pixel(190,0,0xFFFF)   # Right Top - OK    LCD.pixel(190,152,0xFFFF) # Right Bottom - OK# RPi Pico - Pin assignmenti2c = I2C(id=0, scl=Pin(21), sda=Pin(20))bme = BME680_I2C(i2c=i2c)  if 'BME680 log.txt' in os.listdir():     print('Header Row exists; pass on to next step!')    else:    file = open ("BME680 log.txt", "a")    print(type(file))    results1 = "Date" +'\t' +'\t' + "Time" + '\t' + "Temp." +  chr(176) +'F' + '\t' + '\t' + "Hum. %" +  '\t' + "Press. in Hg" + '\t' + "Gas KOhms" + "\n"    file.write(results1)    file.close()    pass         def glyph(self, c, x, y, color=1):        """display a glyph for a char "c" at given pixel           position x, y. X, y are topleft corner of glyph drawn.           For simplicity, the glyph are defined in 'ascii art'           and evaluated at runtime.           More efficient to generate byte data offline from           ascii definitions.        """        glyphs = {            '°': [ "   **   ",                   "  *  *  ",                   "   **   ",                   "        ",                   "        ",                   "        ",                   "        ",                   "        ", ],            '↑': [ "    *   ", # just for fun                   "   * *  ",                   "  * * * ",                   "    *   ",                   "    *   ",                   "    *   ",                   "    *   ",                   "    *   ", ],            }        if not c in glyphs:            print( f"glyphs does not contain '{c}'")            return        glyph = glyphs[c]        for row in range(0,8):            for col in range(0,8):                if glyph[row][col] == '*':                    self.pixel(x+col, y+row, color)temp = 23.456text = f"temp={temp:5.2f}"cursor_pixel = 0display.text(text, cursor_pixel, 0, 1)cursor_pixel += len(text)*8display.glyph('°', cursor_pixel, 0, 1)cursor_pixel += 8display.text("C", cursor_pixel, 0, 1)cursor_pixel += 8display.show()LCD.show()   

Statistics: Posted by ajax12 — Sun Sep 14, 2025 11:21 pm



Viewing all articles
Browse latest Browse all 8026

Trending Articles