Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

}

View.initialize(); // Initialize the UI


}

// Load your resources here


function onLayout(dc) {
}

// Called when this View is brought to the foreground.


function onShow() {
// (re)start the timer
tempTimer.start(method(:updateTemp), 10000, true);
}

// Update the view


function onUpdate(dc) {
dc.clear(); // Clear the display

// Set a white color for drawing and draw a rectangle


dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT);
dc.fillRectangle(0, 0, dc.getWidth(), dc.getHeight());

// Draw black text against a white


// background for the temperature
dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_WHITE);
dc.drawText(dc.getWidth()/2, 0, Gfx.FONT_XTINY,
"Temperature:"+ mTemp, Gfx.TEXT_JUSTIFY_CENTER);
}

// Called when this View is removed from the screen.


function onHide() {
tempTimer.stop(); // Stop the timer
}

Here’s where you declare which libraries you are using. The as
keyword lets you assign a shorter alias for them. So instead of
writing Graphics, I can write Gfx throughout this file.

These three variables represent the current temperature read


from the sensor, a timer object for updating the value periodi‐
cally, and a sensor object to refer to the sensor itself.

This function is called by the timer, and does two things: gets
the latest value from the sensor and forces the user interface to
refresh, which causes the onUpdate() function to be called.

44 | Chapter 4: Projects

You might also like