• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

SSD1306 hline issue – graphics drawing

Q&A Forum › Category: ESP32 › SSD1306 hline issue – graphics drawing
0 Vote Up Vote Down
Damian Hartley asked 6 years ago

Hi Sara

another topic

if i run this code

display.pixel(3, 4, 1)  # 3rd param is the colour
display.show()

i get a result on the display as per an example on following link

twobitarcade.net/article/oled-displays-i2c-micropython/#setting%20up

further down on the same link i try

display.hline(2, 3, 25, 1)  
display.show()

i get

AttributeError: ‘SSD1306_I2C’ object has no attribute ‘hline’

others seem to have a similar problem and im googled out looking for a solution so thought I would ask. the framebuf reference on Damien P georges site did not help much

I am basically trying to draw a battery gauge on the SSD so thought I would first start constructing box bounds and thats when I get the Hline error

is it something to do with the SSD1306.py code and circuitpython version of it??

Any help is appreciated. Another long evening after work 🙂

Thanks

Regards

Damian

PS I am using the DOIT board

Question Tags: error when trying to use .hline & .vline
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Damian.
What is the source code of the library you’re using?
That error basically means that there isn’t any method with that name “hline” in the library you’re using.
In the SSD1306 library that we use there isn’t any method with that name either. So, it must be another library.
There is this library that supports those methods: https://github.com/adafruit/micropython-adafruit-gfx/blob/master/gfx.py. 
You probably need to install both libraries GFX and SSD1306, but I haven’t tried this.
Let me know if you were able to solve your problem.
Regards,
Sara
 
 

0 Vote Up Vote Down
Damian Hartley answered 6 years ago

Hi Sara

sorry tp pester but you can see Im keen to get my head around this simple task of drawing a line between two points.
I followed your link ( i have seen before but was not sure what to do)
I created a file called gfx.py containing the listed text in the link
I loaded it to the board
I then made a main.py containing the following
import machine
from machine import Pin, I2C
import gfx # I created gfx.py from the github site as advised to look there
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
#instantiated an object hopefully from the class GFX ( GFX class is inside the GFX import
graphics = gfx.GFX(128, 64, hline)
graphics.hline(2, 3, 25, 1) #hline is inside the gfx library
graphics.show()
I got the following response
>>>
Ready to download this file,please wait!
…
download ok
exec(open(‘Main.py’).read(),globals())
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “<string>”, line 6, in <module>
NameError: name ‘hline’ isn’t defined
>>>

Im not trying to turn this into personal tuition it appears to me that there is some missing info or conflicts between drivers micropython ssd1306 and or modified imports of circuit python

put simply how would you draw a line on a 0.96″ display 128 x 64 pixels that may be a simpler solution than me exploring the .hline route ( I have seen something that involves bytearray and [ICON] matrix of bytes for writting an array but cant help feeling there is something easier

Once again sorry for taking up your time.

Regards

Damian

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Damian.

I got it working.

After installing the ssd1306.py and gfx.py libraries, here’s a simple code that draws lines:

from machine import Pin, I2C
import ssd1306
import gfx
# ESP32 Pin assignment
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
graphics = gfx.GFX(oled_width, oled_height, oled.pixel)
graphics.line(0, 0, oled_width, oled_height, 1)
graphics.hline(10, 5, 20, 1)
oled.show()

Basically, you need to include the ssd1306 and gfx libraries.
Then, define the pins for the ESP32.
Create an oled object.

oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

Then, you need to create a graphics object to draw shapes. It takes as arguments, the width and height of the drawing area. In this case, we want to draw in the entire oled, so we pass the oled width and height. We should also pass as argument one function of our display that draws pixels, in our case is oled.pixel.

graphics = gfx.GFX(oled_width, oled_height, oled.pixel)

Then, you can use the drawing functions.
If you take a look at the GFX library you can see how to use those functions.
In case of hline, it takes as arguments the starting point (x,y), the length of the line and the last argument is 1 that refers to the color of the pixels.

graphics.hline(10, 5, 20, 1)

You always need to call oled.show() so that it actually displays on the OLED.

I hope this works for you.

Regards,
Sara

0 Vote Up Vote Down
Damian Hartley answered 6 years ago

Hi Sara

You are certainly putting in the hours to help me thanks. I have got something working with the ssd1306 and hline tried several times but tonight hline worked. I will go through your splendid assistance in solving over the weekend to get my head fully around it .

I have noticed that using upycraft quite regularly refuses to com port connect and I need to reflash the bin file to re establish connection next time I boot pc

Anyway once again many thanks to you for your tenacity and patience in assisting me

Happy Christmas to you and Rui

Best wishes

Damian

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Damian.
If you’re having that trouble with upycraft IDE, I recommend using Thonny IDE. That issue usually doesn’t happen.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.