It's Holiday Sale Weekend at Adafruit - use the code ADATY to get 15% off items storewide! Some restrictions apply.
0

Refresh a display
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Refresh a display

by markh9 on Sun Oct 17, 2021 6:05 pm

Hello,

I am using a Clue board with the latest circuit python and libraries (as of last week).

I am trying to display an analog clock face. However, I can not get the hands to move. I have tried auto refresh false with a manual refresh and I have tried with the refresh at default. I can move the circle by changing x and y. I can change text, but so far I can not figure out how to refresh a line.

Please, what am I missing?

Below is a simplified copy of my code. The line simply does not move.

Yes, I know everyone else uses group or splash where I used canvas. Canvas is just easier for me. Also, I can guess where the group usage came from, but why use splash? What is splash?

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
import math
import time

from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.line import Line
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
from adafruit_clue import clue


RED = 0x880000
GREEN = 0x008800
BLUE = 0x000088
YELLOW = 0x884400
CYAN = 0x0088BB
MAGENTA = 0x9900BB
WHITE = 0x888888
display = board.DISPLAY
canvas = displayio.Group()
display.show(canvas)

palette = displayio.Palette(3)
palette[0] = 0x125690
palette[2] = 0xFF00FF

cx = 110
cy = 110
edge = 10
R = 100

circle = Circle(cx, cy, R, fill=0, outline=GREEN, stroke=3)
canvas.append(circle)

# x and y start of the line
startX = cx
startY = cy

# here is an angle just to get the line started
angle = 45

# x and y end of the line
endX = startX + round(math.cos(angle) * R)
endY = startY - round(math.sin(angle) * R)
line = Line(startX, startY, round(endX), round(endY), color=0x00FF00)
canvas.append(line)

time.sleep(1)

while True:
    # new angle just to see the refresh work
    newAngle = 185
    endX = startX + round(math.cos(newAngle) * R)
    endY = startY - round(math.sin(newAngle) * R)
    line.x0 = 110
    line.y0 = 110
    line.x1 = round(endX)
    line.y1 = round(endY)
    # print statements to show that the line start and end points have changed.
    print("line x and y", line.x, line.y)
    print("line x0 and y0: ", line.x0, line.y0)
    print("line x1 and y1: ", line.x1, line.y1)

    display.refresh()
    time.sleep(1)
markh9
 
Posts: 6
Joined: Fri Feb 28, 2014 2:10 pm

Re: Refresh a display

by kevinjwalters on Wed Oct 20, 2021 7:12 pm

Something like this?

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
import math
import time

from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.line import Line
from adafruit_display_text import label
#from adafruit_bitmap_font import bitmap_font
#from adafruit_clue import clue


RED = 0x880000
GREEN = 0x008800
BLUE = 0x000088
YELLOW = 0x884400
CYAN = 0x0088BB
MAGENTA = 0x9900BB
WHITE = 0x888888
display = board.DISPLAY
display.auto_refresh = False
canvas = displayio.Group()
display.show(canvas)


palette = displayio.Palette(3)
palette[0] = 0x125690
palette[2] = 0xFF00FF

edge = 10
cx = display.width // 2
cy = display.height // 2
sec_radius = min(display.width, display.height) // 2 - edge
min_radius = int(sec_radius * 0.9)
hour_radius = int(sec_radius * 0.8)
sec_colour = WHITE
min_colour = YELLOW
hour_colour = MAGENTA

def make_hand(angle, radius, color):
    endX = cx + round(math.sin(angle) * radius)
    endY = cy - round(math.cos(angle) * radius)
    return Line(cx, cy, endX, endY, color=color)

circle = Circle(cx, cy, sec_radius, fill=0, outline=GREEN, stroke=3)
canvas.append(circle)
sec_idx = len(canvas)
canvas.append(make_hand(0, sec_radius, sec_colour))
min_idx = len(canvas)
canvas.append(make_hand(0, min_radius, min_colour))
hour_idx = len(canvas)
canvas.append(make_hand(0, hour_radius, hour_colour))

start_ns = time.monotonic_ns()
while True:
    seconds = (time.monotonic_ns() - start_ns) / 1e9
    sec_hand_angle = math.radians(math.fmod(seconds, 60.0) * (360 / 60))
    min_hand_angle = math.radians(math.fmod(seconds, 3600.0) * (360 / 3600))
    hour_hand_angle = math.radians(math.fmod(seconds, 43200.0) * ( 360 / 43200))
    canvas[sec_idx] = make_hand(sec_hand_angle, sec_radius, sec_colour)
    canvas[min_idx] = make_hand(min_hand_angle, min_radius, min_colour)
    canvas[hour_idx] = make_hand(hour_hand_angle, hour_radius, hour_colour)
    display.refresh()
    time.sleep(0.1)


I think the problem here is most of these graphical objects are created when you make the object and can't be changed afterwards. Some do have attributes which can be changed like x0 and y0 on Circle. Python will just let you add attributes to any object and read them back even though they are doing nothing which confuses the situation.

kevinjwalters
 
Posts: 982
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.