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

Refresh a display

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.

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 boardimport displayioimport terminalioimport mathimport timefrom adafruit_display_shapes.circle import Circlefrom adafruit_display_shapes.line import Linefrom adafruit_display_text import labelfrom adafruit_bitmap_font import bitmap_fontfrom adafruit_clue import clueRED = 0x880000GREEN = 0x008800BLUE = 0x000088YELLOW = 0x884400CYAN = 0x0088BBMAGENTA = 0x9900BBWHITE = 0x888888display = board.DISPLAYcanvas = displayio.Group()display.show(canvas)palette = displayio.Palette(3)palette[0] = 0x125690palette[2] = 0xFF00FFcx = 110cy = 110edge = 10R = 100circle = Circle(cx, cy, R, fill=0, outline=GREEN, stroke=3)canvas.append(circle)# x and y start of the linestartX = cxstartY = cy# here is an angle just to get the line startedangle = 45# x and y end of the lineendX = 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

Something like this?

Code: Select all | TOGGLE FULL SIZE
`import boardimport displayioimport terminalioimport mathimport timefrom adafruit_display_shapes.circle import Circlefrom adafruit_display_shapes.line import Linefrom adafruit_display_text import label#from adafruit_bitmap_font import bitmap_font#from adafruit_clue import clueRED = 0x880000GREEN = 0x008800BLUE = 0x000088YELLOW = 0x884400CYAN = 0x0088BBMAGENTA = 0x9900BBWHITE = 0x888888display = board.DISPLAYdisplay.auto_refresh = Falsecanvas = displayio.Group()display.show(canvas)palette = displayio.Palette(3)palette[0] = 0x125690palette[2] = 0xFF00FFedge = 10cx = display.width // 2cy = display.height // 2sec_radius = min(display.width, display.height) // 2 - edgemin_radius = int(sec_radius * 0.9)hour_radius = int(sec_radius * 0.8)sec_colour = WHITEmin_colour = YELLOWhour_colour = MAGENTAdef 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