Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
1b73fd9c3This file is pinned to the selected release. Download the original to open it in your design tool.
<!DOCTYPE html>
<html lang="en" class="js csstransforms3d">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="generator" content="Hugo 0.49" />
<meta name="description" content="All about the NodeConf EU 2017 Badge">
<meta name="author" content="nearForm">
<link rel="shortcut icon" href="/images/favicon.png" type="image/x-icon" />
<link rel="icon" href="/images/favicon.png" type="image/x-icon" />
<title>Graphics :: NodeConf EU Badge</title>
<link href="/css/nucleus.css?1537939880" rel="stylesheet">
<link href="/css/font-awesome.min.css?1537939880" rel="stylesheet">
<link href="/css/hybrid.css?1537939880" rel="stylesheet">
<link href="/css/featherlight.min.css?1537939880" rel="stylesheet">
<link href="/css/perfect-scrollbar.min.css?1537939880" rel="stylesheet">
<link href="/css/auto-complete.css?1537939880" rel="stylesheet">
<link href="/css/theme.css?1537939880" rel="stylesheet">
<link href="/css/hugo-theme.css?1537939880" rel="stylesheet">
<script src="/js/jquery-2.x.min.js?1537939880"></script>
<style type="text/css">
:root #header + #content > #left > #rlblock_left{
display:none !important;
}
</style>
</head>
<body class="" data-url="/workshop/graphics/">
<nav id="sidebar" class="">
<div id="header-wrapper">
<div id="header">
<a href="/"><img src="/images/logo.png" /></a>
</div>
<div class="searchbox">
<label for="search-by"><i class="fa fa-search"></i></label>
<input data-search-input id="search-by" type="text" placeholder="Search...">
<span data-search-clear=""><i class="fa fa-close"></i></span>
</div>
<script type="text/javascript" src="/js/lunr.min.js?1537939880"></script>
<script type="text/javascript" src="/js/auto-complete.js?1537939880"></script>
<script type="text/javascript">
var baseurl = "http:\/\/nodeconfeubadge.org";
</script>
<script type="text/javascript" src="/js/search.js?1537939880"></script>
</div>
<div class="highlightable">
<ul class="topics">
<li data-nav-id="/getting-started/" title="Getting Started" class="dd-item
">
<a href="/getting-started/">
Getting Started
</a>
<ul>
<li data-nav-id="/getting-started/unlocking-your-badge/" title="Unlocking Your Badge" class="dd-item ">
<a href="/getting-started/unlocking-your-badge/">
Unlocking Your Badge
</a>
</li>
<li data-nav-id="/getting-started/built-in-software/" title="Built-in Software" class="dd-item ">
<a href="/getting-started/built-in-software/">
Built-in Software
</a>
</li>
<li data-nav-id="/getting-started/connecting-to-the-badge/" title="Connecting to the Badge" class="dd-item ">
<a href="/getting-started/connecting-to-the-badge/">
Connecting to the Badge
</a>
</li>
<li data-nav-id="/getting-started/hello-world-console/" title="Hello World on the Console" class="dd-item ">
<a href="/getting-started/hello-world-console/">
Hello World on the Console
</a>
</li>
<li data-nav-id="/getting-started/other-code-samples/" title="Other Code Samples" class="dd-item ">
<a href="/getting-started/other-code-samples/">
Other Code Samples
</a>
</li>
<li data-nav-id="/getting-started/accessing-hardware-features/" title="Accessing Hardware Features" class="dd-item ">
<a href="/getting-started/accessing-hardware-features/">
Accessing Hardware Features
</a>
</li>
<li data-nav-id="/getting-started/using-prototyping-area/" title="Using the prototyping area and corners" class="dd-item ">
<a href="/getting-started/using-prototyping-area/">
Using the prototyping area and corners
</a>
</li>
<li data-nav-id="/getting-started/faq/" title="FAQ" class="dd-item ">
<a href="/getting-started/faq/">
FAQ
</a>
</li>
</ul>
</li>
<li data-nav-id="/challenges/" title="Challenges" class="dd-item
">
<a href="/challenges/">
Challenges
</a>
<ul>
<li data-nav-id="/challenges/hello-world-screen/" title="Hello World on screen" class="dd-item ">
<a href="/challenges/hello-world-screen/">
Hello World on screen
</a>
</li>
<li data-nav-id="/challenges/detect-button-press/" title="Detect Button Press" class="dd-item ">
<a href="/challenges/detect-button-press/">
Detect Button Press
</a>
</li>
<li data-nav-id="/challenges/detect-capacitive-touch/" title="Detect Capacitive Touch" class="dd-item ">
<a href="/challenges/detect-capacitive-touch/">
Detect Capacitive Touch
</a>
</li>
<li data-nav-id="/challenges/flash-leds/" title="Flash LEDs" class="dd-item ">
<a href="/challenges/flash-leds/">
Flash LEDs
</a>
</li>
<li data-nav-id="/challenges/play-music-with-buzzer/" title="Play Music with Buzzer" class="dd-item ">
<a href="/challenges/play-music-with-buzzer/">
Play Music with Buzzer
</a>
</li>
<li data-nav-id="/challenges/on-screen-menu/" title="On-Screen Menu" class="dd-item ">
<a href="/challenges/on-screen-menu/">
On-Screen Menu
</a>
</li>
<li data-nav-id="/challenges/broadcast-nfc-url/" title="Broadcast NFC URL" class="dd-item ">
<a href="/challenges/broadcast-nfc-url/">
Broadcast NFC URL
</a>
</li>
<li data-nav-id="/challenges/broadcast-beacons/" title="Broadcast Beacons" class="dd-item ">
<a href="/challenges/broadcast-beacons/">
Broadcast Beacons
</a>
</li>
<li data-nav-id="/challenges/use-noble-on-laptop/" title="Use Noble on Laptop" class="dd-item ">
<a href="/challenges/use-noble-on-laptop/">
Use Noble on Laptop
</a>
</li>
<li data-nav-id="/challenges/use-badge-for-hid/" title="Use the Badge as a HID Button" class="dd-item ">
<a href="/challenges/use-badge-for-hid/">
Use the Badge as a HID Button
</a>
</li>
<li data-nav-id="/challenges/display-an-image/" title="Display an Image" class="dd-item ">
<a href="/challenges/display-an-image/">
Display an Image
</a>
</li>
<li data-nav-id="/challenges/create-web-bluetooth-page/" title="Create a Web Bluetooth Page" class="dd-item ">
<a href="/challenges/create-web-bluetooth-page/">
Create a Web Bluetooth Page
</a>
</li>
<li data-nav-id="/challenges/detect-person-with-pir-sensor/" title="Detect movement with a PIR Sensor" class="dd-item ">
<a href="/challenges/detect-person-with-pir-sensor/">
Detect movement with a PIR Sensor
</a>
</li>
<li data-nav-id="/challenges/connect-to-ble-lightbulb/" title="Connect to BLE Lightbulb" class="dd-item ">
<a href="/challenges/connect-to-ble-lightbulb/">
Connect to BLE Lightbulb
</a>
</li>
<li data-nav-id="/challenges/connect-to-ble-led-strip/" title="Connect to BLE LED Strip" class="dd-item ">
<a href="/challenges/connect-to-ble-led-strip/">
Connect to BLE LED Strip
</a>
</li>
<li data-nav-id="/challenges/communicate-with-another-badge/" title="Communicate with another Badge" class="dd-item ">
<a href="/challenges/communicate-with-another-badge/">
Communicate with another Badge
</a>
</li>
<li data-nav-id="/challenges/communicate-with-other-ble-device/" title="Communicate with another Device" class="dd-item ">
<a href="/challenges/communicate-with-other-ble-device/">
Communicate with another Device
</a>
</li>
<li data-nav-id="/challenges/implement-dat-protocol/" title="Implement Dat Protocol" class="dd-item ">
<a href="/challenges/implement-dat-protocol/">
Implement Dat Protocol
</a>
</li>
</ul>
</li>
<li data-nav-id="/workshop/" title="Workshop" class="dd-item
parent
">
<a href="/workshop/">
Workshop
</a>
<ul>
<li data-nav-id="/workshop/get-connected/" title="Get Connected" class="dd-item ">
<a href="/workshop/get-connected/">
Get Connected
</a>
</li>
<li data-nav-id="/workshop/clean-slate/" title="Clean Slate" class="dd-item ">
<a href="/workshop/clean-slate/">
Clean Slate
</a>
</li>
<li data-nav-id="/workshop/capacitive-sense/" title="Capacitive Sense" class="dd-item ">
<a href="/workshop/capacitive-sense/">
Capacitive Sense
</a>
</li>
<li data-nav-id="/workshop/graphics/" title="Graphics" class="dd-item active">
<a href="/workshop/graphics/">
Graphics
</a>
</li>
<li data-nav-id="/workshop/sound/" title="Sound" class="dd-item ">
<a href="/workshop/sound/">
Sound
</a>
</li>
<li data-nav-id="/workshop/making-a-game/" title="Making a Game" class="dd-item ">
<a href="/workshop/making-a-game/">
Making a Game
</a>
</li>
<li data-nav-id="/workshop/what-next/" title="What next?" class="dd-item ">
<a href="/workshop/what-next/">
What next?
</a>
</li>
<li data-nav-id="/workshop/adding-to-the-menu/" title="Adding to your Badge's menu" class="dd-item ">
<a href="/workshop/adding-to-the-menu/">
Adding to your Badge's menu
</a>
</li>
</ul>
</li>
</ul>
<section id="footer">
<p>Built using the
<a href="https://github.com/matcornic/hugo-theme-learn">Learn</a> theme on
<a href="http://gohugo.io/">Hugo</a> ported from
<a href="http://getgrav.org">Grav</a>.
</p>
</section>
</div>
</nav>
<section id="body">
<div id="overlay"></div>
<div class="padding highlightable sticky-parent">
<div class="sticky-spacer">
<div id="top-bar">
<div id="breadcrumbs" itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
<span id="sidebar-toggle-span">
<a href="#" id="sidebar-toggle" data-sidebar-toggle="">
<i class="fa fa-bars"></i>
</a>
</span>
<span id="toc-menu"><i class="fa fa-list-alt"></i></span>
<span class="links">
<a href='/'></a> > <a href='/workshop/'>Workshop</a> > Graphics
</span>
</div>
<div class="progress">
<div class="wrapper">
</div>
</div>
</div>
</div>
<div id="body-inner">
<h1>Graphics</h1>
<p>The badge exposes the LCD via a built-in variable called <code>g</code>. This has
a bunch of methods such as <code>drawLine</code> or <code>drawString</code> that can be accessed
(<a href="http://www.espruino.com/Graphics">http://www.espruino.com/Graphics</a>) however each command acts on an internal
memory buffer. To update the display itself you need to call <code>g.flip()</code>.</p>
<p>Try entering the following code on the left-hand side:</p>
<pre><code>g.clear();
g.drawString("Hello World!");
g.flip();
</code></pre>
<p>The badge should now display the obvious ‘Hello World’!</p>
<p>But what if we want to display our capacitance data? Enter the following
on the <em>left-hand side</em>.</p>
<pre><code>function plotCap() {
g.clear();
var caps = [1,2,3,4,5,6].map(c=>Badge.capSense(c));
caps.forEach(
(cap,x) => g.fillRect(x*21,64,(x+1)*21, 64-cap/1000)
);
g.flip();
}
setInterval(plotCap,50)
</code></pre>
<p><img src="/images/workshop_graphics1.jpg" alt="Plot capacitance" /></p>
<p><strong>Note:</strong> This uses a whole bunch of battery power because of polling the
capacitive sense as well as updating the screen, and if left this way
could run the Badge’s battery down in a few hours. When using the buttons
rather than capacitive sense, the hardware itself can detect the change
in the wire’s voltage while the processor is asleep - you do that
using <code>setWatch</code> - which is super power efficient.</p>
<p>This is just displaying a few bars, but it would make more sense to display
the status on a hexagon that matched the badge itself:</p>
<pre><code>function plotBadge(presses) {
g.clear();
// function to work out each corner's coordinates
function corner(c,r) {
var a = (c-2)*Math.PI/3;
return [64+Math.sin(a)*40*r, 32-Math.cos(a)*30*r];
}
// draw a hexagon - a line to each corner in turn
g.moveTo.apply(g,corner(6,1));
[1,2,3,4,5,6].forEach(c=>g.lineTo.apply(g,corner(c,1)));
// Draw a circle where pressed
presses.forEach((hit,c) => {
if (!hit) return;
var coords = corner(c+1,0.8);
g.fillCircle(coords[0],coords[1],5);
});
g.flip();
}
function plotCap() {
var caps = [1,2,3,4,5,6].map(c=>Badge.capSense(c)>20000);
plotBadge(caps);
}
</code></pre>
<p><img src="/images/workshop_graphics2.jpg" alt="Plot corners" /></p>
<p>We’re using a separate <code>plotBadge</code> function here so we can use it
later to output the pattern we want the user to enter.</p>
<p><strong>Note:</strong> because we’re entering this on the left-hand side we
already have the interval from the last code we entered - just
re-entering <code>plotCap</code> will replace the old one and the code will magically
start working.</p>
<footer class=" footline" >
</footer>
</div>
</div>
<div id="navigation">
<a class="nav nav-prev" href="/workshop/capacitive-sense/" title="Capacitive Sense"> <i class="fa fa-chevron-left"></i></a>
<a class="nav nav-next" href="/workshop/sound/" title="Sound" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
</div>
</section>
<div style="left: -1000px; overflow: scroll; position: absolute; top: -1000px; border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;">
<div style="border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;"></div>
</div>
<script src="/js/clipboard.min.js?1537939880"></script>
<script src="/js/perfect-scrollbar.min.js?1537939880"></script>
<script src="/js/perfect-scrollbar.jquery.min.js?1537939880"></script>
<script src="/js/jquery.sticky-kit.min.js?1537939880"></script>
<script src="/js/featherlight.min.js?1537939880"></script>
<script src="/js/html5shiv-printshiv.min.js?1537939880"></script>
<script src="/js/highlight.pack.js?1537939880"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="/js/modernizr.custom.71422.js?1537939880"></script>
<script src="/js/learn.js?1537939880"></script>
<script src="/js/hugo-learn.js?1537939880"></script>
<link href="/mermaid/mermaid.css?1537939880" type="text/css" rel="stylesheet" />
<script src="/mermaid/mermaid.js?1537939880"></script>
<script>
mermaid.initialize({ startOnLoad: true });
</script>
</body>
</html>