mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
427 lines
19 KiB
HTML
427 lines
19 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<title>ArduinoLibs: Running figure example</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body onload='searchBox.OnSelectItem(0);'>
|
|
<!-- Generated by Doxygen 1.7.4 -->
|
|
<script type="text/javascript"><!--
|
|
var searchBox = new SearchBox("searchBox", "search",false,'Search');
|
|
--></script>
|
|
<div id="top">
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td style="padding-left: 0.5em;">
|
|
<div id="projectname">ArduinoLibs</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div id="navrow1" class="tabs">
|
|
<ul class="tablist">
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="annotated.html"><span>Classes</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li id="searchli">
|
|
<div id="MSearchBox" class="MSearchBoxInactive">
|
|
<span class="left">
|
|
<img id="MSearchSelect" src="search/mag_sel.png"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
alt=""/>
|
|
<input type="text" id="MSearchField" value="Search" accesskey="S"
|
|
onfocus="searchBox.OnSearchFieldFocus(true)"
|
|
onblur="searchBox.OnSearchFieldFocus(false)"
|
|
onkeyup="searchBox.OnSearchFieldChange(event)"/>
|
|
</span><span class="right">
|
|
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
|
|
</span>
|
|
</div>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">Running figure example </div> </div>
|
|
</div>
|
|
<div class="contents">
|
|
<div class="textblock"><p>This example demonstrates how to draw animated bitmaps to <a href="http://www.freetronics.com/dmd">Freetronics Large Dot Matrix Displays</a>. These displays have 512 LED's arranged in a 32x16 matrix and controlled by an SPI interface. The displays are available in red, blue, green, yellow, and white variations.</p>
|
|
<p>The first step is to initialize the display:</p>
|
|
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include <DMD.h></span>
|
|
|
|
<a class="code" href="classDMD.html" title="Handle large dot matrix displays composed of LED's.">DMD</a> display;
|
|
</pre></div></p>
|
|
<p>We will also need some bitmaps to animate the running figure. We will use static bitmaps stored in program memory. The first frame of the 10-frame animation is:</p>
|
|
<div class="fragment"><pre class="fragment">byte <span class="keyword">const</span> run1[] PROGMEM = {
|
|
16, 16,
|
|
B00000000, B00001100,
|
|
B00000000, B00011110,
|
|
B00000111, B11111110,
|
|
B00001111, B11111110,
|
|
B00011100, B11111100,
|
|
B00000001, B11111100,
|
|
B00000001, B11110000,
|
|
B00000011, B11111000,
|
|
B00000111, B00011000,
|
|
B00001110, B01110000,
|
|
B00011100, B01100000,
|
|
B00111000, B00000000,
|
|
B01110000, B00000000,
|
|
B01100000, B00000000,
|
|
B01000000, B00000000,
|
|
B00000000, B00000000
|
|
};
|
|
</pre></div></p>
|
|
<p>As can be seen, the bitmap is made up of 0's and 1's; a 1 bit indicates that the corresponding LED will be lit when it is drawn to the dot matrix display. The first two bytes are the width and height of the bitmap in pixels. In this case, the first frame is 16x16 pixels. Other frames in the animation are 18x16 and 13x16.</p>
|
|
<p>We store pointers to all of the frames in a common array:</p>
|
|
<div class="fragment"><pre class="fragment"><span class="keyword">const</span> prog_uint8_t *frames[] = {
|
|
run1,
|
|
run2,
|
|
run3,
|
|
run4,
|
|
run5,
|
|
run6,
|
|
run7,
|
|
run8,
|
|
run9,
|
|
run10
|
|
};
|
|
<span class="preprocessor">#define NUM_FRAMES (sizeof(frames) / sizeof(frames[0]))</span>
|
|
<span class="preprocessor">unsigned int frame = 0;</span>
|
|
</pre></div></p>
|
|
<p>All that remains is to run the animation loop:</p>
|
|
<div class="fragment"><pre class="fragment"><span class="preprocessor">#define ADVANCE_MS (1000 / NUM_FRAMES)</span>
|
|
<span class="preprocessor"></span><span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastFrame;
|
|
|
|
<span class="keywordtype">void</span> setup() {
|
|
lastFrame = millis() - ADVANCE_MS;
|
|
}
|
|
|
|
<span class="keywordtype">void</span> loop() {
|
|
<span class="keywordflow">if</span> ((millis() - lastFrame) >= ADVANCE_MS) {
|
|
display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1" title="Clears the entire bitmap to the specified color.">clear</a>();
|
|
<span class="keywordtype">int</span> x = (32 - pgm_read_byte(frames[frame])) / 2;
|
|
display.<a class="code" href="classBitmap.html#a491e9c0bb20ddf5a5eb4933077c8ed72" title="Draws bitmap at (x, y) in color.">drawBitmap</a>(x, 0, frames[frame]);
|
|
lastFrame += ADVANCE_MS;
|
|
frame = (frame + 1) % NUM_FRAMES;
|
|
}
|
|
display.<a class="code" href="classDMD.html#a2c74a0845ef6080056b972d490648114" title="Performs regular display refresh activities from the application's main loop.">loop</a>();
|
|
}
|
|
</pre></div></p>
|
|
<p>Each time <code>ADVANCE_MS</code> milliseconds expires, we clear the display and draw a bitmap centered on the screen. To help with the centering, we read the width value from the bitmap for the current frame (the height is always 16). We must also call <a class="el" href="classDMD.html#a2c74a0845ef6080056b972d490648114" title="Performs regular display refresh activities from the application's main loop.">DMD::loop()</a> repeatedly from the application's main <code>loop()</code> function to ensure that the display is kept refreshed.</p>
|
|
<p>Sometimes it can be inconvenient to arrange for <a class="el" href="classDMD.html#a2c74a0845ef6080056b972d490648114" title="Performs regular display refresh activities from the application's main loop.">DMD::loop()</a> to be called regularly. An alternative is to use Timer1 or Timer2 and <a class="el" href="classDMD.html#dmd_interrupts">interrupt-driven display refresh</a>:</p>
|
|
<div class="fragment"><pre class="fragment"><span class="preprocessor">#define ADVANCE_MS (1000 / NUM_FRAMES)</span>
|
|
<span class="preprocessor"></span>
|
|
ISR(TIMER1_OVF_vect)
|
|
{
|
|
display.<a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8" title="Refresh the display.">refresh</a>();
|
|
}
|
|
|
|
<span class="keywordtype">void</span> setup() {
|
|
display.<a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2" title="Enables Timer1 overflow interrupts for updating this display.">enableTimer1</a>();
|
|
}
|
|
|
|
<span class="keywordtype">void</span> loop() {
|
|
display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1" title="Clears the entire bitmap to the specified color.">clear</a>();
|
|
<span class="keywordtype">int</span> x = (32 - pgm_read_byte(frames[frame])) / 2;
|
|
display.<a class="code" href="classBitmap.html#a491e9c0bb20ddf5a5eb4933077c8ed72" title="Draws bitmap at (x, y) in color.">drawBitmap</a>(x, 0, frames[frame]);
|
|
frame = (frame + 1) % NUM_FRAMES;
|
|
|
|
delay(ADVANCE_MS);
|
|
}
|
|
</pre></div></p>
|
|
<p>In the case of Timer2, <code>TIMER2_OVF_vect</code> and <a class="el" href="classDMD.html#a5469775db7fafebca2cdbc6a6372fb97">enableTimer2()</a> would be used in place of <code>TIMER1_OVF_vect</code> and <a class="el" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2">enableTimer1()</a>.</p>
|
|
<p>The full source code for the example follows:</p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
|
|
<span class="comment"> * Copyright (C) 2012 Southern Storm Software, Pty Ltd.</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</span>
|
|
<span class="comment"> * copy of this software and associated documentation files (the "Software"),</span>
|
|
<span class="comment"> * to deal in the Software without restriction, including without limitation</span>
|
|
<span class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</span>
|
|
<span class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</span>
|
|
<span class="comment"> * Software is furnished to do so, subject to the following conditions:</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * The above copyright notice and this permission notice shall be included</span>
|
|
<span class="comment"> * in all copies or substantial portions of the Software.</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS</span>
|
|
<span class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
|
|
<span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
|
|
<span class="comment"> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
|
|
<span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span>
|
|
<span class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</span>
|
|
<span class="comment"> * DEALINGS IN THE SOFTWARE.</span>
|
|
<span class="comment"> */</span>
|
|
|
|
<span class="preprocessor">#include <DMD.h></span>
|
|
|
|
<a class="code" href="classDMD.html" title="Handle large dot matrix displays composed of LED's.">DMD</a> display;
|
|
|
|
<span class="comment">// Running stick figure pictures are loosely based on those from this tutorial:</span>
|
|
<span class="comment">// http://www.fluidanims.com/FAelite/phpBB3/viewtopic.php?f=10&t=102</span>
|
|
|
|
byte <span class="keyword">const</span> run1[] PROGMEM = {
|
|
16, 16,
|
|
B00000000, B00001100,
|
|
B00000000, B00011110,
|
|
B00000111, B11111110,
|
|
B00001111, B11111110,
|
|
B00011100, B11111100,
|
|
B00000001, B11111100,
|
|
B00000001, B11110000,
|
|
B00000011, B11111000,
|
|
B00000111, B00011000,
|
|
B00001110, B01110000,
|
|
B00011100, B01100000,
|
|
B00111000, B00000000,
|
|
B01110000, B00000000,
|
|
B01100000, B00000000,
|
|
B01000000, B00000000,
|
|
B00000000, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run2[] PROGMEM = {
|
|
18, 16,
|
|
B00000000, B01110011, B10000000,
|
|
B00000000, B11111111, B10000000,
|
|
B00000000, B00011111, B10000000,
|
|
B00000000, B00111111, B11000000,
|
|
B00000000, B01111011, B11000000,
|
|
B00000000, B11110011, B10000000,
|
|
B00000001, B11100000, B00000000,
|
|
B00000011, B11100000, B00000000,
|
|
B00000111, B01110000, B00000000,
|
|
B01111110, B00111000, B00000000,
|
|
B11111100, B00011100, B00000000,
|
|
B00000000, B00001110, B00000000,
|
|
B00000000, B00000111, B00000000,
|
|
B00000000, B00000011, B10000000,
|
|
B00000000, B00000001, B00000000,
|
|
B00000000, B00000000, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run3[] PROGMEM = {
|
|
18, 16,
|
|
B00000000, B00110000, B00000000,
|
|
B00000000, B01111000, B00000000,
|
|
B00000000, B00011111, B00000000,
|
|
B00000000, B00011111, B00000000,
|
|
B00000000, B00111111, B10000000,
|
|
B00000000, B01111111, B11000000,
|
|
B00000000, B11100011, B10000000,
|
|
B00000001, B11000000, B00000000,
|
|
B00000011, B11100000, B00000000,
|
|
B11111111, B01110000, B00000000,
|
|
B11111110, B00111000, B00000000,
|
|
B00000000, B00011000, B00000000,
|
|
B00000000, B00011100, B00000000,
|
|
B00000000, B00001110, B00000000,
|
|
B00000000, B00000100, B00000000,
|
|
B00000000, B00000000, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run4[] PROGMEM = {
|
|
16, 16,
|
|
B00000001, B11100000,
|
|
B00000011, B11111100,
|
|
B00000000, B00111110,
|
|
B00000000, B01111110,
|
|
B00000000, B11111100,
|
|
B00000001, B10011111,
|
|
B00000011, B00001110,
|
|
B00000011, B00000000,
|
|
B00000011, B10000000,
|
|
B11111111, B10000000,
|
|
B11111000, B11000000,
|
|
B00000001, B11000000,
|
|
B00000011, B10000000,
|
|
B00000111, B00000000,
|
|
B00000110, B00000000,
|
|
B00000100, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run5[] PROGMEM = {
|
|
13, 16,
|
|
B00000000, B00000000,
|
|
B00000000, B00110000,
|
|
B00000111, B11111000,
|
|
B00000111, B11111000,
|
|
B00000111, B11110000,
|
|
B00001111, B11100000,
|
|
B00000111, B00000000,
|
|
B00001111, B00000000,
|
|
B00001111, B00000000,
|
|
B00001111, B10000000,
|
|
B00011100, B00000000,
|
|
B00111000, B00000000,
|
|
B01110000, B00000000,
|
|
B11100000, B00000000,
|
|
B11000000, B00000000,
|
|
B10000000, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run6[] PROGMEM = {
|
|
16, 16,
|
|
B00000000, B00000000,
|
|
B00000000, B00011100,
|
|
B00000000, B00111110,
|
|
B00000001, B11111110,
|
|
B00000000, B11100000,
|
|
B00000001, B11100000,
|
|
B00000001, B11111000,
|
|
B00000011, B00011100,
|
|
B00000110, B00111000,
|
|
B00000110, B01110000,
|
|
B00001100, B00100000,
|
|
B00111000, B00000000,
|
|
B01100000, B00000000,
|
|
B11000000, B00000000,
|
|
B10000000, B00000000,
|
|
B10000000, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run7[] PROGMEM = {
|
|
18, 16,
|
|
B00000000, B00000011, B10000000,
|
|
B00000000, B01111011, B10000000,
|
|
B00000000, B01111111, B10000000,
|
|
B00000000, B00001111, B00100000,
|
|
B00000000, B00011001, B11000000,
|
|
B00000000, B00110000, B11000000,
|
|
B00000000, B01110000, B00000000,
|
|
B00000001, B11110000, B00000000,
|
|
B11111111, B10111000, B00000000,
|
|
B11111111, B00011100, B00000000,
|
|
B00000000, B00001110, B00000000,
|
|
B00000000, B00000111, B00000000,
|
|
B00000000, B00000011, B10000000,
|
|
B00000000, B00000001, B11000000,
|
|
B00000000, B00000000, B01000000,
|
|
B00000000, B00000000, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run8[] PROGMEM = {
|
|
18, 16,
|
|
B00000000, B00000110, B00000000,
|
|
B00000001, B11101111, B00000000,
|
|
B00000001, B11111111, B00000000,
|
|
B00000000, B00111110, B00000000,
|
|
B00000000, B01111111, B11000000,
|
|
B00000000, B11100011, B10000000,
|
|
B00000001, B11000000, B00000000,
|
|
B00000011, B11100000, B00000000,
|
|
B11111111, B01110000, B00000000,
|
|
B11111110, B00111000, B00000000,
|
|
B00000000, B00011100, B00000000,
|
|
B00000000, B00000110, B00000000,
|
|
B00000000, B00000110, B00000000,
|
|
B00000000, B00000111, B00000000,
|
|
B00000000, B00000011, B00000000,
|
|
B00000000, B00000001, B00000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run9[] PROGMEM = {
|
|
16, 16,
|
|
B00000000, B00000000,
|
|
B00000000, B01001110,
|
|
B00000001, B11101110,
|
|
B00000011, B11111110,
|
|
B00000011, B11111110,
|
|
B00000001, B10111100,
|
|
B00000011, B00000000,
|
|
B00000111, B00000000,
|
|
B11111111, B10000000,
|
|
B11111100, B11000000,
|
|
B00000000, B11000000,
|
|
B00000000, B11000000,
|
|
B00000000, B11000000,
|
|
B00000000, B11000000,
|
|
B00000000, B11000000,
|
|
B00000000, B11000000
|
|
};
|
|
|
|
byte <span class="keyword">const</span> run10[] PROGMEM = {
|
|
13, 16,
|
|
B00000000, B00000000,
|
|
B00000000, B00110000,
|
|
B00000000, B01111000,
|
|
B00000111, B11111000,
|
|
B00001111, B11111000,
|
|
B00000111, B11000000,
|
|
B00001110, B00000000,
|
|
B00001100, B00000000,
|
|
B00001100, B00000000,
|
|
B01111100, B00000000,
|
|
B11111100, B00000000,
|
|
B00011000, B00000000,
|
|
B00110000, B00000000,
|
|
B01110000, B00000000,
|
|
B01100000, B00000000,
|
|
B01000000, B00000000
|
|
};
|
|
|
|
<span class="keyword">const</span> prog_uint8_t *frames[] = {
|
|
run1,
|
|
run2,
|
|
run3,
|
|
run4,
|
|
run5,
|
|
run6,
|
|
run7,
|
|
run8,
|
|
run9,
|
|
run10
|
|
};
|
|
<span class="preprocessor">#define NUM_FRAMES (sizeof(frames) / sizeof(frames[0]))</span>
|
|
<span class="preprocessor"></span><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> frame = 0;
|
|
|
|
<span class="preprocessor">#define ADVANCE_MS (1000 / NUM_FRAMES)</span>
|
|
<span class="preprocessor"></span><span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastFrame;
|
|
|
|
<span class="keywordtype">void</span> setup() {
|
|
lastFrame = millis() - ADVANCE_MS;
|
|
}
|
|
|
|
<span class="keywordtype">void</span> loop() {
|
|
<span class="keywordflow">if</span> ((millis() - lastFrame) >= ADVANCE_MS) {
|
|
display.<a class="code" href="classBitmap.html#a839dc8fab05a5ebf7a6b2e61436b2fa1" title="Clears the entire bitmap to the specified color.">clear</a>();
|
|
<span class="keywordtype">int</span> x = (32 - pgm_read_byte(frames[frame])) / 2;
|
|
display.<a class="code" href="classBitmap.html#a491e9c0bb20ddf5a5eb4933077c8ed72" title="Draws bitmap at (x, y) in color.">drawBitmap</a>(x, 0, frames[frame]);
|
|
lastFrame += ADVANCE_MS;
|
|
frame = (frame + 1) % NUM_FRAMES;
|
|
}
|
|
display.<a class="code" href="classDMD.html#a2c74a0845ef6080056b972d490648114" title="Performs regular display refresh activities from the application's main loop.">loop</a>();
|
|
}
|
|
</pre></div> </div></div>
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a></div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<hr class="footer"/><address class="footer"><small>Generated on Wed May 30 2012 15:44:34 for ArduinoLibs by 
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
|
|
</body>
|
|
</html>
|