I created a python program to display a wave on the display pack. This works fine but is too slow so I created the same program in C. This is faster, but still too slow, and I am surprised that there is little difference in speed between python and C.
Roughly: python takes 2.3 seconds and C takes 1.3 seconds.
I expect I’m doing something daft, but have no idea what ??
Python code:
import picodisplay as display
import math
width = display.get_width()
height = display.get_height()
display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565)
display.init(display_buffer)
display.set_led(0,0,0) # Turn the LED off
display.set_backlight(1.0)
display.set_pen(51, 153, 255)
display.clear()
display.set_pen(0, 51, 204)
display.rectangle(0,0,240,20) # dark blue sea base
display.update()
for x in range(240):
angle = x*1.5
y = int(math.sin(math.radians(angle)) * 15) + 14
display.set_pen(0, 51, 204) # dark blue
display.rectangle(x,20,2,y)
display.update()
C code:
#include “pico_display.hpp”
#include <math.h>
using namespace pimoroni;
uint16_t buffer[PicoDisplay::WIDTH * PicoDisplay::HEIGHT];
PicoDisplay pico_display(buffer);
int angle;
int x;
int y;
double degtorad(double degree) { // degrees > radians
double pi = 3.14159265359;
return (degree * (pi / 180));
}
int main() {
pico_display.init();
pico_display.set_led(0, 0, 0); // led off
pico_display.set_backlight(150); //0 - 255
pico_display.set_pen(51, 153, 255); // blue sky
pico_display.clear();
pico_display.set_pen(0, 51, 204); //dark blue sea base
Rect sea_rect(0,0,240,20);
pico_display.rectangle(sea_rect);
pico_display.update();
for(x=0; x<240; x=x+2) {
angle = x*1.5;
y = int(sin(degtorad(angle)) * 15) + 14;
Rect db_rect(x,20,2,y);
pico_display.set_pen(0, 51, 204); //dark blue wave
pico_display.rectangle(db_rect);
pico_display.update();
}
}