#include #include "graphics_line.h" #include "assert.h" #include #include #include /* now we have an array of graphics_line */ /* remember graphics_line lines[] is the same as graphics_line *line, but the [] reminds us that we are passing an array, not a single element. */ #define CS 6 #define DC 7 #define RST 8 // Colour definition #define BLACK 0x0000 Adafruit_ST7735 tft = Adafruit_ST7735(CS, DC, RST); void draw_lines(int num_lines, graphics_line lines[]){ for (int i=0; i < num_lines; i++) { // note how the . "dot" notation gets the field of the struct tft.drawLine(lines[i].xs, lines[i].ys, lines[i].xe, lines[i].ye, lines[i].color); } } // creates num_lines random lines with random colors and returns pointer // to the list containing them graphics_line * make_random_lines(int num_lines) { graphics_line * list; // a temporary line graphics_line tmp; list = (typeof(list)) malloc(num_lines * sizeof(*list)); assert(list != 0, 2); for (int i=0; i < num_lines; i++) { tmp.xs = random(0, tft.width()); tmp.ys = random(0, tft.height()); tmp.xe = random(0, tft.width()); tmp.ye = random(0, tft.height()); tmp.color = tft.Color565(random(0, 255), random(0, 255), random(0, 255)); list[i] = tmp; } return list; } void setup() { int num_lines = 128; graphics_line *random_lines; Serial.begin( 9600 ); // wait for button press to start button_pause("Press button to start"); tft.initR(INITR_REDTAB); // initialize a ST7735R chip, red tab Serial.println("Generating lines"); random_lines = make_random_lines(num_lines); Serial.println("Drawing lines"); tft.fillScreen(BLACK); draw_lines(num_lines, random_lines); Serial.println("Freeing lines"); free(random_lines); } void loop() { }