LCDdemo

Salut,

I have written a short LCD demo using Sparki 1.0.5.3 - unfortunately, due to the restrictions introduced in 1.0.5.4, this will not work anymore as I do not only draw in white. In this version you have to introduce a #define WHITE 1 in sparki.h (which is applied in 1.5.0.4)

Enjoy, Mathias

[code]/*******************************************
Basic Sparki Code

This is the most basic code you can use to
run Sparki.
********************************************/
#include <Sparki.h> // include the sparki library

//#define WHITE 1
// needs to be inserted into sparki.h
// sparki uses a 128x8 buffer to hold the pixel information, giving one bit per pixel
// WHITE is by default 0, which means BLACK here. The define above fixes so we are no longer painting black on black

const byte maxx = 128;
const byte maxy = 64;

void setup() // code inside these brackets runs first, and only once
{

sparki.clearLCD();
sparki.drawRect( 64, 32, 40, 30);

sparki.updateLCD();
}

void loop() // code inside these brackets runs over and over forever
{
char str[40];
int sonar;
int sinc;
int angle;
int rx, ry, rx2, ry2, rl, rm;

sparki.clearLCD();
sparki.println();
sparki.println();
sparki.println(“LCD test skecth”);
sparki.println("(2014) Dr. M. Wilhelm");
sparki.updateLCD();
delay(2000);

sparki.clearLCD();
sparki.println(“Draw 1000 pixels”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
// draw random points
for (int i = 0; i < 240; i++) {
sparki.drawPixel(random(maxx), random(maxy), WHITE);
}
sparki.updateLCD();
delay(2000);

sparki.clearLCD();
sparki.println(“Draw rectangles”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawRect( 1, 1, 10, 10);
sparki.drawRect( 11, 11, 20, 20);
sparki.drawRect( 31, 31, 30, 30);
sparki.drawRect( 61, 61, 10, 10); // this one exceeds the y scale and should be cropped
sparki.updateLCD();
delay(2000);

sparki.clearLCD();
sparki.println(“Draw filled rectangles”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawRectFilled( 1, 1, 10, 10);
sparki.drawRectFilled( 11, 11, 20, 20);
sparki.drawRectFilled( 31, 31, 30, 30);
sparki.drawRectFilled( 61, 61, 10, 10); // this one exceeds the y scale and should be cropped
sparki.updateLCD();
delay(2000);

sparki.clearLCD();
sparki.println(“Draw circles”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawCircle( 1, 1, 10);
sparki.drawCircle( 11, 11, 20);
sparki.drawCircle( 31, 31, 30);
sparki.drawCircle( 61, 61, 10); // this one exceeds the y scale and should be cropped
sparki.updateLCD();
delay(2000);

sparki.clearLCD();
sparki.println(“Draw filled circles”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawCircleFilled( 1, 1, 10);
sparki.drawCircleFilled( 11, 11, 20);
sparki.drawCircleFilled( 31, 31, 30);
sparki.drawCircleFilled( 61, 61, 10); // this one exceeds the y scale and should be cropped
sparki.updateLCD();
delay(2000);

sparki.clearLCD();
sparki.println(“Draw radar”);
sparki.updateLCD();
delay(1000);
angle=0;
sinc=1;
sparki.clearLCD();
sparki.println("* * * Sonar * * ");
for (int i = 0; i < 1000; i++) {
sparki.servo(angle);
sonar = sparki.ping();
angle += sinc;
if (angle>60) sinc = -1;
if (angle<-60) sinc = 1;
rx = 64;
ry = 63;
rm = 40; // max length of radar line
rl = map(sonar,0,30,0,rm); // map 30 cm to rm pixels
if (rl>rm) rl=rm;
rx2 = rx + rl
sin(angle2PI/360);
ry2 = ry - rlcos(angle2PI/360);
M_drawLine(rx,ry,rx2,ry2,WHITE);
rx2 = rx + rm
sin(angle2PI/360);
ry2 = ry - rmcos(angle2PI/360);
rx = rx + rl
sin(angle2PI/360);
ry = ry - rlcos(angle2*PI/360);
M_drawLine(rx,ry,rx2,ry2,0);

sparki.updateLCD();  

}
delay(2000);
}

// The original drawLine code has WHITE as color hardcoded, so I cannot “erase” by painting black
// this code is the original code extendsd by a color variable
void M_drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t color ) {
uint8_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}

if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}

// much faster to put the test here, since we’ve already sorted the points
//updateBoundingBox(x0, y0, x1, y1);

uint8_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);

int8_t err = dx / 2;
int8_t ystep;

if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;}

for (; x0<=x1; x0++) {
if (steep) {
sparki.drawPixel(y0, x0, color);
} else {
sparki.drawPixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
[/code]