Skip to content

Commit

Permalink
-Moved parameters from font to text
Browse files Browse the repository at this point in the history
-Added comments to examples
  • Loading branch information
vesteraas committed Dec 26, 2014
1 parent 702cad9 commit c4bdbe2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 56 deletions.
6 changes: 4 additions & 2 deletions examples/berries.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var pitft = require("../pitft");

var fb = pitft("/dev/fb1");
var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode

// Clear the screen buffer
fb.clear();

var xMax = fb.size().width;
var yMax = fb.size().height;

fb.image(x, y, "raspberry-pi.png");
fb.image(x, y, "raspberry-pi.png"); // Draw the image from the file "raspberry-pi.png" at position x, y

for (var n=0; n<1000; n++) {
var x = Math.random() * (xMax + 32) - 16;
Expand Down
9 changes: 6 additions & 3 deletions examples/circles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var pitft = require("../pitft");

var fb = pitft("/dev/fb1");
var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode

// Clear the screen buffer
fb.clear();

var xMax = fb.size().width;
Expand All @@ -13,12 +15,13 @@ for (var n=0; n<250; n++) {
y = parseInt(Math.random() * yMax, 10);
radius = parseInt(Math.random() * yMax / 2, 10);

// Create a random color
r = Math.random();
g = Math.random();
b = Math.random();

fb.color(r, g, b);
fb.circle(x, y, radius, false, 1);
fb.circle(x, y, radius, false, 1); // Draw an outlined circle with a 1 pixel wide border
}

fb.clear();
Expand All @@ -35,5 +38,5 @@ for (var n=0; n<250; n++) {
b = Math.random();

fb.color(r, g, b);
fb.circle(x, y, radius, true);
fb.circle(x, y, radius, true); // Draw a filled circle
}
57 changes: 31 additions & 26 deletions examples/clock.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
var pitft = require("../pitft");

var fb = pitft("/dev/fb1", true);
var fb = pitft("/dev/fb1", true); // Returns a framebuffer in double buffering mode

// Clear the back buffer
fb.clear();

var xMax = fb.size().width;
var yMax = fb.size().height;
var rad = 180/Math.PI;

var radius = yMax/2 - 10;

var RA = 180/Math.PI;

var hand = function(_fb, x, y, angle, length, width) {
var x0 = xMax/2 + Math.sin(angle/rad);
var y0 = yMax/2 - Math.cos(angle/rad);
var x0 = xMax/2 + Math.sin(angle/RA);
var y0 = yMax/2 - Math.cos(angle/RA);

var x1 = xMax/2 + Math.sin(angle/rad) * length;
var y1 = yMax/2 - Math.cos(angle/rad) * length;
var x1 = xMax/2 + Math.sin(angle/RA) * length;
var y1 = yMax/2 - Math.cos(angle/RA) * length;

fb.line(x0, y0, x1, y1, width);
}

var drawDial = function() {
fb.color(1, 1, 1);
fb.circle(xMax/2, yMax/2, 100);
fb.circle(xMax/2, yMax/2, radius);

fb.color(0, 0, 0);
for (var a = 0; a < 360; a += 6) {
var x0, y0;

var x0 = xMax/2 + Math.sin(a/rad) * 95;
var y0 = yMax/2 + Math.cos(a/rad) * 95;
var x0 = xMax/2 + Math.sin(a/RA) * (radius * 0.95);
var y0 = yMax/2 + Math.cos(a/RA) * (radius * 0.95);

if (a % 30 == 0) {
x1 = xMax/2 + Math.sin(a/rad) * 85;
y1 = yMax/2 + Math.cos(a/rad) * 85;
fb.line(x0, y0, x1, y1, 5);
x1 = xMax/2 + Math.sin(a/RA) * (radius * 0.85);
y1 = yMax/2 + Math.cos(a/RA) * (radius * 0.85);
fb.line(x0, y0, x1, y1, radius * 0.05);
} else {
x1 = xMax/2 + Math.sin(a/rad) * 90;
y1 = yMax/2 + Math.cos(a/rad) * 90;
fb.line(x0, y0, x1, y1, 1);
x1 = xMax/2 + Math.sin(a/RA) * (radius * 0.90);
y1 = yMax/2 + Math.cos(a/RA) * (radius * 0.90);
fb.line(x0, y0, x1, y1, radius * 0.01);
}
}
}

var updateHands = function() {
fb.color(1, 1, 1);
fb.circle(xMax/2, yMax/2, 85);

var update = function() {
var now = new Date(),
midnight = new Date(
now.getFullYear(),
Expand All @@ -52,19 +54,22 @@ var updateHands = function() {
0,0,0),
hours = (now.getTime() - midnight.getTime())/1000/60/60,
minutes = (hours * 60) % 60;
seconds = parseInt((minutes * 60) % 60);
seconds = parseInt((minutes * 60) % 60);

fb.color(1, 1, 1);
fb.circle(xMax/2, yMax/2, radius * 0.85);
fb.image(xMax/2 - 16, yMax/2 + radius * 0.50 - 16, "raspberry-pi-icon.png");
fb.color(1, 0, 0);
hand(fb, 0, 0, hours/12 * 360, 60, 5);
hand(fb, 0, 0, minutes/60 * 360, 80, 5);
hand(fb, 0, 0, hours/12 * 360, radius * 0.6, radius * 0.05);
hand(fb, 0, 0, minutes/60 * 360, radius * 0.8, radius * 0.05);
fb.color(0, 0, 0);
hand(fb, 0, 0, seconds/60 * 360, 80, 1.5);
hand(fb, 0, 0, seconds/60 * 360, radius * 0.8, radius * 0.015);
fb.color(1, 0, 0);
fb.circle(xMax/2, yMax/2, 7.5);
fb.blit();
fb.circle(xMax/2, yMax/2, radius * 0.075);
fb.blit(); // Transfer the back buffer to the screen buffer
};

drawDial();
setInterval(function() {
updateHands();
update();
}, 100);
5 changes: 3 additions & 2 deletions examples/lines.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var pitft = require("../pitft");

var fb = pitft("/dev/fb1");
var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode

// Clear the screen buffer
fb.clear();

var xMax = fb.size().width;
Expand All @@ -21,5 +22,5 @@ for (var n=0; n<500; n++) {
b = Math.random();

fb.color(r, g, b);
fb.line(x0, y0, x1, y1, 1, r, g, b);
fb.line(x0, y0, x1, y1, 1, r, g, b); // Draw a line from (x0, y0) to (x1, y1) with a width of one pixel, and the color (r, g, b)
}
8 changes: 5 additions & 3 deletions examples/rectangles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var pitft = require("../pitft");

var fb = pitft("/dev/fb1");
var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode

// Clear the screen buffer
fb.clear();

var xMax = fb.size().width;
Expand All @@ -24,7 +26,7 @@ for (var n=0; n<500; n++) {
b = Math.random();

fb.color(r, g, b);
fb.rect(x, y, w, h, false, 1);
fb.rect(x, y, w, h, false, 1); // Draw an outlined rectangle with a 1 pixel wide border
}

fb.clear();
Expand All @@ -47,5 +49,5 @@ for (var n=0; n<500; n++) {
b = Math.random();

fb.color(r, g, b);
fb.rect(x, y, w, h, true);
fb.rect(x, y, w, h, true); // Draw a filled rectangle
}
18 changes: 10 additions & 8 deletions examples/text.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
var pitft = require("../pitft");

var fb = pitft("/dev/fb1");
var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode

// Clear the screen buffer
fb.clear();

var xMax = fb.size().width;
var yMax = fb.size().height;

fb.color(1, 1, 1);
fb.color(1, 1, 1); // Set the color to white

for (var a=0; a<=90; a+=15) {
fb.font("fantasy", 12, false, a);
fb.text(20, 20, "Rotated text");
fb.font("fantasy", 12); // Use the "fantasy" font with size 12
fb.text(20, 20, "Rotated text", false, a); // Draw the text non-centered, rotated _a_ degrees
}

for (var a=0; a<=180; a+=15) {
fb.font("fantasy", 24, true, a);
fb.text(xMax/2, yMax/2, "Rotated text");
fb.font("fantasy", 24, true); // Use the "fantasy" font with size 24, and font weight bold
fb.text(xMax/2, yMax/2, "Rotated text", true, a); // Draw the text centered, rotated _a_ degrees
}

for (var a=180; a<=270; a+=15) {
fb.font("fantasy", 12, false, a);
fb.text(xMax-20, yMax-20, "Rotated text");
fb.font("fantasy", 12); // Use the "fantasy" font with size 12
fb.text(xMax-20, yMax-20, "Rotated text", false, a); // Draw the text non-centered, rotated _a_ degrees
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pitft",
"description": "A NodeJS module that gives you access to the PiTFT framebuffer",
"version": "0.0.8",
"version": "0.0.9",
"main": "pitft.js",
"author": "Werner Vesteraas <[email protected]>",
"license": {
Expand Down
20 changes: 12 additions & 8 deletions src/framebuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ NAN_METHOD(FrameBuffer::Font) {

obj->fontName = _fontName.c_str();
obj->fontSize = args[1]->IsUndefined() ? 12 : args[1]->NumberValue();
obj->textCentered = args[2]->IsUndefined() ? false : args[2]->BooleanValue();
obj->textRotation = args[3]->IsUndefined() ? 0 : args[3]->NumberValue();
obj->fontBold = args[2]->IsUndefined() ? false : args[2]->BooleanValue();

NanReturnUndefined();
}
Expand All @@ -242,25 +241,30 @@ NAN_METHOD(FrameBuffer::Text) {
v8::String::Utf8Value text(args[2]->ToString());
std::string _text = std::string(*text);

bool textCentered = args[3]->IsUndefined() ? false : args[3]->BooleanValue();
double textRotation = args[4]->IsUndefined() ? 0 : args[4]->NumberValue();

FrameBuffer *obj = ObjectWrap::Unwrap<FrameBuffer>(args.Holder());

cairo_t *cr = getDrawingContext(obj);

cairo_set_source_rgb(cr, obj->r, obj->g, obj->b);

cairo_select_font_face(cr, obj->fontName,
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
if (obj->fontBold) {
cairo_select_font_face(cr, obj->fontName, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
} else {
cairo_select_font_face(cr, obj->fontName, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
}

cairo_set_font_size(cr, obj->fontSize);

cairo_translate(cr, x, y);

if (obj->textRotation != 0) {
cairo_rotate(cr, obj->textRotation / (180.0 / 3.141592654));
if (textRotation != 0) {
cairo_rotate(cr, textRotation / (180.0 / 3.141592654));
}

if (obj->textCentered) {
if (textCentered) {
cairo_text_extents_t extents;
cairo_text_extents(cr, _text.c_str(), &extents);

Expand Down
4 changes: 1 addition & 3 deletions src/framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ class FrameBuffer : public node::ObjectWrap {

const char *fontName;
double fontSize;

double textRotation;
bool textCentered;
bool fontBold;

bool drawToBuffer;
};
Expand Down

0 comments on commit c4bdbe2

Please sign in to comment.