1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Update docs

This commit is contained in:
Rhys Weatherley
2012-05-30 15:44:53 +10:00
parent 98ff8741c0
commit bf683c08fb
162 changed files with 1596 additions and 567 deletions

View File

@@ -94,291 +94,320 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search');
<a name="l00032"></a>00032 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00033"></a>00033 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00034"></a>00034
<a name="l00192"></a>00192 <span class="comment">// Pins on the DMD connector board.</span>
<a name="l00193"></a>00193 <span class="preprocessor">#define DMD_PIN_PHASE_LSB 6 // A</span>
<a name="l00194"></a>00194 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_PHASE_MSB 7 // B</span>
<a name="l00195"></a>00195 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_LATCH 8 // SCLK</span>
<a name="l00196"></a>00196 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_OUTPUT_ENABLE 9 // nOE</span>
<a name="l00197"></a>00197 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_SS SS // SPI Slave Select</span>
<a name="l00198"></a>00198 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_MOSI MOSI // SPI Master Out, Slave In (R)</span>
<a name="l00199"></a>00199 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_MISO MISO // SPI Master In, Slave Out</span>
<a name="l00200"></a>00200 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_SCK SCK // SPI Serial Clock (CLK)</span>
<a name="l00201"></a>00201 <span class="preprocessor"></span>
<a name="l00202"></a>00202 <span class="comment">// Dimension information for the display.</span>
<a name="l00203"></a>00203 <span class="preprocessor">#define DMD_NUM_COLUMNS 32 // Number of columns in a panel.</span>
<a name="l00204"></a>00204 <span class="preprocessor"></span><span class="preprocessor">#define DMD_NUM_ROWS 16 // Number of rows in a panel.</span>
<a name="l00205"></a>00205 <span class="preprocessor"></span>
<a name="l00206"></a>00206 <span class="comment">// Refresh times.</span>
<a name="l00207"></a>00207 <span class="preprocessor">#define DMD_REFRESH_MS 5</span>
<a name="l00208"></a>00208 <span class="preprocessor"></span><span class="preprocessor">#define DMD_REFRESH_US 5000</span>
<a name="l00209"></a>00209 <span class="preprocessor"></span>
<a name="l00219"></a><a class="code" href="classDMD.html#affd37accffe951c8878434dfa1245809">00219</a> <a class="code" href="classDMD.html#affd37accffe951c8878434dfa1245809" title="Constructs a new dot matrix display handler for a display that is widthPanels x heightPanels in size...">DMD::DMD</a>(<span class="keywordtype">int</span> widthPanels, <span class="keywordtype">int</span> heightPanels)
<a name="l00220"></a>00220 : <a class="code" href="classBitmap.html" title="Represents a monochrome bitmap within main memory.">Bitmap</a>(widthPanels * DMD_NUM_COLUMNS, heightPanels * DMD_NUM_ROWS)
<a name="l00221"></a>00221 , _doubleBuffer(false)
<a name="l00222"></a>00222 , phase(0)
<a name="l00223"></a>00223 , fb0(0)
<a name="l00224"></a>00224 , fb1(0)
<a name="l00225"></a>00225 , displayfb(0)
<a name="l00226"></a>00226 , lastRefresh(millis())
<a name="l00227"></a>00227 {
<a name="l00228"></a>00228 <span class="comment">// Both rendering and display are to fb0 initially.</span>
<a name="l00229"></a>00229 fb0 = displayfb = fb;
<a name="l00230"></a>00230
<a name="l00231"></a>00231 <span class="comment">// Initialize SPI to MSB-first, mode 0, clock divider = 2.</span>
<a name="l00232"></a>00232 pinMode(DMD_PIN_SPI_SCK, OUTPUT);
<a name="l00233"></a>00233 pinMode(DMD_PIN_SPI_MOSI, OUTPUT);
<a name="l00234"></a>00234 pinMode(DMD_PIN_SPI_SS, OUTPUT);
<a name="l00235"></a>00235 digitalWrite(DMD_PIN_SPI_SCK, LOW);
<a name="l00236"></a>00236 digitalWrite(DMD_PIN_SPI_MOSI, LOW);
<a name="l00237"></a>00237 digitalWrite(DMD_PIN_SPI_SS, HIGH);
<a name="l00238"></a>00238 SPCR |= _BV(MSTR);
<a name="l00239"></a>00239 SPCR |= _BV(SPE);
<a name="l00240"></a>00240 SPCR &amp;= ~(_BV(DORD)); <span class="comment">// MSB-first</span>
<a name="l00241"></a>00241 SPCR &amp;= ~0x0C; <span class="comment">// Mode 0</span>
<a name="l00242"></a>00242 SPCR &amp;= ~0x03; <span class="comment">// Clock divider rate 2</span>
<a name="l00243"></a>00243 SPSR |= 0x01; <span class="comment">// MSB of clock divider rate</span>
<a name="l00244"></a>00244
<a name="l00245"></a>00245 <span class="comment">// Initialize the DMD-specific pins.</span>
<a name="l00246"></a>00246 pinMode(DMD_PIN_PHASE_LSB, OUTPUT);
<a name="l00247"></a>00247 pinMode(DMD_PIN_PHASE_MSB, OUTPUT);
<a name="l00248"></a>00248 pinMode(DMD_PIN_LATCH, OUTPUT);
<a name="l00249"></a>00249 pinMode(DMD_PIN_OUTPUT_ENABLE, OUTPUT);
<a name="l00250"></a>00250 digitalWrite(DMD_PIN_PHASE_LSB, LOW);
<a name="l00251"></a>00251 digitalWrite(DMD_PIN_PHASE_MSB, LOW);
<a name="l00252"></a>00252 digitalWrite(DMD_PIN_LATCH, LOW);
<a name="l00253"></a>00253 digitalWrite(DMD_PIN_OUTPUT_ENABLE, LOW);
<a name="l00254"></a>00254 digitalWrite(DMD_PIN_SPI_MOSI, HIGH);
<a name="l00255"></a>00255 }
<a name="l00256"></a>00256
<a name="l00260"></a><a class="code" href="classDMD.html#a7b37e05584d3e0308163700920df99b2">00260</a> <a class="code" href="classDMD.html#a7b37e05584d3e0308163700920df99b2" title="Destroys this dot matrix display handler.">DMD::~DMD</a>()
<a name="l00261"></a>00261 {
<a name="l00262"></a>00262 <span class="keywordflow">if</span> (fb0)
<a name="l00263"></a>00263 free(fb0);
<a name="l00264"></a>00264 <span class="keywordflow">if</span> (fb1)
<a name="l00265"></a>00265 free(fb1);
<a name="l00266"></a>00266 fb = 0; <span class="comment">// Don&#39;t free the buffer again in the base class.</span>
<a name="l00267"></a>00267 }
<a name="l00268"></a>00268
<a name="l00296"></a><a class="code" href="classDMD.html#a6fbdcf8832f91d02500cb7a9b84d2723">00296</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a6fbdcf8832f91d02500cb7a9b84d2723" title="Enables or disables double-buffering according to doubleBuffer.">DMD::setDoubleBuffer</a>(<span class="keywordtype">bool</span> doubleBuffer)
<a name="l00297"></a>00297 {
<a name="l00298"></a>00298 <span class="keywordflow">if</span> (doubleBuffer != _doubleBuffer) {
<a name="l00299"></a>00299 _doubleBuffer = <a class="code" href="classDMD.html#aab1f3ba29c053d630ae12865d22166ec" title="Returns true if the display is double-buffered; false if single-buffered. The default is false...">doubleBuffer</a>;
<a name="l00300"></a>00300 <span class="keywordflow">if</span> (doubleBuffer) {
<a name="l00301"></a>00301 <span class="comment">// Allocate a new back buffer.</span>
<a name="l00302"></a>00302 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size = _stride * _height;
<a name="l00303"></a>00303 fb1 = (uint8_t *)malloc(size);
<a name="l00304"></a>00304
<a name="l00305"></a>00305 <span class="comment">// Clear the new back buffer and then switch to it, leaving</span>
<a name="l00306"></a>00306 <span class="comment">// the current contents of fb0 on the screen.</span>
<a name="l00307"></a>00307 <span class="keywordflow">if</span> (fb1) {
<a name="l00308"></a>00308 memset(fb1, 0xFF, size);
<a name="l00309"></a>00309 cli();
<a name="l00310"></a>00310 fb = fb1;
<a name="l00311"></a>00311 displayfb = fb0;
<a name="l00312"></a>00312 sei();
<a name="l00313"></a>00313 } <span class="keywordflow">else</span> {
<a name="l00314"></a>00314 <span class="comment">// Failed to allocate the memory, so revert to single-buffered.</span>
<a name="l00315"></a>00315 _doubleBuffer = <span class="keyword">false</span>;
<a name="l00316"></a>00316 }
<a name="l00317"></a>00317 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fb1) {
<a name="l00318"></a>00318 <span class="comment">// Disabling double-buffering, so forcibly switch to fb0.</span>
<a name="l00319"></a>00319 cli();
<a name="l00320"></a>00320 fb = fb0;
<a name="l00321"></a>00321 displayfb = fb0;
<a name="l00322"></a>00322 sei();
<a name="l00323"></a>00323
<a name="l00324"></a>00324 <span class="comment">// Free the unnecessary buffer.</span>
<a name="l00325"></a>00325 free(fb1);
<a name="l00326"></a>00326 fb1 = 0;
<a name="l00327"></a>00327 }
<a name="l00328"></a>00328 }
<a name="l00329"></a>00329 }
<a name="l00330"></a>00330
<a name="l00345"></a><a class="code" href="classDMD.html#a80269ccd44b3ef9ee15f0a1009b7a60a">00345</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a80269ccd44b3ef9ee15f0a1009b7a60a" title="Swaps the buffers that are used for rendering to the display.">DMD::swapBuffers</a>()
<a name="l00346"></a>00346 {
<a name="l00347"></a>00347 <span class="keywordflow">if</span> (_doubleBuffer) {
<a name="l00348"></a>00348 <span class="comment">// Turn off interrupts while swapping buffers so that we don&#39;t</span>
<a name="l00349"></a>00349 <span class="comment">// accidentally try to refresh() in the middle of this code.</span>
<a name="l00350"></a>00350 cli();
<a name="l00351"></a>00351 <span class="keywordflow">if</span> (fb == fb0) {
<a name="l00352"></a>00352 fb = fb1;
<a name="l00353"></a>00353 displayfb = fb0;
<a name="l00354"></a>00354 } <span class="keywordflow">else</span> {
<a name="l00355"></a>00355 fb = fb0;
<a name="l00356"></a>00356 displayfb = fb1;
<a name="l00357"></a>00357 }
<a name="l00358"></a>00358 sei();
<a name="l00359"></a>00359 }
<a name="l00360"></a>00360 }
<a name="l00361"></a>00361
<a name="l00378"></a><a class="code" href="classDMD.html#a0b1771cf790b2b62eea55e56b02e3736">00378</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a0b1771cf790b2b62eea55e56b02e3736" title="Swaps the buffers that are used for rendering to the display and copies the former back buffer conten...">DMD::swapBuffersAndCopy</a>()
<a name="l00379"></a>00379 {
<a name="l00380"></a>00380 <a class="code" href="classDMD.html#a80269ccd44b3ef9ee15f0a1009b7a60a" title="Swaps the buffers that are used for rendering to the display.">swapBuffers</a>();
<a name="l00381"></a>00381 <span class="keywordflow">if</span> (_doubleBuffer)
<a name="l00382"></a>00382 memcpy(fb, displayfb, _stride * _height);
<a name="l00383"></a>00383 }
<a name="l00384"></a>00384
<a name="l00402"></a><a class="code" href="classDMD.html#a2c74a0845ef6080056b972d490648114">00402</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a2c74a0845ef6080056b972d490648114" title="Performs regular display refresh activities from the application&#39;s main loop.">DMD::loop</a>()
<a name="l00403"></a>00403 {
<a name="l00404"></a>00404 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> currentTime = millis();
<a name="l00405"></a>00405 <span class="keywordflow">if</span> ((currentTime - lastRefresh) &gt;= DMD_REFRESH_MS) {
<a name="l00406"></a>00406 lastRefresh = currentTime;
<a name="l00407"></a>00407 <a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8" title="Refresh the display.">refresh</a>();
<a name="l00408"></a>00408 }
<a name="l00409"></a>00409 }
<a name="l00410"></a>00410
<a name="l00411"></a>00411 <span class="comment">// Send a single byte via SPI.</span>
<a name="l00412"></a>00412 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> spiSend(byte value)
<a name="l00413"></a>00413 {
<a name="l00414"></a>00414 SPDR = value;
<a name="l00415"></a>00415 <span class="keywordflow">while</span> (!(SPSR &amp; _BV(SPIF)))
<a name="l00416"></a>00416 ; <span class="comment">// Wait for the transfer to complete.</span>
<a name="l00417"></a>00417 }
<a name="l00418"></a>00418
<a name="l00419"></a>00419 <span class="comment">// Flip the bits in a byte. Table generated by genflip.c</span>
<a name="l00420"></a>00420 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t flipBits[256] PROGMEM = {
<a name="l00421"></a>00421 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0,
<a name="l00422"></a>00422 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
<a name="l00423"></a>00423 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4,
<a name="l00424"></a>00424 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
<a name="l00425"></a>00425 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC,
<a name="l00426"></a>00426 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
<a name="l00427"></a>00427 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA,
<a name="l00428"></a>00428 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
<a name="l00429"></a>00429 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6,
<a name="l00430"></a>00430 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
<a name="l00431"></a>00431 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1,
<a name="l00432"></a>00432 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
<a name="l00433"></a>00433 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9,
<a name="l00434"></a>00434 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
<a name="l00435"></a>00435 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD,
<a name="l00436"></a>00436 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
<a name="l00437"></a>00437 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3,
<a name="l00438"></a>00438 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
<a name="l00439"></a>00439 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7,
<a name="l00440"></a>00440 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
<a name="l00441"></a>00441 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF,
<a name="l00442"></a>00442 0x3F, 0xBF, 0x7F, 0xFF
<a name="l00443"></a>00443 };
<a name="l00444"></a>00444
<a name="l00460"></a><a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8">00460</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8" title="Refresh the display.">DMD::refresh</a>()
<a name="l00461"></a>00461 {
<a name="l00462"></a>00462 <span class="comment">// Bail out if there is a conflict on the SPI bus.</span>
<a name="l00463"></a>00463 <span class="keywordflow">if</span> (!digitalRead(DMD_PIN_SPI_SS))
<a name="l00464"></a>00464 <span class="keywordflow">return</span>;
<a name="l00465"></a>00465
<a name="l00466"></a>00466 <span class="comment">// Transfer the data for the next group of interleaved rows.</span>
<a name="l00467"></a>00467 <span class="keywordtype">int</span> stride4 = _stride * 4;
<a name="l00468"></a>00468 uint8_t *data0;
<a name="l00469"></a>00469 uint8_t *data1;
<a name="l00470"></a>00470 uint8_t *data2;
<a name="l00471"></a>00471 uint8_t *data3;
<a name="l00472"></a>00472 <span class="keywordtype">bool</span> flipRow = ((_height &amp; 0x10) == 0);
<a name="l00473"></a>00473 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> y = 0; y &lt; _height; y += 16) {
<a name="l00474"></a>00474 <span class="keywordflow">if</span> (!flipRow) {
<a name="l00475"></a>00475 <span class="comment">// The panels in this row are the right way up.</span>
<a name="l00476"></a>00476 data0 = displayfb + _stride * (y + phase);
<a name="l00477"></a>00477 data1 = data0 + stride4;
<a name="l00478"></a>00478 data2 = data1 + stride4;
<a name="l00479"></a>00479 data3 = data2 + stride4;
<a name="l00480"></a>00480 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> x = _stride; x &gt; 0; --x) {
<a name="l00481"></a>00481 spiSend(*data3++);
<a name="l00482"></a>00482 spiSend(*data2++);
<a name="l00483"></a>00483 spiSend(*data1++);
<a name="l00484"></a>00484 spiSend(*data0++);
<a name="l00485"></a>00485 }
<a name="l00486"></a>00486 flipRow = <span class="keyword">true</span>;
<a name="l00487"></a>00487 } <span class="keywordflow">else</span> {
<a name="l00488"></a>00488 <span class="comment">// The panels in this row are upside-down and reversed.</span>
<a name="l00489"></a>00489 data0 = displayfb + _stride * (y + 16 - phase) - 1;
<a name="l00490"></a>00490 data1 = data0 - stride4;
<a name="l00491"></a>00491 data2 = data1 - stride4;
<a name="l00492"></a>00492 data3 = data2 - stride4;
<a name="l00493"></a>00493 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> x = _stride; x &gt; 0; --x) {
<a name="l00494"></a>00494 spiSend(pgm_read_byte(&amp;(flipBits[*data3--])));
<a name="l00495"></a>00495 spiSend(pgm_read_byte(&amp;(flipBits[*data2--])));
<a name="l00496"></a>00496 spiSend(pgm_read_byte(&amp;(flipBits[*data1--])));
<a name="l00497"></a>00497 spiSend(pgm_read_byte(&amp;(flipBits[*data0--])));
<a name="l00498"></a>00498 }
<a name="l00499"></a>00499 flipRow = <span class="keyword">false</span>;
<a name="l00500"></a>00500 }
<a name="l00501"></a>00501 }
<a name="l00502"></a>00502
<a name="l00503"></a>00503 <span class="comment">// Latch the data from the shift registers onto the actual display.</span>
<a name="l00504"></a>00504 digitalWrite(DMD_PIN_OUTPUT_ENABLE, LOW);
<a name="l00505"></a>00505 digitalWrite(DMD_PIN_LATCH, HIGH);
<a name="l00506"></a>00506 digitalWrite(DMD_PIN_LATCH, LOW);
<a name="l00507"></a>00507 <span class="keywordflow">if</span> (phase &amp; 0x02)
<a name="l00508"></a>00508 digitalWrite(DMD_PIN_PHASE_MSB, HIGH);
<a name="l00509"></a>00509 <span class="keywordflow">else</span>
<a name="l00510"></a>00510 digitalWrite(DMD_PIN_PHASE_MSB, LOW);
<a name="l00511"></a>00511 <span class="keywordflow">if</span> (phase &amp; 0x01)
<a name="l00512"></a>00512 digitalWrite(DMD_PIN_PHASE_LSB, HIGH);
<a name="l00513"></a>00513 <span class="keywordflow">else</span>
<a name="l00514"></a>00514 digitalWrite(DMD_PIN_PHASE_LSB, LOW);
<a name="l00515"></a>00515 digitalWrite(DMD_PIN_OUTPUT_ENABLE, HIGH);
<a name="l00516"></a>00516 phase = (phase + 1) &amp; 0x03;
<a name="l00517"></a>00517 }
<a name="l00518"></a>00518
<a name="l00545"></a><a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2">00545</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2" title="Enables Timer1 overflow interrupts for updating this display.">DMD::enableTimer1</a>()
<a name="l00546"></a>00546 {
<a name="l00547"></a>00547 <span class="comment">// Number of CPU cycles in the display&#39;s refresh period.</span>
<a name="l00548"></a>00548 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> numCycles = (F_CPU / 2000000) * DMD_REFRESH_US;
<a name="l00549"></a>00549
<a name="l00550"></a>00550 <span class="comment">// Determine the prescaler to be used.</span>
<a name="l00551"></a>00551 <span class="preprocessor"> #define TIMER1_RESOLUTION 65536UL</span>
<a name="l00552"></a>00552 <span class="preprocessor"></span> uint8_t prescaler;
<a name="l00553"></a>00553 <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION) {
<a name="l00554"></a>00554 <span class="comment">// No prescaling required.</span>
<a name="l00555"></a>00555 prescaler = _BV(CS10);
<a name="l00556"></a>00556 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 8) {
<a name="l00557"></a>00557 <span class="comment">// Prescaler = 8.</span>
<a name="l00558"></a>00558 prescaler = _BV(CS11);
<a name="l00559"></a>00559 numCycles &gt;&gt;= 3;
<a name="l00560"></a>00560 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 64) {
<a name="l00561"></a>00561 <span class="comment">// Prescaler = 64.</span>
<a name="l00562"></a>00562 prescaler = _BV(CS11) | _BV(CS10);
<a name="l00563"></a>00563 numCycles &gt;&gt;= 6;
<a name="l00564"></a>00564 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 256) {
<a name="l00565"></a>00565 <span class="comment">// Prescaler = 256.</span>
<a name="l00566"></a>00566 prescaler = _BV(CS12);
<a name="l00567"></a>00567 numCycles &gt;&gt;= 8;
<a name="l00568"></a>00568 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 1024) {
<a name="l00569"></a>00569 <span class="comment">// Prescaler = 1024.</span>
<a name="l00570"></a>00570 prescaler = _BV(CS12) | _BV(CS10);
<a name="l00571"></a>00571 numCycles &gt;&gt;= 10;
<a name="l00572"></a>00572 } <span class="keywordflow">else</span> {
<a name="l00573"></a>00573 <span class="comment">// Too long, so set the maximum timeout.</span>
<a name="l00574"></a>00574 prescaler = _BV(CS12) | _BV(CS10);
<a name="l00575"></a>00575 numCycles = TIMER1_RESOLUTION - 1;
<a name="l00576"></a>00576 }
<a name="l00577"></a>00577
<a name="l00578"></a>00578 <span class="comment">// Configure Timer1 for the period we want.</span>
<a name="l00579"></a>00579 TCCR1A = 0;
<a name="l00580"></a>00580 TCCR1B = _BV(WGM13);
<a name="l00581"></a>00581 uint8_t saveSREG = SREG;
<a name="l00582"></a>00582 cli();
<a name="l00583"></a>00583 ICR1 = numCycles;
<a name="l00584"></a>00584 SREG = saveSREG; <span class="comment">// Implicit sei() if interrupts were on previously.</span>
<a name="l00585"></a>00585 TCCR1B = (TCCR1B &amp; ~(_BV(CS12) | _BV(CS11) | _BV(CS10))) | prescaler;
<a name="l00586"></a>00586
<a name="l00587"></a>00587 <span class="comment">// Turn on the Timer1 overflow interrupt.</span>
<a name="l00588"></a>00588 TIMSK1 |= _BV(TOIE1);
<a name="l00589"></a>00589 }
<a name="l00590"></a>00590
<a name="l00596"></a><a class="code" href="classDMD.html#a39af27e216f654ecc7e60b0614cb6b33">00596</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a39af27e216f654ecc7e60b0614cb6b33" title="Disables Timer1 overflow interrupts.">DMD::disableTimer1</a>()
<a name="l00597"></a>00597 {
<a name="l00598"></a>00598 <span class="comment">// Turn off the Timer1 overflow interrupt.</span>
<a name="l00599"></a>00599 TIMSK1 &amp;= ~_BV(TOIE1);
<a name="l00600"></a>00600 }
<a name="l00601"></a>00601
<a name="l00612"></a><a class="code" href="classDMD.html#a557412f734fc4596e1102bf71e110ea0">00612</a> <a class="code" href="classBitmap.html#a88d386944a7017aa776a177b10d8b2ba" title="Type that represents the color of a pixel in a bitmap.">DMD::Color</a> <a class="code" href="classDMD.html#a557412f734fc4596e1102bf71e110ea0" title="Converts an RGB value into a pixel color value.">DMD::fromRGB</a>(uint8_t r, uint8_t g, uint8_t b)
<a name="l00613"></a>00613 {
<a name="l00614"></a>00614 <span class="keywordflow">if</span> (r || g || b)
<a name="l00615"></a>00615 <span class="keywordflow">return</span> <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to &quot;white&quot;. If the bitmap is displayed on a LED array, then it may have a d...">White</a>;
<a name="l00616"></a>00616 <span class="keywordflow">else</span>
<a name="l00617"></a>00617 <span class="keywordflow">return</span> <a class="code" href="classBitmap.html#a2c7faeeb89d3624b5bbca58871785adc" title="Color value corresponding to &quot;black&quot;.">Black</a>;
<a name="l00210"></a>00210 <span class="comment">// Pins on the DMD connector board.</span>
<a name="l00211"></a>00211 <span class="preprocessor">#define DMD_PIN_PHASE_LSB 6 // A</span>
<a name="l00212"></a>00212 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_PHASE_MSB 7 // B</span>
<a name="l00213"></a>00213 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_LATCH 8 // SCLK</span>
<a name="l00214"></a>00214 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_OUTPUT_ENABLE 9 // nOE</span>
<a name="l00215"></a>00215 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_SS SS // SPI Slave Select</span>
<a name="l00216"></a>00216 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_MOSI MOSI // SPI Master Out, Slave In (R)</span>
<a name="l00217"></a>00217 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_MISO MISO // SPI Master In, Slave Out</span>
<a name="l00218"></a>00218 <span class="preprocessor"></span><span class="preprocessor">#define DMD_PIN_SPI_SCK SCK // SPI Serial Clock (CLK)</span>
<a name="l00219"></a>00219 <span class="preprocessor"></span>
<a name="l00220"></a>00220 <span class="comment">// Dimension information for the display.</span>
<a name="l00221"></a>00221 <span class="preprocessor">#define DMD_NUM_COLUMNS 32 // Number of columns in a panel.</span>
<a name="l00222"></a>00222 <span class="preprocessor"></span><span class="preprocessor">#define DMD_NUM_ROWS 16 // Number of rows in a panel.</span>
<a name="l00223"></a>00223 <span class="preprocessor"></span>
<a name="l00224"></a>00224 <span class="comment">// Refresh times.</span>
<a name="l00225"></a>00225 <span class="preprocessor">#define DMD_REFRESH_MS 5</span>
<a name="l00226"></a>00226 <span class="preprocessor"></span><span class="preprocessor">#define DMD_REFRESH_US 5000</span>
<a name="l00227"></a>00227 <span class="preprocessor"></span>
<a name="l00237"></a><a class="code" href="classDMD.html#affd37accffe951c8878434dfa1245809">00237</a> <a class="code" href="classDMD.html#affd37accffe951c8878434dfa1245809" title="Constructs a new dot matrix display handler for a display that is widthPanels x heightPanels in size...">DMD::DMD</a>(<span class="keywordtype">int</span> widthPanels, <span class="keywordtype">int</span> heightPanels)
<a name="l00238"></a>00238 : <a class="code" href="classBitmap.html" title="Represents a monochrome bitmap within main memory.">Bitmap</a>(widthPanels * DMD_NUM_COLUMNS, heightPanels * DMD_NUM_ROWS)
<a name="l00239"></a>00239 , _doubleBuffer(false)
<a name="l00240"></a>00240 , phase(0)
<a name="l00241"></a>00241 , fb0(0)
<a name="l00242"></a>00242 , fb1(0)
<a name="l00243"></a>00243 , displayfb(0)
<a name="l00244"></a>00244 , lastRefresh(millis())
<a name="l00245"></a>00245 {
<a name="l00246"></a>00246 <span class="comment">// Both rendering and display are to fb0 initially.</span>
<a name="l00247"></a>00247 fb0 = displayfb = fb;
<a name="l00248"></a>00248
<a name="l00249"></a>00249 <span class="comment">// Initialize SPI to MSB-first, mode 0, clock divider = 2.</span>
<a name="l00250"></a>00250 pinMode(DMD_PIN_SPI_SCK, OUTPUT);
<a name="l00251"></a>00251 pinMode(DMD_PIN_SPI_MOSI, OUTPUT);
<a name="l00252"></a>00252 pinMode(DMD_PIN_SPI_SS, OUTPUT);
<a name="l00253"></a>00253 digitalWrite(DMD_PIN_SPI_SCK, LOW);
<a name="l00254"></a>00254 digitalWrite(DMD_PIN_SPI_MOSI, LOW);
<a name="l00255"></a>00255 digitalWrite(DMD_PIN_SPI_SS, HIGH);
<a name="l00256"></a>00256 SPCR |= _BV(MSTR);
<a name="l00257"></a>00257 SPCR |= _BV(SPE);
<a name="l00258"></a>00258 SPCR &amp;= ~(_BV(DORD)); <span class="comment">// MSB-first</span>
<a name="l00259"></a>00259 SPCR &amp;= ~0x0C; <span class="comment">// Mode 0</span>
<a name="l00260"></a>00260 SPCR &amp;= ~0x03; <span class="comment">// Clock divider rate 2</span>
<a name="l00261"></a>00261 SPSR |= 0x01; <span class="comment">// MSB of clock divider rate</span>
<a name="l00262"></a>00262
<a name="l00263"></a>00263 <span class="comment">// Initialize the DMD-specific pins.</span>
<a name="l00264"></a>00264 pinMode(DMD_PIN_PHASE_LSB, OUTPUT);
<a name="l00265"></a>00265 pinMode(DMD_PIN_PHASE_MSB, OUTPUT);
<a name="l00266"></a>00266 pinMode(DMD_PIN_LATCH, OUTPUT);
<a name="l00267"></a>00267 pinMode(DMD_PIN_OUTPUT_ENABLE, OUTPUT);
<a name="l00268"></a>00268 digitalWrite(DMD_PIN_PHASE_LSB, LOW);
<a name="l00269"></a>00269 digitalWrite(DMD_PIN_PHASE_MSB, LOW);
<a name="l00270"></a>00270 digitalWrite(DMD_PIN_LATCH, LOW);
<a name="l00271"></a>00271 digitalWrite(DMD_PIN_OUTPUT_ENABLE, LOW);
<a name="l00272"></a>00272 digitalWrite(DMD_PIN_SPI_MOSI, HIGH);
<a name="l00273"></a>00273 }
<a name="l00274"></a>00274
<a name="l00278"></a><a class="code" href="classDMD.html#a7b37e05584d3e0308163700920df99b2">00278</a> <a class="code" href="classDMD.html#a7b37e05584d3e0308163700920df99b2" title="Destroys this dot matrix display handler.">DMD::~DMD</a>()
<a name="l00279"></a>00279 {
<a name="l00280"></a>00280 <span class="keywordflow">if</span> (fb0)
<a name="l00281"></a>00281 free(fb0);
<a name="l00282"></a>00282 <span class="keywordflow">if</span> (fb1)
<a name="l00283"></a>00283 free(fb1);
<a name="l00284"></a>00284 fb = 0; <span class="comment">// Don&#39;t free the buffer again in the base class.</span>
<a name="l00285"></a>00285 }
<a name="l00286"></a>00286
<a name="l00314"></a><a class="code" href="classDMD.html#a6fbdcf8832f91d02500cb7a9b84d2723">00314</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a6fbdcf8832f91d02500cb7a9b84d2723" title="Enables or disables double-buffering according to doubleBuffer.">DMD::setDoubleBuffer</a>(<span class="keywordtype">bool</span> doubleBuffer)
<a name="l00315"></a>00315 {
<a name="l00316"></a>00316 <span class="keywordflow">if</span> (doubleBuffer != _doubleBuffer) {
<a name="l00317"></a>00317 _doubleBuffer = <a class="code" href="classDMD.html#aab1f3ba29c053d630ae12865d22166ec" title="Returns true if the display is double-buffered; false if single-buffered. The default is false...">doubleBuffer</a>;
<a name="l00318"></a>00318 <span class="keywordflow">if</span> (doubleBuffer) {
<a name="l00319"></a>00319 <span class="comment">// Allocate a new back buffer.</span>
<a name="l00320"></a>00320 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size = _stride * _height;
<a name="l00321"></a>00321 fb1 = (uint8_t *)malloc(size);
<a name="l00322"></a>00322
<a name="l00323"></a>00323 <span class="comment">// Clear the new back buffer and then switch to it, leaving</span>
<a name="l00324"></a>00324 <span class="comment">// the current contents of fb0 on the screen.</span>
<a name="l00325"></a>00325 <span class="keywordflow">if</span> (fb1) {
<a name="l00326"></a>00326 memset(fb1, 0xFF, size);
<a name="l00327"></a>00327 cli();
<a name="l00328"></a>00328 fb = fb1;
<a name="l00329"></a>00329 displayfb = fb0;
<a name="l00330"></a>00330 sei();
<a name="l00331"></a>00331 } <span class="keywordflow">else</span> {
<a name="l00332"></a>00332 <span class="comment">// Failed to allocate the memory, so revert to single-buffered.</span>
<a name="l00333"></a>00333 _doubleBuffer = <span class="keyword">false</span>;
<a name="l00334"></a>00334 }
<a name="l00335"></a>00335 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fb1) {
<a name="l00336"></a>00336 <span class="comment">// Disabling double-buffering, so forcibly switch to fb0.</span>
<a name="l00337"></a>00337 cli();
<a name="l00338"></a>00338 fb = fb0;
<a name="l00339"></a>00339 displayfb = fb0;
<a name="l00340"></a>00340 sei();
<a name="l00341"></a>00341
<a name="l00342"></a>00342 <span class="comment">// Free the unnecessary buffer.</span>
<a name="l00343"></a>00343 free(fb1);
<a name="l00344"></a>00344 fb1 = 0;
<a name="l00345"></a>00345 }
<a name="l00346"></a>00346 }
<a name="l00347"></a>00347 }
<a name="l00348"></a>00348
<a name="l00363"></a><a class="code" href="classDMD.html#a80269ccd44b3ef9ee15f0a1009b7a60a">00363</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a80269ccd44b3ef9ee15f0a1009b7a60a" title="Swaps the buffers that are used for rendering to the display.">DMD::swapBuffers</a>()
<a name="l00364"></a>00364 {
<a name="l00365"></a>00365 <span class="keywordflow">if</span> (_doubleBuffer) {
<a name="l00366"></a>00366 <span class="comment">// Turn off interrupts while swapping buffers so that we don&#39;t</span>
<a name="l00367"></a>00367 <span class="comment">// accidentally try to refresh() in the middle of this code.</span>
<a name="l00368"></a>00368 cli();
<a name="l00369"></a>00369 <span class="keywordflow">if</span> (fb == fb0) {
<a name="l00370"></a>00370 fb = fb1;
<a name="l00371"></a>00371 displayfb = fb0;
<a name="l00372"></a>00372 } <span class="keywordflow">else</span> {
<a name="l00373"></a>00373 fb = fb0;
<a name="l00374"></a>00374 displayfb = fb1;
<a name="l00375"></a>00375 }
<a name="l00376"></a>00376 sei();
<a name="l00377"></a>00377 }
<a name="l00378"></a>00378 }
<a name="l00379"></a>00379
<a name="l00396"></a><a class="code" href="classDMD.html#a0b1771cf790b2b62eea55e56b02e3736">00396</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a0b1771cf790b2b62eea55e56b02e3736" title="Swaps the buffers that are used for rendering to the display and copies the former back buffer conten...">DMD::swapBuffersAndCopy</a>()
<a name="l00397"></a>00397 {
<a name="l00398"></a>00398 <a class="code" href="classDMD.html#a80269ccd44b3ef9ee15f0a1009b7a60a" title="Swaps the buffers that are used for rendering to the display.">swapBuffers</a>();
<a name="l00399"></a>00399 <span class="keywordflow">if</span> (_doubleBuffer)
<a name="l00400"></a>00400 memcpy(fb, displayfb, _stride * _height);
<a name="l00401"></a>00401 }
<a name="l00402"></a>00402
<a name="l00420"></a><a class="code" href="classDMD.html#a2c74a0845ef6080056b972d490648114">00420</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a2c74a0845ef6080056b972d490648114" title="Performs regular display refresh activities from the application&#39;s main loop.">DMD::loop</a>()
<a name="l00421"></a>00421 {
<a name="l00422"></a>00422 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> currentTime = millis();
<a name="l00423"></a>00423 <span class="keywordflow">if</span> ((currentTime - lastRefresh) &gt;= DMD_REFRESH_MS) {
<a name="l00424"></a>00424 lastRefresh = currentTime;
<a name="l00425"></a>00425 <a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8" title="Refresh the display.">refresh</a>();
<a name="l00426"></a>00426 }
<a name="l00427"></a>00427 }
<a name="l00428"></a>00428
<a name="l00429"></a>00429 <span class="comment">// Send a single byte via SPI.</span>
<a name="l00430"></a>00430 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> spiSend(byte value)
<a name="l00431"></a>00431 {
<a name="l00432"></a>00432 SPDR = value;
<a name="l00433"></a>00433 <span class="keywordflow">while</span> (!(SPSR &amp; _BV(SPIF)))
<a name="l00434"></a>00434 ; <span class="comment">// Wait for the transfer to complete.</span>
<a name="l00435"></a>00435 }
<a name="l00436"></a>00436
<a name="l00437"></a>00437 <span class="comment">// Flip the bits in a byte. Table generated by genflip.c</span>
<a name="l00438"></a>00438 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t flipBits[256] PROGMEM = {
<a name="l00439"></a>00439 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0,
<a name="l00440"></a>00440 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
<a name="l00441"></a>00441 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4,
<a name="l00442"></a>00442 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
<a name="l00443"></a>00443 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC,
<a name="l00444"></a>00444 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
<a name="l00445"></a>00445 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA,
<a name="l00446"></a>00446 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
<a name="l00447"></a>00447 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6,
<a name="l00448"></a>00448 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
<a name="l00449"></a>00449 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1,
<a name="l00450"></a>00450 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
<a name="l00451"></a>00451 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9,
<a name="l00452"></a>00452 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
<a name="l00453"></a>00453 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD,
<a name="l00454"></a>00454 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
<a name="l00455"></a>00455 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3,
<a name="l00456"></a>00456 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
<a name="l00457"></a>00457 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7,
<a name="l00458"></a>00458 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
<a name="l00459"></a>00459 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF,
<a name="l00460"></a>00460 0x3F, 0xBF, 0x7F, 0xFF
<a name="l00461"></a>00461 };
<a name="l00462"></a>00462
<a name="l00478"></a><a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8">00478</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a9e4bf2a9d247312d35c1401ff61261c8" title="Refresh the display.">DMD::refresh</a>()
<a name="l00479"></a>00479 {
<a name="l00480"></a>00480 <span class="comment">// Bail out if there is a conflict on the SPI bus.</span>
<a name="l00481"></a>00481 <span class="keywordflow">if</span> (!digitalRead(DMD_PIN_SPI_SS))
<a name="l00482"></a>00482 <span class="keywordflow">return</span>;
<a name="l00483"></a>00483
<a name="l00484"></a>00484 <span class="comment">// Transfer the data for the next group of interleaved rows.</span>
<a name="l00485"></a>00485 <span class="keywordtype">int</span> stride4 = _stride * 4;
<a name="l00486"></a>00486 uint8_t *data0;
<a name="l00487"></a>00487 uint8_t *data1;
<a name="l00488"></a>00488 uint8_t *data2;
<a name="l00489"></a>00489 uint8_t *data3;
<a name="l00490"></a>00490 <span class="keywordtype">bool</span> flipRow = ((_height &amp; 0x10) == 0);
<a name="l00491"></a>00491 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> y = 0; y &lt; _height; y += 16) {
<a name="l00492"></a>00492 <span class="keywordflow">if</span> (!flipRow) {
<a name="l00493"></a>00493 <span class="comment">// The panels in this row are the right way up.</span>
<a name="l00494"></a>00494 data0 = displayfb + _stride * (y + phase);
<a name="l00495"></a>00495 data1 = data0 + stride4;
<a name="l00496"></a>00496 data2 = data1 + stride4;
<a name="l00497"></a>00497 data3 = data2 + stride4;
<a name="l00498"></a>00498 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> x = _stride; x &gt; 0; --x) {
<a name="l00499"></a>00499 spiSend(*data3++);
<a name="l00500"></a>00500 spiSend(*data2++);
<a name="l00501"></a>00501 spiSend(*data1++);
<a name="l00502"></a>00502 spiSend(*data0++);
<a name="l00503"></a>00503 }
<a name="l00504"></a>00504 flipRow = <span class="keyword">true</span>;
<a name="l00505"></a>00505 } <span class="keywordflow">else</span> {
<a name="l00506"></a>00506 <span class="comment">// The panels in this row are upside-down and reversed.</span>
<a name="l00507"></a>00507 data0 = displayfb + _stride * (y + 16 - phase) - 1;
<a name="l00508"></a>00508 data1 = data0 - stride4;
<a name="l00509"></a>00509 data2 = data1 - stride4;
<a name="l00510"></a>00510 data3 = data2 - stride4;
<a name="l00511"></a>00511 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> x = _stride; x &gt; 0; --x) {
<a name="l00512"></a>00512 spiSend(pgm_read_byte(&amp;(flipBits[*data3--])));
<a name="l00513"></a>00513 spiSend(pgm_read_byte(&amp;(flipBits[*data2--])));
<a name="l00514"></a>00514 spiSend(pgm_read_byte(&amp;(flipBits[*data1--])));
<a name="l00515"></a>00515 spiSend(pgm_read_byte(&amp;(flipBits[*data0--])));
<a name="l00516"></a>00516 }
<a name="l00517"></a>00517 flipRow = <span class="keyword">false</span>;
<a name="l00518"></a>00518 }
<a name="l00519"></a>00519 }
<a name="l00520"></a>00520
<a name="l00521"></a>00521 <span class="comment">// Latch the data from the shift registers onto the actual display.</span>
<a name="l00522"></a>00522 digitalWrite(DMD_PIN_OUTPUT_ENABLE, LOW);
<a name="l00523"></a>00523 digitalWrite(DMD_PIN_LATCH, HIGH);
<a name="l00524"></a>00524 digitalWrite(DMD_PIN_LATCH, LOW);
<a name="l00525"></a>00525 <span class="keywordflow">if</span> (phase &amp; 0x02)
<a name="l00526"></a>00526 digitalWrite(DMD_PIN_PHASE_MSB, HIGH);
<a name="l00527"></a>00527 <span class="keywordflow">else</span>
<a name="l00528"></a>00528 digitalWrite(DMD_PIN_PHASE_MSB, LOW);
<a name="l00529"></a>00529 <span class="keywordflow">if</span> (phase &amp; 0x01)
<a name="l00530"></a>00530 digitalWrite(DMD_PIN_PHASE_LSB, HIGH);
<a name="l00531"></a>00531 <span class="keywordflow">else</span>
<a name="l00532"></a>00532 digitalWrite(DMD_PIN_PHASE_LSB, LOW);
<a name="l00533"></a>00533 digitalWrite(DMD_PIN_OUTPUT_ENABLE, HIGH);
<a name="l00534"></a>00534 phase = (phase + 1) &amp; 0x03;
<a name="l00535"></a>00535 }
<a name="l00536"></a>00536
<a name="l00563"></a><a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2">00563</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a4c3b04b384f3d656a9b59690836775e2" title="Enables Timer1 overflow interrupts for updating this display.">DMD::enableTimer1</a>()
<a name="l00564"></a>00564 {
<a name="l00565"></a>00565 <span class="comment">// Number of CPU cycles in the display&#39;s refresh period.</span>
<a name="l00566"></a>00566 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> numCycles = (F_CPU / 2000000) * DMD_REFRESH_US;
<a name="l00567"></a>00567
<a name="l00568"></a>00568 <span class="comment">// Determine the prescaler to be used.</span>
<a name="l00569"></a>00569 <span class="preprocessor"> #define TIMER1_RESOLUTION 65536UL</span>
<a name="l00570"></a>00570 <span class="preprocessor"></span> uint8_t prescaler;
<a name="l00571"></a>00571 <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION) {
<a name="l00572"></a>00572 <span class="comment">// No prescaling required.</span>
<a name="l00573"></a>00573 prescaler = _BV(CS10);
<a name="l00574"></a>00574 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 8) {
<a name="l00575"></a>00575 <span class="comment">// Prescaler = 8.</span>
<a name="l00576"></a>00576 prescaler = _BV(CS11);
<a name="l00577"></a>00577 numCycles &gt;&gt;= 3;
<a name="l00578"></a>00578 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 64) {
<a name="l00579"></a>00579 <span class="comment">// Prescaler = 64.</span>
<a name="l00580"></a>00580 prescaler = _BV(CS11) | _BV(CS10);
<a name="l00581"></a>00581 numCycles &gt;&gt;= 6;
<a name="l00582"></a>00582 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 256) {
<a name="l00583"></a>00583 <span class="comment">// Prescaler = 256.</span>
<a name="l00584"></a>00584 prescaler = _BV(CS12);
<a name="l00585"></a>00585 numCycles &gt;&gt;= 8;
<a name="l00586"></a>00586 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (numCycles &lt; TIMER1_RESOLUTION * 1024) {
<a name="l00587"></a>00587 <span class="comment">// Prescaler = 1024.</span>
<a name="l00588"></a>00588 prescaler = _BV(CS12) | _BV(CS10);
<a name="l00589"></a>00589 numCycles &gt;&gt;= 10;
<a name="l00590"></a>00590 } <span class="keywordflow">else</span> {
<a name="l00591"></a>00591 <span class="comment">// Too long, so set the maximum timeout.</span>
<a name="l00592"></a>00592 prescaler = _BV(CS12) | _BV(CS10);
<a name="l00593"></a>00593 numCycles = TIMER1_RESOLUTION - 1;
<a name="l00594"></a>00594 }
<a name="l00595"></a>00595
<a name="l00596"></a>00596 <span class="comment">// Configure Timer1 for the period we want.</span>
<a name="l00597"></a>00597 TCCR1A = 0;
<a name="l00598"></a>00598 TCCR1B = _BV(WGM13);
<a name="l00599"></a>00599 uint8_t saveSREG = SREG;
<a name="l00600"></a>00600 cli();
<a name="l00601"></a>00601 ICR1 = numCycles;
<a name="l00602"></a>00602 SREG = saveSREG; <span class="comment">// Implicit sei() if interrupts were on previously.</span>
<a name="l00603"></a>00603 TCCR1B = (TCCR1B &amp; ~(_BV(CS12) | _BV(CS11) | _BV(CS10))) | prescaler;
<a name="l00604"></a>00604
<a name="l00605"></a>00605 <span class="comment">// Turn on the Timer1 overflow interrupt.</span>
<a name="l00606"></a>00606 TIMSK1 |= _BV(TOIE1);
<a name="l00607"></a>00607 }
<a name="l00608"></a>00608
<a name="l00614"></a><a class="code" href="classDMD.html#a39af27e216f654ecc7e60b0614cb6b33">00614</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a39af27e216f654ecc7e60b0614cb6b33" title="Disables Timer1 overflow interrupts.">DMD::disableTimer1</a>()
<a name="l00615"></a>00615 {
<a name="l00616"></a>00616 <span class="comment">// Turn off the Timer1 overflow interrupt.</span>
<a name="l00617"></a>00617 TIMSK1 &amp;= ~_BV(TOIE1);
<a name="l00618"></a>00618 }
<a name="l00619"></a>00619
<a name="l00646"></a><a class="code" href="classDMD.html#a5469775db7fafebca2cdbc6a6372fb97">00646</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a5469775db7fafebca2cdbc6a6372fb97" title="Enables Timer2 overflow interrupts for updating this display.">DMD::enableTimer2</a>()
<a name="l00647"></a>00647 {
<a name="l00648"></a>00648 <span class="comment">// Configure Timer2 for the period we want. With the prescaler set</span>
<a name="l00649"></a>00649 <span class="comment">// to 128, then 256 increments of Timer2 gives roughly 4 ms between</span>
<a name="l00650"></a>00650 <span class="comment">// overflows on a system with a 16 MHz clock. We adjust the prescaler</span>
<a name="l00651"></a>00651 <span class="comment">// accordingly for other clock frequencies.</span>
<a name="l00652"></a>00652 TCCR2A = 0;
<a name="l00653"></a>00653 <span class="keywordflow">if</span> (F_CPU &gt;= 32000000)
<a name="l00654"></a>00654 TCCR2B = _BV(CS22) | _BV(CS21); <span class="comment">// Prescaler = 256</span>
<a name="l00655"></a>00655 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (F_CPU &gt;= 16000000)
<a name="l00656"></a>00656 TCCR2B = _BV(CS22) | _BV(CS20); <span class="comment">// Prescaler = 128</span>
<a name="l00657"></a>00657 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (F_CPU &gt;= 8000000)
<a name="l00658"></a>00658 TCCR2B = _BV(CS22); <span class="comment">// Prescaler = 64</span>
<a name="l00659"></a>00659 <span class="keywordflow">else</span>
<a name="l00660"></a>00660 TCCR2B = _BV(CS21) | _BV(CS20); <span class="comment">// Prescaler = 32</span>
<a name="l00661"></a>00661
<a name="l00662"></a>00662 <span class="comment">// Reset Timer2 to kick off the process.</span>
<a name="l00663"></a>00663 TCNT2 = 0;
<a name="l00664"></a>00664
<a name="l00665"></a>00665 <span class="comment">// Turn on the Timer2 overflow interrupt (also turn off OCIE2A and OCIE2B).</span>
<a name="l00666"></a>00666 TIMSK2 = _BV(TOIE2);
<a name="l00667"></a>00667 }
<a name="l00668"></a>00668
<a name="l00674"></a><a class="code" href="classDMD.html#a52fe885bfb380b74df54c96221811cff">00674</a> <span class="keywordtype">void</span> <a class="code" href="classDMD.html#a52fe885bfb380b74df54c96221811cff" title="Disables Timer2 overflow interrupts.">DMD::disableTimer2</a>()
<a name="l00675"></a>00675 {
<a name="l00676"></a>00676 <span class="comment">// Turn off the Timer2 overflow interrupt.</span>
<a name="l00677"></a>00677 TIMSK2 &amp;= ~_BV(TOIE2);
<a name="l00678"></a>00678 }
<a name="l00679"></a>00679
<a name="l00690"></a><a class="code" href="classDMD.html#a557412f734fc4596e1102bf71e110ea0">00690</a> <a class="code" href="classBitmap.html#a88d386944a7017aa776a177b10d8b2ba" title="Type that represents the color of a pixel in a bitmap.">DMD::Color</a> <a class="code" href="classDMD.html#a557412f734fc4596e1102bf71e110ea0" title="Converts an RGB value into a pixel color value.">DMD::fromRGB</a>(uint8_t r, uint8_t g, uint8_t b)
<a name="l00691"></a>00691 {
<a name="l00692"></a>00692 <span class="keywordflow">if</span> (r || g || b)
<a name="l00693"></a>00693 <span class="keywordflow">return</span> <a class="code" href="classBitmap.html#a39b6754cfe50a457bbfdb1980fd87eb7" title="Color value corresponding to &quot;white&quot;. If the bitmap is displayed on a LED array, then it may have a d...">White</a>;
<a name="l00694"></a>00694 <span class="keywordflow">else</span>
<a name="l00695"></a>00695 <span class="keywordflow">return</span> <a class="code" href="classBitmap.html#a2c7faeeb89d3624b5bbca58871785adc" title="Color value corresponding to &quot;black&quot;.">Black</a>;
<a name="l00696"></a>00696 }
</pre></div></div>
</div>
<!-- window showing the filter options -->
@@ -395,7 +424,7 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search');
</iframe>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Tue May 29 2012 15:23:58 for ArduinoLibs by&#160;
<hr class="footer"/><address class="footer"><small>Generated on Wed May 30 2012 15:44:33 for ArduinoLibs by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>