<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <title type="text">Mika Tuupola</title>
  <generator uri="http://effectif.com/nesta">Nesta</generator>
  <id>tag:www.appelsiini.net,2009:/</id>
  
  <link href="http://www.appelsiini.net" rel="alternate" />
  <subtitle type="html">Technology guy with passion for advertising.</subtitle>
  <updated>2012-01-28T23:30:00+02:00</updated>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/tuupola" /><feedburner:info uri="tuupola" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>tuupola</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2Ftuupola" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Ftuupola" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2Ftuupola" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/tuupola" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Ftuupola" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Ftuupola" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Ftuupola" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><entry>
    <title>Lazy Load 1.7.0 Released</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/QxWs0lngeHw/lazyload-170" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2012-01-28:/2012/lazyload-170</id>
    <content type="html">
            &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/bradypus-2.jpg" alt="" /&gt;&lt;/p&gt;
            &lt;blockquote&gt;Photo by &lt;a href="http://www.flickr.com/photos/marisecaetano/"&gt;Marise Caetano&lt;/a&gt;&lt;/blockquote&gt;
            &lt;p&gt;Previous version of &lt;a href="http://www.appelsiini.net/projects/lazyload"&gt;Lazy Load&lt;/a&gt; gained traction pretty fast. Good patches were submitted to GitHub. This version of plugin mostly concentrates on speed optimization and event handlers.&lt;/p&gt;
            &lt;h2&gt;New Events&lt;/h2&gt;
            &lt;p&gt;Two new events were added. Handler for &lt;code&gt;appear&lt;/code&gt; event is called when image appears to viewport but before it is loaded.  Handler for &lt;code&gt;load&lt;/code&gt; event is called when image is loaded. Both event handler receive two parameters. First parameter &lt;code&gt;elements_left&lt;/code&gt; is numbers of images left to load. Second parameter &lt;code&gt;settings&lt;/code&gt; is the settings passed to Lazy Load plugin. Inside both handlers &lt;code&gt;this&lt;/code&gt; refers to the image dom element.&lt;/p&gt;
            &lt;pre class="prettyprint"&gt;$("img.lazy").lazyload({&amp;#x000A;    appear : function(elements_left, settings) {&amp;#x000A;        console.log(this, elements_left, settings);&amp;#x000A;    },&amp;#x000A;    load : function(elements_left, settings) {&amp;#x000A;        console.log(this, elements_left, settings);&amp;#x000A;    }&amp;#x000A;});&lt;/pre&gt;&lt;h2&gt;New Parameter&lt;/h2&gt;
            &lt;p&gt;New parameter &lt;code&gt;data_attribute&lt;/code&gt; was added. It allows custom naming of original image attribute.&lt;/p&gt;
            &lt;pre class="prettyprint"&gt;$("img.lazy").lazyload({&amp;#x000A;  data_attribute  : "kitten"&amp;#x000A;});&lt;/pre&gt;
            
            &lt;pre class="prettyprint"&gt;&amp;lt;img src="/img/placeholder.gif" data-kitten="/img/real-image.png" width="640" height="480" /&amp;gt;&lt;/pre&gt;
            &lt;h2&gt;Renamed Parameter&lt;/h2&gt;
            &lt;p&gt;Parameter &lt;code&gt;effecspeed&lt;/code&gt; was renamed to &lt;code&gt;effect_speed&lt;/code&gt;. Old version will work for couple of versions. This parameter has existed before but it was previously undocumented.&lt;/p&gt;
            &lt;h2&gt;Selectors&lt;/h2&gt;
            &lt;p&gt;Viewport selectors got tuned up. Internally they are used to determine when image appears on screen. Speed up is around 25%. You can compare speed tests of &lt;a href="http://jsperf.com/lazyload-1-6-0"&gt;1.6.0&lt;/a&gt; and &lt;a href="http://jsperf.com/lazyload-1-7-0"&gt;1.7.0&lt;/a&gt;. While you&amp;#8217;re at there click the &lt;strong&gt;Run tests&lt;/strong&gt; button to help me collect better data. Some selector names were added and changed to match &lt;a href="http://www.appelsiini.net/projects/viewport"&gt;Viewport Selectors plugin&lt;/a&gt;.&lt;/p&gt;
            &lt;pre class="prettyprint"&gt;$("img:in-viewport").something();&amp;#x000A;$("img:below-the-fold").something();&amp;#x000A;$("img:above-the-top").something();&amp;#x000A;$("img:left-of-screen").something();&amp;#x000A;$("img:right-of-screen").something();&lt;/pre&gt;
            &lt;h2&gt;Download&lt;/h2&gt;
            &lt;p&gt;Latest &lt;a href="https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.js"&gt;source&lt;/a&gt; or &lt;a href="https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.min.js"&gt;minified&lt;/a&gt;. Plugin has been tested with Safari 5.1, Firefox 3.6, Firefox 7.0, Firefox 8.0 on &lt;span class="caps"&gt;OSX&lt;/span&gt; and Firefox 3.0, Chrome 14 and IE 8 on Windows and Safari 5.1 on iOS 5 both iPhone and iPad.&lt;/p&gt;
          &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=QxWs0lngeHw:7SEsS_rm1og:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=QxWs0lngeHw:7SEsS_rm1og:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/QxWs0lngeHw" height="1" width="1"/&gt;</content>
    <published>2012-01-28T23:30:00+02:00</published>
    <updated>2012-01-28T23:30:00+02:00</updated>
    <category term="javascript" />
    <category term="jquery" />
    <category term="lazyload" />
  <feedburner:origLink>http://www.appelsiini.net/2012/lazyload-170</feedburner:origLink></entry>
  <entry>
    <title>Driving 595 Shift Registers</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/PCC875wTOuQ/driving-595-shift-registers" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2012-01-02:/2012/driving-595-shift-registers</id>
    <content type="html">
              &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/atmega32u4.jpg" alt="" /&gt;&lt;/p&gt;
              &lt;blockquote&gt;Photo by &lt;a href="http://www.flickr.com/photos/oskay/"&gt;Windell Oskay&lt;/a&gt;&lt;/blockquote&gt;
              &lt;p&gt;595 series shift registers come in many flavors. SN74HC595 is the most usual. &lt;a href="http://www.adafruit.com/products/457"&gt;TPIC6B595&lt;/a&gt; is similar but can be used with more power hungry applications. Pin layouts are different but they all operate in the same way.&lt;/p&gt;
              &lt;p&gt;Shift register is controlled with three pins. They are usually called &lt;code&gt;DATA&lt;/code&gt;, &lt;code&gt;LATCH&lt;/code&gt; and &lt;code&gt; CLOCK&lt;/code&gt;. Chip manufacturers have different names. See the table below for two examples from Texas Instruments.&lt;/p&gt;
              &lt;table class="bordered-table zebra-striped"&gt;
              &lt;thead&gt;
              &lt;tr&gt;
                    &lt;th&gt;&lt;/th&gt;
                    &lt;th&gt;74HC595&lt;/th&gt;
                    &lt;th&gt;TPIC6B595&lt;/th&gt;
              &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;tr&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;DATA&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SER&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SER&lt;/span&gt; IN&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;LATCH&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;RCLK&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;RCK&lt;/span&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;CLOCK&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SRCLK&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SRCK&lt;/span&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;/table&gt;
              &lt;p&gt;&lt;code&gt;CLOCK&lt;/code&gt; is an constant high &amp;#8211; low signal used to synchronize data transfer. Each time &lt;code&gt;CLOCK&lt;/code&gt; goes high two things happen. Current value in shift register gets shifted left by one. Last bit is dropped out. First bit will be set to current value of &lt;code&gt;DATA&lt;/code&gt;. To write a byte to shift register this has to happen eight times in a loop.&lt;/p&gt;
              &lt;pre class="prettyprint"&gt;&amp;#x000A;#define LATCH   B0  &amp;#x000A;#define CLOCK   B1&amp;#x000A;#define DATA    B2&amp;#x000A;  &amp;#x000A;void shift_out(uint8_t data) {&amp;#x000A;    for(uint8_t i = 0; i &amp;lt; 8; i++) {&amp;#x000A;        /* Write bit to data port. */&amp;#x000A;        if (0 == (data &amp;amp; _BV(7 - i))) {&amp;#x000A;            digital_write(DATA, LOW);            &amp;#x000A;        } else {&amp;#x000A;            digital_write(DATA, HIGH);&amp;#x000A;        }&amp;#x000A;        &amp;#x000A;        /* Pulse clock input to write next bit. */&amp;#x000A;        digital_write(CLOCK, LOW);&amp;#x000A;        digital_write(CLOCK, HIGH);&amp;#x000A;    }&amp;#x000A;}&lt;/pre&gt;
              &lt;p&gt;After calling &lt;code&gt;shift_out()&lt;/code&gt; the shift register internally contains new value. To update output pins you must pull &lt;code&gt;LATCH&lt;/code&gt; high. This is sometimes called latching on pulsing the latch. Note that it is not enough to hold &lt;code&gt;LATCH&lt;/code&gt; high. Data transfer happens on transition from low to high. This is also called rising edge. Code for a &lt;a href="http://vimeo.com/34472869"&gt;led binary counter&lt;/a&gt; from 0 to 65535 would look like the following:&lt;/p&gt;
              &lt;pre class="prettyprint"&gt;&amp;#x000A;int main(void) { &amp;#x000A;  &amp;#x000A;    for(uint16_t i = 0; i &amp;lt; 0xffff; i++) {&amp;#x000A;        /* Shift high byte first to shift registers. */&amp;#x000A;        shift_out(i &amp;gt;&amp;gt; 8); &amp;#x000A;        shift_out(i &amp;amp; 0xff);&amp;#x000A;&amp;#x000A;        /* Pulse latch to transfer data from shift registers */&amp;#x000A;        /* to storage registers. */&amp;#x000A;        digital_write(LATCH, LOW); &amp;#x000A;        digital_write(LATCH, HIGH);&amp;#x000A;&amp;#x000A;        _delay_ms(50);&amp;#x000A;    }&amp;#x000A;&amp;#x000A;    return 0;&amp;#x000A;}&lt;/pre&gt;
              &lt;p&gt;The technique above is called bit banging. Bit banging is a technique for serial communications using software instead of dedicated hardware. Good thing is it is cheap to implement. Bad thing is it wastes processing time. Luckily most &lt;span class="caps"&gt;AVR&lt;/span&gt; chips provide an alternative.&lt;/p&gt;
              &lt;h2&gt;Serial Peripheral Interface Bus&lt;/h2&gt;
              &lt;p&gt;The Serial Peripheral Interface Bus or &lt;span class="caps"&gt;SPI&lt;/span&gt; is a synchronous serial data connection. It provides hardware implementation of the clock pulse and writing data serially. &lt;span class="caps"&gt;SPI&lt;/span&gt; terminology differs from bit banging a bit. You can use &lt;span class="caps"&gt;SPI&lt;/span&gt; with three pins only. If you also want to read value back from the slave device you must use fourth pin. This is what &lt;code&gt;MISO&lt;/code&gt; is used for.&lt;/p&gt;
              &lt;table class="bordered-table zebra-striped"&gt;
              &lt;thead&gt;
              &lt;tr&gt;
                    &lt;th&gt;Bit Bang&lt;/th&gt;
                    &lt;th&gt;&lt;span class="caps"&gt;SPI&lt;/span&gt;&lt;/th&gt;
                    &lt;th&gt;Description&lt;/th&gt;
              &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;tr&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;DATA&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;MOSI&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;Master Out Slave In&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;LATCH&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;SS&lt;/td&gt;
                    &lt;td&gt;Slave Select&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;CLOCK&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SCLK&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;Serial Clock&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;-&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;MISO&lt;/span&gt;&lt;/td&gt;
                    &lt;td&gt;Master In Slave Out&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;/table&gt;
              &lt;p&gt;Before you can use &lt;span class="caps"&gt;SPI&lt;/span&gt; it must be configured. Main things to do are setting the device to work as master and the data order. Most of the time this is enough. Configuration is done by setting the appropriate bit in the &lt;span class="caps"&gt;SPI&lt;/span&gt; Control Register &lt;code&gt;SPCR&lt;/code&gt;. For all configurable options see the &lt;a href="#spcr"&gt;Control and Status Registers&lt;/a&gt; table. When using &lt;span class="caps"&gt;SPI&lt;/span&gt; you cannot use any arbitrary pins. Instead read your datasheet to find out which pins your microcontroller uses for &lt;span class="caps"&gt;SPI&lt;/span&gt;. Values below are for ATmega32U4 which is used in both &lt;a href="http://www.adafruit.com/products/296"&gt;Adafruit ATmega32U4 Breakout Board&lt;/a&gt; and &lt;a href="http://www.pjrc.com/teensy/"&gt;Teensy &lt;span class="caps"&gt;USB&lt;/span&gt; Development Board&lt;/a&gt;.&lt;/p&gt;
              &lt;pre class="prettyprint"&gt;&amp;#x000A;#define SS   B0 &amp;#x000A;#define SCLK B1 &amp;#x000A;#define MOSI B2 &amp;#x000A;#define MISO B3&amp;#x000A;&amp;#x000A;void spi_init(void) {&amp;#x000A;    pin_mode(SCLK, OUTPUT);&amp;#x000A;    pin_mode(MOSI, OUTPUT);&amp;#x000A;    pin_mode(SS, OUTPUT); /* Should be output in Master mode. */&amp;#x000A;    &amp;#x000A;    SPCR &amp;amp;= ~(_BV(DORD)); /* MSB first. */&amp;#x000A;    SPCR |= _BV(MSTR);    /* Act as master. */&amp;#x000A;    SPCR |= _BV(SPE);     /* Enable SPI. */&amp;#x000A;}&lt;/pre&gt;
              &lt;p&gt;After &lt;span class="caps"&gt;SPI&lt;/span&gt; is configured reading and writing to it is easy. When byte is written to &lt;span class="caps"&gt;SPI&lt;/span&gt; Data Register &lt;code&gt;SPDR&lt;/code&gt; it will be transmitted to slave. Received bytes (if any) are written to hardware receive buffer. Reading &lt;code&gt;SPDR&lt;/code&gt; will return the data in receive buffer.&lt;/p&gt;
              &lt;pre class="prettyprint"&gt;&amp;#x000A;uint8_t spi_transfer(volatile uint8_t data) {&amp;#x000A;    SPDR = data;&amp;#x000A;    loop_until_bit_is_set(SPSR, SPIF);&amp;#x000A;    return SPDR;&amp;#x000A;}&lt;/pre&gt;
              &lt;p&gt;Why &lt;code&gt;spi_transfer()&lt;/code&gt; and not separate &lt;code&gt;spi_read()&lt;/code&gt; and &lt;code&gt;spi_write()&lt;/code&gt; functions? Internally both master and slave are 8-bit shift registers (do not confuse this with the TPIC6B595 shift register used in article). One bit is shifted from the master to the slave and from the slave to the master simultaneously in one serial clock cycle. After eight &lt;code&gt;SLCK&lt;/code&gt; pulses data has been exchanged between master and slave. Slave never sends data to master by itself. Master always must write something to slave.&lt;/p&gt;
              &lt;p&gt;Main program is essentially the same as when using &lt;code&gt;shift_out()&lt;/code&gt; function. Since we do not read anything back we can just ignore return value of &lt;code&gt;spi_transfer()&lt;/code&gt; call.&lt;/p&gt;
              &lt;pre class="prettyprint"&gt;&amp;#x000A;int main(void) {    &amp;#x000A;  &amp;#x000A;    spi_init();&amp;#x000A;&amp;#x000A;    for(uint16_t i = 0; i &amp;lt; 0xffff; i++) {&amp;#x000A;        /* Shift high byte first to shift registers. */&amp;#x000A;        spi_transfer(i &amp;gt;&amp;gt; 8); &amp;#x000A;        spi_transfer(i &amp;amp; 0xff);&amp;#x000A;&amp;#x000A;        /* Pulse latch to transfer data from shift registers */&amp;#x000A;        /* to storage registers. */&amp;#x000A;        digital_write(SPI_SS, LOW); &amp;#x000A;        digital_write(SPI_SS, HIGH);&amp;#x000A;    }&amp;#x000A;&amp;#x000A;    return 0;&amp;#x000A;}&lt;/pre&gt;
              &lt;p&gt;&lt;a name="spcr"&gt;&lt;/a&gt;&lt;/p&gt;
              &lt;h2&gt;Control and Status Registers&lt;/h2&gt;
              &lt;table class="bordered-table zebra-striped"&gt;
              &lt;thead&gt;
              &lt;tr&gt;
                    &lt;th&gt;&lt;span class="caps"&gt;SPCR&lt;/span&gt; Bit #&lt;/th&gt;
                    &lt;th&gt;Name&lt;/th&gt;
                    &lt;th&gt;Description&lt;/th&gt;
              &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 7&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;SPIE&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SPI&lt;/span&gt; Interrupt Enable.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 6&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;SPE&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SPI&lt;/span&gt; Enable. When set hardware &lt;span class="caps"&gt;SPI&lt;/span&gt; operations will be enabled.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 5&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;DORD&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;Data Order. When set &lt;span class="caps"&gt;LSB&lt;/span&gt; will be transmitted first.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 4&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;MSTR&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;Master / Slave Select. When set device will act as &lt;span class="caps"&gt;SPI&lt;/span&gt; master.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 3&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;CPOL&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;Clock Polarity. When set leading edge will be falling and trailing edge rising. Vice versa when unset.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 2&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;CPHA&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;Clock Phase. When set data will be sampled on trailing edge of the clock. Vice versa when unset.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 1&lt;br&gt;bit 0&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;SPR1&lt;/code&gt;&lt;br&gt;&lt;code&gt;SPR0&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SPI&lt;/span&gt; Clock Rate Select 1 and 0. Used together with &lt;code&gt;SPI2X&lt;/code&gt; to set the &lt;code&gt;SCK&lt;/code&gt; frequency.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;/table&gt;
              &lt;table class="bordered-table zebra-striped"&gt;
              &lt;thead&gt;
              &lt;tr&gt;
                    &lt;th&gt;&lt;span class="caps"&gt;SPSR&lt;/span&gt; Bit #&lt;/th&gt;
                    &lt;th&gt;Name&lt;/th&gt;
                    &lt;th&gt;Description&lt;/th&gt;
              &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 7&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;SPIF&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;span class="caps"&gt;SPI&lt;/span&gt; Interrupt Flag. Set when a serial transfer is complete. An interrupt is   generated if &lt;code&gt;SPIE&lt;/code&gt; in &lt;code&gt;SPCR&lt;/code&gt; is set and global interrupts are enabled. Cleared by hardware when interrupt handling vector is executed.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 6&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;WCOL&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;Write Collision Flag. Set if the &lt;code&gt;SPDR&lt;/code&gt; is written during a data transfer.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 5&lt;br&gt;bit 4&lt;br&gt;bit 3&lt;br&gt;bit 2&lt;br&gt;bit 1&lt;br&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;/td&gt;
                    &lt;td&gt;Reserved. Always zero.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;bit 0&lt;/td&gt;
                    &lt;td&gt;&lt;code&gt;SPI2X&lt;/code&gt;&lt;/td&gt;
                    &lt;td&gt;Double &lt;span class="caps"&gt;SPI&lt;/span&gt; Speed. Used together with &lt;code&gt;SPR1&lt;/code&gt; and &lt;code&gt;SPR0&lt;/code&gt; to set the &lt;code&gt;SCK&lt;/code&gt; frequency.&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;/table&gt;
              &lt;table class="bordered-table zebra-striped"&gt;
              &lt;thead&gt;
              &lt;tr&gt;
                    &lt;th&gt;SPI2X&lt;/th&gt;
                    &lt;th&gt;SPR1&lt;/th&gt;
                    &lt;th&gt;SPR0&lt;/th&gt;
                    &lt;th&gt;&lt;span class="caps"&gt;SCK&lt;/span&gt; Frequency&lt;/th&gt;
              &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;tr&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/4&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/16&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/64&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/128&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/2&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/8&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;0&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/32&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;1&lt;/td&gt;
                    &lt;td&gt;f&lt;sub&gt;osc&lt;/sub&gt;/64&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;/table&gt;
              &lt;h2&gt;Wiring TPIC6B595&lt;/h2&gt;
              &lt;p&gt;Wiring is pretty straight forward but depends on the chip used. &lt;a href="http://www.adafruit.com/products/457"&gt;TPIC6B595 from Adafruit&lt;/a&gt; was used when writing this article.&lt;/p&gt;
              &lt;p&gt;All shift registers should share &lt;code&gt;CLOCK&lt;/code&gt; and &lt;code&gt;LATCH&lt;/code&gt; signals. When cascading more than one shift register &lt;code&gt;DATA&lt;/code&gt; is redirected to next register by connecting &lt;code&gt;SER OUT&lt;/code&gt; to &lt;code&gt;SER IN&lt;/code&gt;. Outputs are connected to corresponding leds. To enable outputs of TPIC6B595 you must also tie &lt;code&gt;SRCLR&lt;/code&gt; pin to positive voltage and &lt;code&gt;G&lt;/code&gt; pin to ground.&lt;/p&gt;
              &lt;p&gt;In image below &lt;code&gt;CLOCK&lt;/code&gt; is blue, &lt;code&gt;LATCH&lt;/code&gt; is yellow and &lt;code&gt;DATA&lt;/code&gt; is orange.&lt;/p&gt;
              &lt;p&gt;&lt;img src="/attachments/tpic6b595.png" alt="" /&gt;&lt;/p&gt;
              &lt;h2&gt;More Reading&lt;/h2&gt;
              &lt;p&gt;&lt;a href="https://github.com/tuupola/avr_demo/tree/master/blog/driving_595"&gt;Full source code&lt;/a&gt; of this article. &lt;a href="http://www.protostack.com/blog/2010/05/introduction-to-74hc595-shift-register-controlling-16-leds/"&gt;Introduction to 74HC595 Shift Register&lt;/a&gt; by ProtoStack. &lt;a href="http://bildr.org/2011/02/74hc595/"&gt;The 74HC595 8 bit shift register&lt;/a&gt; by DrLuke. &lt;a href="http://atmel.com/dyn/resources/prod_documents/doc2585.pdf"&gt;Setup And Use of The &lt;span class="caps"&gt;SPI&lt;/span&gt;&lt;/a&gt; technote by Atmel. &lt;a href="http://avrbeginners.net/architecture/spi/spi.html" title="SPI"&gt;The Serial Peripheral Interface&lt;/a&gt; from &lt;span class="caps"&gt;AVR&lt;/span&gt; beginners. &lt;a href="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus"&gt;Serial Peripheral Interface Bus&lt;/a&gt; article in Wikipedia.&lt;/p&gt;
            &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=PCC875wTOuQ:LrPHARhG5Hs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=PCC875wTOuQ:LrPHARhG5Hs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/PCC875wTOuQ" height="1" width="1"/&gt;</content>
    <published>2012-01-02T21:00:00+02:00</published>
    <updated>2012-01-02T21:00:00+02:00</updated>
    <category term="avr" />
    <category term="electronics" />
  <feedburner:origLink>http://www.appelsiini.net/2012/driving-595-shift-registers</feedburner:origLink></entry>
  <entry>
    <title>Lazy Load 1.6.0 Released</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/fW1wNu9VGjU/lazyload-160" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-12-27:/2011/lazyload-160</id>
    <content type="html">
                &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/bradypus.jpg?v=2" alt="" /&gt;&lt;/p&gt;
                &lt;blockquote&gt;Photo by &lt;a href="http://www.flickr.com/photos/gjofili/"&gt;Guilherme Jófili&lt;/a&gt;&lt;/blockquote&gt;
                &lt;p&gt;After neglecting the code for a while I was finally able to release new version of &lt;a href="http://www.appelsiini.net/projects/lazyload"&gt;Lazy Load plugin&lt;/a&gt;. Changes are not dramatic but there is one important thing. New browsers load image even if you remove the src attribute with JavaScript. Because of this you must alter your html code. Put placeholder image into src attribute of your img tag. Real image url should be stored &lt;code&gt;data-original&lt;/code&gt; attribute.&lt;/p&gt;
                &lt;pre class="prettyprint"&gt;&amp;lt;img data-original=“img/example.jpg” src=“img/grey.gif”&amp;gt;&lt;/pre&gt;
                &lt;p&gt;All code regarding the old behavior where you could just include plugin code in your page and it would automagically lazyload your images was removed.&lt;/p&gt;&lt;h2&gt;Ignore Hidden Images&lt;/h2&gt;
                &lt;p&gt;There are cases when you have invisible images. For example when using plugin in together with a large filterable list of items that can have their visibility state changed dynamically. To improve performance hidden images are now ignored by default. If you want to load hidden images set the new &lt;code&gt;skip_invisible&lt;/code&gt; parameter to false.&lt;/p&gt;
                &lt;pre class="prettyprint"&gt;$("img.lazy").lazyload({ &amp;#x000A;    skip_invisible : false&amp;#x000A;});&lt;/pre&gt;
                &lt;h2&gt;New Scrollstop Event&lt;/h2&gt;
                &lt;p&gt;Support for James Padolseys &lt;a href="http://james.padolsey.com/javascript/special-scroll-events-for-jquery/"&gt;scrollstop event&lt;/a&gt; was added. Use it when you have hundreds of images on page. Check the &lt;a href="http://www.appelsiini.net/projects/lazyload/enabled_gazillion.html"&gt;demo page&lt;/a&gt; to see it in action. I have extracted the code from James&amp;#8217; webpage and put it into &lt;a href=""&gt;separate file&lt;/a&gt;.&lt;/p&gt;
                &lt;pre class="prettyprint"&gt;&amp;lt;script src="jquery.scrollstop.js" type="text/javascript" charset="utf-8"&amp;gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;
                &lt;pre class="prettyprint"&gt;$("img").lazyload({&amp;#x000A;    event: "scrollstop"&amp;#x000A;});&lt;/pre&gt;
                &lt;h2&gt;Renamed Parameter&lt;/h2&gt;
                &lt;p&gt;Parameter &lt;code&gt;failurelimit&lt;/code&gt; was renamed to &lt;code&gt;failure_limit&lt;/code&gt;. Old version will work for couple of versions. I cannot seem to be able to decide on my own coding standards&amp;#8230;&lt;/p&gt;
                &lt;h2&gt;Download&lt;/h2&gt;
                &lt;p&gt;Latest &lt;a href="https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.js"&gt;source&lt;/a&gt; or &lt;a href="https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.min.js"&gt;minified&lt;/a&gt;. Plugin has been tested with Safari 5.1, Firefox 3.6, Firefox 7.0, Firefox 8.0 on &lt;span class="caps"&gt;OSX&lt;/span&gt; and Firefox 3.0, Chrome 14 and IE 8 on Windows and Safari 5.1 on iOS 5 both iPhone and iPad.&lt;/p&gt;
              &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=fW1wNu9VGjU:_ySoq8hFszg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=fW1wNu9VGjU:_ySoq8hFszg:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/fW1wNu9VGjU" height="1" width="1"/&gt;</content>
    <published>2011-12-27T02:00:00+02:00</published>
    <updated>2011-12-27T02:00:00+02:00</updated>
    <category term="javascript" />
    <category term="jquery" />
    <category term="lazyload" />
  <feedburner:origLink>http://www.appelsiini.net/2011/lazyload-160</feedburner:origLink></entry>
  <entry>
    <title>Simple Serial Communications With AVR Libc</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/tEitUHdyT_E/simple-usart-with-avr-libc" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-11-19:/2011/simple-usart-with-avr-libc</id>
    <content type="html">
                  &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/atmega32u4.jpg" alt="" /&gt;&lt;/p&gt;
                  &lt;blockquote&gt;Photo by &lt;a href="http://rasterweb.net/raster/"&gt;Pete Prodoehl&lt;/a&gt;&lt;/blockquote&gt;
                  &lt;p&gt;I like to use various Arduino boards for &lt;span class="caps"&gt;AVR&lt;/span&gt; development. What I do not like are the Arduino libraries. They are often just &lt;a href="https://github.com/arduino/Arduino/tree/master/libraries/EEPROM"&gt;wrappers around libc functions&lt;/a&gt; or &lt;a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Print.cpp"&gt;rewrites of functions libc already provides&lt;/a&gt;. Serial communications is one good example. Arduino provides you with its own implementation of &lt;code&gt;Serial.print()&lt;/code&gt;, &lt;code&gt;Serial.println()&lt;/code&gt; and &lt;code&gt;Serial.read()&lt;/code&gt; methods. At the same time &lt;span class="caps"&gt;AVR&lt;/span&gt; Libc has proven &lt;code&gt;printf()&lt;/code&gt;, &lt;code&gt;puts()&lt;/code&gt; and &lt;code&gt;getchar()&lt;/code&gt; functions. This article explains easy implementation of libc functions used for serial communications.&lt;/p&gt;
                  &lt;div class="alert-message block-message warning"&gt;
                  &lt;strong&gt;If you do not have much experience in programming it is probably better to stick with Arduino libraries.&lt;/strong&gt; They are good at hiding some of the confusing features of embedded programming. However changes are you grow out of them after few projects. Atmel datasheets are not as confusing as they first appear. You might also want to check the &lt;a href="https://github.com/tuupola/avr_demo/tree/master/blog/simple_usart"&gt;finished code&lt;/a&gt; of this article.
                  &lt;/div&gt;
                  &lt;h2&gt;Configuring &lt;span class="caps"&gt;UART&lt;/span&gt;&lt;/h2&gt;
                  &lt;p&gt;&lt;span class="caps"&gt;AVR&lt;/span&gt; microcontrollers have three control and status registers. Register &lt;code&gt;UCSR0A&lt;/code&gt; mostly contains status data. &lt;code&gt;UCSR0B&lt;/code&gt; and &lt;code&gt;UCSR0C&lt;/code&gt; contain all the configuration settings. See the &lt;a href="#registers"&gt;tables in the end of article&lt;/a&gt;  for all possible values.&lt;/p&gt;
                  &lt;p&gt;&lt;span class="caps"&gt;AVR&lt;/span&gt; Libc provides &lt;a href="http://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html"&gt;helper macros for baud rate calculations&lt;/a&gt;. Header file requires &lt;code&gt;F_CPU&lt;/code&gt; and &lt;code&gt;BAUD&lt;/code&gt; to be defined. After including the header file &lt;code&gt;UBRRL_VALUE&lt;/code&gt;, &lt;code&gt;UBRRH_VALUE&lt;/code&gt; and  &lt;code&gt;USE_2X&lt;/code&gt; are defined. First two are used to set &lt;span class="caps"&gt;UART&lt;/span&gt; speed. Last one is used to determine if &lt;span class="caps"&gt;UART&lt;/span&gt; has to be configured to run in double speed mode with given baud rate.&lt;/p&gt;
                  &lt;p&gt;&lt;code&gt;UCSZ20&lt;/code&gt; &lt;code&gt;UCSZ01&lt;/code&gt; and &lt;code&gt;UCSZ00&lt;/code&gt; control the data size. Possible sizes are 5-bit (000), 6-bit (001), 7-bit (010), 8-bit (011) and 9-bit (111). Most common used data size is 8-bit.&lt;/p&gt;
                  &lt;p&gt;With above bits we can set most common configuration: no parity, 8 data bits, 1 stop bit.&lt;/p&gt;
                  
                  &lt;pre class="prettyprint"&gt;&amp;#x000A;#define F_CPU 16000000UL&amp;#x000A;#define BAUD 9600&amp;#x000A;&amp;#x000A;#include &amp;lt;util/setbaud.h&amp;gt;&amp;#x000A;  &amp;#x000A;void uart_init(void) {&amp;#x000A;    UBRR0H = UBRRH_VALUE;&amp;#x000A;    UBRR0L = UBRRL_VALUE;&amp;#x000A;&amp;#x000A;#if USE_2X&amp;#x000A;    UCSR0A |= _BV(U2X0);&amp;#x000A;#else&amp;#x000A;    UCSR0A &amp;amp;= ~(_BV(U2X0));&amp;#x000A;#endif&amp;#x000A;&amp;#x000A;    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ &amp;#x000A;    UCSR0B = _BV(RXEN0) | _BV(TXEN0);   /* Enable RX and TX */&amp;#x000A;}&lt;/pre&gt;
                  &lt;h2&gt;Writing and Reading From &lt;span class="caps"&gt;UART&lt;/span&gt;&lt;/h2&gt;
                  &lt;p&gt;You can transmit data to &lt;span class="caps"&gt;UART&lt;/span&gt; by writing a byte to &lt;span class="caps"&gt;USART&lt;/span&gt; Data Register &lt;code&gt;UDR0&lt;/code&gt;.  First you have to make sure &lt;span class="caps"&gt;UART&lt;/span&gt; is ready to transmit new data. You can wait until &lt;span class="caps"&gt;USART&lt;/span&gt; Data Register Empty &lt;code&gt;UDRE&lt;/code&gt; flag is set. Alternatively you can wait after each byte to transmission be ready. &lt;span class="caps"&gt;USART&lt;/span&gt; Transmit Complete &lt;code&gt;TXC0&lt;/code&gt; is set when transmission is ready.&lt;/p&gt;
                  &lt;pre class="prettyprint"&gt;void uart_putchar(char c) {&amp;#x000A;    loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */&amp;#x000A;    UDR0 = c;&amp;#x000A;}&lt;/pre&gt;
                  
                  &lt;pre class="prettyprint"&gt;void uart_putchar(char c) {&amp;#x000A;    UDR0 = c;&amp;#x000A;    loop_until_bit_is_set(UCSR0A, TXC0); /* Wait until transmission ready. */&amp;#x000A;}&lt;/pre&gt;
                  &lt;p&gt;You can receive data from &lt;span class="caps"&gt;UART&lt;/span&gt; by reading a byte from &lt;span class="caps"&gt;USART&lt;/span&gt; Data Register &lt;code&gt;UDR0&lt;/code&gt;. &lt;span class="caps"&gt;USART&lt;/span&gt; Receive Complete &lt;code&gt;RXC0&lt;/code&gt; flag is set if to unread data exists in data register.&lt;/p&gt;
                  &lt;pre class="prettyprint"&gt;char uart_getchar(void) {&amp;#x000A;    loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */&amp;#x000A;    return UDR0;&amp;#x000A;}&lt;/pre&gt;
                  &lt;h2&gt;Redirecting &lt;span class="caps"&gt;STDIN&lt;/span&gt; and &lt;span class="caps"&gt;STDOUT&lt;/span&gt; to &lt;span class="caps"&gt;UART&lt;/span&gt;&lt;/h2&gt;
                  &lt;p&gt;&lt;a href="http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html"&gt;FDEV_SETUP_STREAM&lt;/a&gt; macro can be used to setup a buffer which is valid for stdio operations. Initialized buffer will be of type &lt;code&gt;FILE&lt;/code&gt;. You can define separate buffers for input and output. Alternatively you can define only one buffer which works for both input and output. First and second parameters are names of the functions which will be called when data is either read from or written to the buffer.&lt;/p&gt;
                  &lt;pre class="prettyprint"&gt;FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);&amp;#x000A;FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);&lt;/pre&gt;
                  
                  &lt;pre class="prettyprint"&gt;FILE uart_io FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);&lt;/pre&gt;
                  &lt;p&gt;To prepare our &lt;code&gt;uart_putchar&lt;/code&gt; and &lt;code&gt;uart_getchar&lt;/code&gt; function to be used with streams we have to change the definition a bit. To properly format output we also force adding a carriage return after newline has been sent.&lt;/p&gt;
                  &lt;pre class="prettyprint"&gt;void uart_putchar(char c, FILE *stream) {&amp;#x000A;    if (c == '\n') {&amp;#x000A;        uart_putchar('\r', stream);&amp;#x000A;    }&amp;#x000A;    loop_until_bit_is_set(UCSR0A, UDRE0);&amp;#x000A;    UDR0 = c;&amp;#x000A;}&lt;/pre&gt;
                  
                  &lt;pre class="prettyprint"&gt;char uart_getchar(FILE *stream) {&amp;#x000A;    loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */&amp;#x000A;    return UDR0;&amp;#x000A;}&lt;/pre&gt;
                  &lt;p&gt;Now we can redirect both &lt;span class="caps"&gt;STDIN&lt;/span&gt; and &lt;span class="caps"&gt;STDOUT&lt;/span&gt; to &lt;span class="caps"&gt;UART&lt;/span&gt;. This enables us to use &lt;span class="caps"&gt;AVR&lt;/span&gt; Libc provided functions to read and write to serial port.&lt;/p&gt;
                  &lt;pre class="prettyprint"&gt;int main(void) {    &amp;#x000A;&amp;#x000A;    uart_init();&amp;#x000A;    stdout = &amp;amp;uart_output;&amp;#x000A;    stdin  = &amp;amp;uart_input;&amp;#x000A;                &amp;#x000A;    char input;&amp;#x000A;&amp;#x000A;    while(1) {&amp;#x000A;        puts("Hello world!");&amp;#x000A;        input = getchar();&amp;#x000A;        printf("You wrote %c\n", input);&amp;#x000A;    }&amp;#x000A;        &amp;#x000A;    return 0;&amp;#x000A;}&lt;/pre&gt;
                  &lt;p&gt;&lt;a name="registers"&gt;&lt;/p&gt;
                  &lt;h2&gt;Control and Status Registers&lt;/h2&gt;
                  &lt;table class="bordered-table zebra-striped"&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
                        &lt;th&gt;UCSR0A Bit #&lt;/th&gt;
                        &lt;th&gt;Name&lt;/th&gt;
                        &lt;th&gt;Description&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 7&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;RXC0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Receive Complete. Set when data is available and the data register has not be read yet.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 6&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;TXC0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Transmit Complete. Set when all data has transmitted.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 5&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UDRE0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Data Register Empty. Set when the &lt;code&gt;UDR0&lt;/code&gt; register is empty and new data can be transmitted.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 4&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;FE0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;Frame Error. Set when next byte in the &lt;code&gt;UDR0&lt;/code&gt; register has a framing error.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 3&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;DOR0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;Data OverRun. Set when the &lt;code&gt;UDR0&lt;/code&gt; was not read before the next frame arrived.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 2&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UPE0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Parity Error. Set when next frame in the &lt;code&gt;UDR0&lt;/code&gt; has a parity error.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 1&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;U2X0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Double Transmission Speed. When set decreases the bit time by half doubling the speed.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 0&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;MPCM0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;Multi-processor Communication Mode. When set incoming data is ignored if no addressing information is provided.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/tbody&gt;
                  &lt;/table&gt;
                  &lt;table class="bordered-table zebra-striped"&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
                        &lt;th&gt;UCSR0B Bit #&lt;/th&gt;
                        &lt;th&gt;Name&lt;/th&gt;
                        &lt;th&gt;Description&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 7&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;RXCIE0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;RX Complete Interrupt Enable. Set to allow receive complete interrupts.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 6&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;TXCIE0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;TX Complete Interrupt Enable. Set to allow transmission complete interrupts.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 5&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UDRIE0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Data Register Empty Interrupt Enable. Set to allow data register empty interrupts.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 4&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;RXEN0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Receiver Enable. Set to enable receiver.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 3&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;TXEN0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Transmitter enable. Set to enable transmitter.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 2&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UCSZ20&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Character Size 0. Used together with &lt;code&gt;UCSZ01&lt;/code&gt; and &lt;code&gt;UCSZ00&lt;/code&gt; to set data frame size. Available sizes are 5-bit (000), 6-bit (001), 7-bit (010), 8-bit (011) and 9-bit (111).&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 1&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;RXB80&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;Receive Data Bit 8. When using 8 bit transmission the 8th bit received.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 0&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;TXB80&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;Transmit Data Bit 8. When using 8 bit transmission the 8th bit to be submitted.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/tbody&gt;
                  &lt;/table&gt;
                  &lt;table class="bordered-table zebra-striped"&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
                        &lt;th&gt;UCSR0C Bit #&lt;/th&gt;
                        &lt;th&gt;Name&lt;/th&gt;
                        &lt;th&gt;Description&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 7&lt;br&gt;bit 6&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UMSEL01&lt;/code&gt;&lt;br&gt;&lt;code&gt;UMSEL00&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Mode Select 1 and 0. &lt;code&gt;UMSEL01&lt;/code&gt; and &lt;code&gt;UMSEL00&lt;/code&gt; combined select the operating mode. Available modes are asynchronous (00), synchronous (01) and master &lt;span class="caps"&gt;SPI&lt;/span&gt; (11).&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 5&lt;br&gt;bit 4&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UPM01&lt;/code&gt;&lt;br&gt;&lt;code&gt;UPM00&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Parity Mode 1 and 0. &lt;code&gt;UPM01&lt;/code&gt; and &lt;code&gt;UPM00&lt;/code&gt; select the parity. Available modes are none (00), even (10) and odd (11).&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 3&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;USBS0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Stop Bit Select. Set to select 1 stop bit. Unset to select 2 stop bits.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 2&lt;br&gt;bit 1&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UCSZ01&lt;/code&gt;&lt;br&gt;&lt;code&gt;UCSZ00&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Character Size 1 and 0. Used together with with &lt;code&gt;UCSZ20&lt;/code&gt; to set data frame size. Available sizes are 5-bit (000), 6-bit (001), 7-bit (010), 8-bit (011) and 9-bit (111).&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                        &lt;td&gt;bit 0&lt;/td&gt;
                        &lt;td&gt;&lt;code&gt;UCPOL0&lt;/code&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; Clock Polarity. Set to transmit on falling edge and sample on rising edge. Unset to transmit on rising edge and sample on falling edge.&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/tbody&gt;
                  &lt;/table&gt;
                  &lt;h2&gt;More Reading&lt;/h2&gt;
                  &lt;p&gt;&lt;a href="https://github.com/tuupola/avr_demo/tree/master/blog/simple_usart"&gt;Full source code&lt;/a&gt; of this article. &lt;a href="https://sites.google.com/site/qeewiki/books/avr-guide/usart"&gt;&lt;span class="caps"&gt;USART&lt;/span&gt; entry in QEEWiki&lt;/a&gt; by Q. &lt;a href="http://www.engineersgarage.com/embedded/avr-microcontroller-projects/serial-communication-atmega16-usart"&gt;Serial Communication Using &lt;span class="caps"&gt;AVR&lt;/span&gt; Microcontroller  &lt;span class="caps"&gt;USART&lt;/span&gt;&lt;/a&gt; by Engineers Garage. &lt;a href="http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html"&gt;Serial Communication Notes&lt;/a&gt; by Rod Byrne.&lt;/p&gt;
                &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=tEitUHdyT_E:AALmtiqgI2I:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=tEitUHdyT_E:AALmtiqgI2I:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/tEitUHdyT_E" height="1" width="1"/&gt;</content>
    <published>2011-11-19T22:00:00+02:00</published>
    <updated>2011-11-19T22:00:00+02:00</updated>
    <category term="avr" />
    <category term="electronics" />
  <feedburner:origLink>http://www.appelsiini.net/2011/simple-usart-with-avr-libc</feedburner:origLink></entry>
  <entry>
    <title>How Does Led Matrix Work?</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/nuyjiGrzYI8/how-does-led-matrix-work" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-11-04:/2011/how-does-led-matrix-work</id>
    <content type="html">
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/rgbleds.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;blockquote&gt;Photo by &lt;a href="http://www.flickr.com/photos/oskay/"&gt;Windell Oskay&lt;/a&gt;&lt;/blockquote&gt;
                    &lt;p&gt;Led matrices are fun toys. Who would not love blinkenlights? Electronics is hard. Electronics is much harder than programming. I had hard time trying to understand how do the led matrices work. What is best way to learn something? Build one yourself.&lt;/p&gt;
                    &lt;h1&gt;Structure of Led Matrix&lt;/h1&gt;
                    &lt;p&gt;In a matrix format LEDs are arranged in rows and columns. You can also think of them as y and x coordinates. Lets assume we have 4&amp;#215;4 matrix. Rows would be marked from A to D and columns from 1 to 4. Now we can address each &lt;span class="caps"&gt;LED&lt;/span&gt; by row and column. Top left led would be (A,1). Bottom down led would be (D,4).&lt;/p&gt;
                    &lt;p&gt;Led matrices come in two flavors. Common-row anode (left) and common-row cathode (right).&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-1.png" alt="" /&gt;&lt;/p&gt;
                    &lt;p&gt;Figure above shows the different configurations. The difference between these two configurations is how you lit a led. With common-row anode current sources (positive voltage) are attached to rows A..D and currents sinks (negative voltage, ground) to columns 1..4. With common-row cathode current sinks are attached to rows A..D and currents sources to columns 1..4.&lt;/p&gt;
                    &lt;p&gt;For example. To light bottom down led (D,4) of common cathode matrix you would feed positive voltage to column 4 and connect row D to ground. For sake of clarity I will using common-row cathode in examples for the rest of this article.&lt;/p&gt;&lt;h2&gt;Building a &lt;span class="caps"&gt;LED&lt;/span&gt; Matrix&lt;/h2&gt;
                    &lt;p&gt;To build a 4&amp;#215;4 common-row cathode matrix you will need 16 LEDs, four resistors, some headers and prototyping board. I started by gluing the leds to prototyping board with epoxy glue. This way it is easier to have LEDs beautifully aligned. When gluing the leds make sure long and short legs are aligned the same way.&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-glue.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;p&gt;When glue is dry it is time to bend and solder. First bend all cathodes to left as close to prototyping board as possible. Solder all cathodes in each row together. When cathodes are ready, bend all anodes. Anodes must not touch cathodes. I used piece of plastic tubing to help bending the anodes to form a bridge above cathodes.&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-bend.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;p&gt;Now solder together all anodes in each row. Solder the headers and connect cathode rows directly to the header.&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-bottom.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;p&gt;Anode rows are connected to header with current limiting resistors. Value of the resistor depends on the &lt;span class="caps"&gt;LED&lt;/span&gt; used. Check the &lt;span class="caps"&gt;LED&lt;/span&gt; datasheet for forward voltage and current. &lt;a href="http://led.linear1.org/1led.wiz"&gt;&lt;span class="caps"&gt;LED&lt;/span&gt; calculator&lt;/a&gt; will help you finding out correct resistor. Matrix is now ready for testing.&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-top.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;h2&gt;Addressing Single &lt;span class="caps"&gt;LED&lt;/span&gt;&lt;/h2&gt;
                    &lt;p&gt;Connecting ground to row A and positive voltage to column 1  will light the top right &lt;span class="caps"&gt;LED&lt;/span&gt; (A,1).&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-test-1.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;p&gt;Connecting ground to row D and positive voltage to column 4 will light the bottom down &lt;span class="caps"&gt;LED&lt;/span&gt; (D,4).&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-test-2.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;p&gt;Intuition would say lighting the both (A,1) and (D,4) at the same time is just connecting all the four wires. This is not the case. There are four LEDs which are lit. This is because current is also flowing through (A,4) and (D,1).&lt;/p&gt;
                    &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/4x4-test-3.jpg" alt="" /&gt;&lt;/p&gt;
                    &lt;h2&gt;Multiplexing and Persistence of Vision&lt;/h2&gt;
                    &lt;p&gt;Multiplexing can be used to display arbitrary patterns with led matrices. Multiplexing is sometimes also called scanning. It scans rows (usually from up to down) and lights needed leds only in one row at time. Something like following:&lt;/p&gt;
                    &lt;ol&gt;
                    	&lt;li&gt;Start by having everything disconnected.&lt;/li&gt;
                    	&lt;li&gt;Connect positive voltage all the needed columns.&lt;/li&gt;
                    	&lt;li&gt;Connect row to ground. This lights the needed leds in the row.&lt;/li&gt;
                    	&lt;li&gt;Disconnect the row and all columns.&lt;/li&gt;
                    	&lt;li&gt;Do the same steps one by one to all rows and then start from the beginning.&lt;/li&gt;
                    &lt;/ol&gt;
                    &lt;p&gt;Do this slowly and you would see blinking &lt;span class="caps"&gt;LED&lt;/span&gt; rows. Do it really fast and human eye can see the whole pattern. Phenomenon is called &lt;a href="http://en.wikipedia.org/wiki/Persistence_of_vision"&gt;persistence of vision&lt;/a&gt;.&lt;/p&gt;
                    &lt;h2&gt;Draw a Pattern&lt;/h2&gt;
                    &lt;p&gt;Lets write some simple code for drawing a pattern on the matrix. Note! Even though I am using Arduino board I do not use Arduino libraries nor &lt;span class="caps"&gt;IDE&lt;/span&gt; for developing. I do however like the Arduino pin numbering scheme. Functions &lt;code&gt;pin_mode()&lt;/code&gt; and &lt;code&gt;digital_write()&lt;/code&gt; work exactly the same way as their Arduino equivalents.&lt;/p&gt;
                    &lt;p&gt;We start by setting up the pins and default state for them.&lt;/p&gt;
                    &lt;pre class="prettyprint"&gt;uint8_t column_pins[4] = { 2, 3, 4, 5 };&amp;#x000A;uint8_t row_pins[4]    = { 11, 10, 9, 8 };&amp;#x000A;&amp;#x000A;static void init(void) {&amp;#x000A;  &amp;#x000A;    /* Turn all columns off by setting then low. */&amp;#x000A;    for (uint8_t x=0; x&amp;lt;4; x++) { &amp;#x000A;        pin_mode(column_pins[x], OUTPUT);&amp;#x000A;        digital_write(column_pins[x], LOW);&amp;#x000A;    }&amp;#x000A;    &amp;#x000A;    /* Turn all rows off by setting then high. */&amp;#x000A;    for (uint8_t y=0; y&amp;lt;4; y++) { &amp;#x000A;        pin_mode(row_pins[y], OUTPUT);&amp;#x000A;        digital_write(row_pins[y], HIGH);&amp;#x000A;    }&amp;#x000A;}&lt;/pre&gt;
                    &lt;p&gt;To display a pattern on matrix we use &lt;code&gt;draw()&lt;/code&gt; function. Bitmap is passed as two dimensional array. Delay is used only to demonstrate persistence of vision.&lt;/p&gt;
                    &lt;pre class="prettyprint"&gt;uint8_t pattern[4][4]  = {{1,0,0,1}, {0,1,0,0}, {0,0,1,0}, {1,0,0,1}};&amp;#x000A;&amp;#x000A;void draw(uint8_t buffer[4][4], uint8_t delay) {&amp;#x000A;&amp;#x000A;    for (uint8_t row=0; row&amp;lt;4; ++row) {&amp;#x000A;        /* Connect or disconnect columns as needed. */&amp;#x000A;        for (uint8_t column=0; column&amp;lt;4; ++column) {&amp;#x000A;            digital_write(column_pins[column], buffer[row][column]);                    &amp;#x000A;        }&amp;#x000A;&amp;#x000A;        /* Turn on whole row. */&amp;#x000A;        digital_write(row_pins[row], LOW);&amp;#x000A;&amp;#x000A;        _delay_ms(delay);&amp;#x000A;&amp;#x000A;        /* Turn off whole row. */&amp;#x000A;        digital_write(row_pins[row], HIGH);&amp;#x000A;    }&amp;#x000A;}&lt;/pre&gt;
                    &lt;p&gt;To examine persistence of vision effect we draw the pattern with different delays.&lt;/p&gt;
                    &lt;pre class="prettyprint"&gt;uint8_t main(void) {    &amp;#x000A;    &amp;#x000A;    init();&amp;#x000A;    &amp;#x000A;    /* With 100ms delay eye can see updating row by row. */&amp;#x000A;    for (uint8_t i=0; i&amp;lt;10; i++) { &amp;#x000A;        draw(pattern, 100);&amp;#x000A;    }&amp;#x000A;&amp;#x000A;    /* With 10ms delay pattern appears but flickers. */&amp;#x000A;    for (uint16_t i=0; i&amp;lt;100; i++) { &amp;#x000A;        draw(pattern, 10);&amp;#x000A;    }&amp;#x000A;    &amp;#x000A;    /* Withoud delay solid pattern appears. */&amp;#x000A;    while (1) {&amp;#x000A;        draw(pattern, 1);        &amp;#x000A;    }&amp;#x000A;    &amp;#x000A;    return 0;&amp;#x000A;}&lt;/pre&gt;
                    &lt;p&gt;Full code can be found from &lt;a href="https://github.com/tuupola/avr_demo/tree/master/4x4_matrix_part_1"&gt;GitHub&lt;/a&gt;. Check the output from video below.&lt;/p&gt;
                    &lt;p&gt;&lt;iframe src="http://player.vimeo.com/video/31568410?title=0&amp;amp;byline=0&amp;amp;portrait=0" width="688" height="387" frameborder="0" webkitAllowFullScreen allowFullScreen&gt;&lt;/iframe&gt;&lt;/p&gt;
                    &lt;h2&gt;More Reading&lt;/h2&gt;
                    &lt;p&gt;&lt;a href="http://blog.makezine.com/archive/2011/03/circuit-skills-led-matrix-sponsored-by-jameco-electronics.html"&gt;Circuit Skills: &lt;span class="caps"&gt;LED&lt;/span&gt; Matrix&lt;/a&gt; by Make Magazine. &lt;a href="http://tinkerlog.com/2009/04/05/driving-an-led-with-or-without-a-resistor/"&gt;Driving a &lt;span class="caps"&gt;LED&lt;/span&gt; with or Without a Resistor&lt;/a&gt; by Alexander Weber. &lt;a href="http://www.avagotech.com/docs/5980-3132EN" title="PDF"&gt;Introduction to Driving &lt;span class="caps"&gt;LED&lt;/span&gt; Matrices&lt;/a&gt; technote by Avago Technologies. &lt;a href="http://www.kpsec.freeuk.com/components/led.htm" title="LEDs"&gt;Light Emitting Diodes&lt;/a&gt; by the Electronics Club.&lt;/p&gt;
                  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=nuyjiGrzYI8:UlgbAqY7KWs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=nuyjiGrzYI8:UlgbAqY7KWs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/nuyjiGrzYI8" height="1" width="1"/&gt;</content>
    <published>2011-11-04T01:25:00+02:00</published>
    <updated>2011-11-04T01:25:00+02:00</updated>
    <category term="avr" />
    <category term="electronics" />
  <feedburner:origLink>http://www.appelsiini.net/2011/how-does-led-matrix-work</feedburner:origLink></entry>
  <entry>
    <title>Facebook POST for Canvas fix</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/6KfSBJDLS3E/post-for-canvas-rack-fix" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-04-17:/2011/post-for-canvas-rack-fix</id>
    <content type="html">
                      &lt;div class="alert-message block-message"&gt;
                      &lt;p&gt;Latest usage and examples can be found from &lt;a href="http://www.appelsiini.net/projects/rack-facebook-methodfix"&gt;Rack::Facebook::MethodFix project page&lt;/a&gt;.&lt;/p&gt;
                      &lt;/div&gt;
                      &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/facebook.png" alt="" /&gt;&lt;/p&gt;
                      &lt;p&gt;In early 2011 Facebook started to send all &lt;a href="http://developers.facebook.com/docs/canvas/post/"&gt;iframe application requests as &lt;span class="caps"&gt;POST&lt;/span&gt;&lt;/a&gt;. Because of this most &lt;span class="caps"&gt;REST&lt;/span&gt; based applications broke. Lets say you have object called &lt;code&gt;User&lt;/code&gt;. To get list of users in RESTful application you would issue the following request:&lt;/p&gt;
                      &lt;pre class="prettyprint"&gt;GET /users/&lt;/pre&gt;
                      &lt;p&gt;New user is created with &lt;span class="caps"&gt;POST&lt;/span&gt; request:&lt;/p&gt;
                      &lt;pre class="prettyprint"&gt;POST /users/&lt;/pre&gt;
                      &lt;p&gt;I&amp;#8217;m sure you can spot the problem here. You try to retrieve list of users. That does not work. Instead you end up creating new object because your request was converted to &lt;span class="caps"&gt;POST&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;Usually this could be fixed by sending the intended &lt;span class="caps"&gt;HTTP&lt;/span&gt; verb using &lt;code&gt;_method&lt;/code&gt; parameter. However it is not supported by Facebook.&lt;/p&gt;
                      &lt;p&gt;&lt;a href="https://github.com/tuupola/rack-facebook-method-fix"&gt;Rack::Facebook::MethodFix&lt;/a&gt; middleware looks for incoming &lt;span class="caps"&gt;POST&lt;/span&gt; requests. If the request contains &lt;code&gt;signed_request&lt;/code&gt; parameter it converts request to &lt;span class="caps"&gt;GET&lt;/span&gt; as originally intended. Optionally if you pass in Facebook application secret_id it will also require the contents of &lt;code&gt;signed_request&lt;/code&gt; to be valid.&lt;/p&gt;
                      &lt;h2&gt;Usage&lt;/h2&gt;
                      &lt;p&gt;Install the gem by issuing command:&lt;/p&gt;
                      &lt;pre class="prettyprint"&gt;gem install rack-facebook-method-fix&lt;/pre&gt;
                      &lt;p&gt;After just it is just matter of requiring and using the middleware.&lt;/p&gt;
                      &lt;pre class="prettyprint"&gt;require "rack-facebook-method-fix"&amp;#x000A;use Rack::Facebook::MethodFix&lt;/pre&gt;
                      &lt;p&gt;Optionally if you want to validate contents of &lt;em&gt;signed_request&lt;/em&gt; pass also the application secret.&lt;/p&gt;
                      &lt;pre class="prettyprint"&gt;require "rack-facebook-method-fix"&amp;#x000A;use Rack::Facebook::MethodFix, :secret_id =&amp;gt; "c561df165eacdd6e32672c9eaee10318"&lt;/pre&gt;
                      &lt;p&gt;Related articles: &lt;a href="http://www.appelsiini.net/2010/rake-tasks-for-facebook"&gt;Rake Tasks For Easier Facebook Development&lt;/a&gt;.&lt;/p&gt;
                    &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=6KfSBJDLS3E:1FyOoFdv3Io:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=6KfSBJDLS3E:1FyOoFdv3Io:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/6KfSBJDLS3E" height="1" width="1"/&gt;</content>
    <published>2011-04-17T01:50:00+03:00</published>
    <updated>2011-04-17T01:50:00+03:00</updated>
    <category term="facebook" />
    <category term="ruby" />
  <feedburner:origLink>http://www.appelsiini.net/2011/post-for-canvas-rack-fix</feedburner:origLink></entry>
  <entry>
    <title>How To Make Facebook iFrame Tab Higher Than 800 Pixels?</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/tACJkmga5Tg/how-to-make-iframe-tab-taller-than-800px" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-02-21:/2011/how-to-make-iframe-tab-taller-than-800px</id>
    <content type="html">
                        &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/5000px.png" alt="" /&gt;&lt;/p&gt;
                        &lt;p&gt;One of the best things to happen in Facebook development lately was to enable iframe application tabs. This was widely welcomed by developers. However many &lt;a href="http://sixrevisions.com/web-applications/new-facebook-page/"&gt;blog posts&lt;/a&gt; wrongly &lt;a href="http://www.chilipepperdesign.com/2011/02/14/facebook-iframe-tabs-on-pages-no-more-fbml"&gt;assumed&lt;/a&gt; there was a &lt;a href="http://www.hyperarts.com/blog/adding-iframe-application-to-facebook-fan-page/"&gt;800 pixel limit&lt;/a&gt; on the tab height. Behold, here is the &lt;a href="http://www.facebook.com/apps/application.php?id=197849326909392&amp;amp;sk=app_197849326909392"&gt;5000 pixel tab&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;New Tab is iFrame Canvas Application&lt;/h2&gt;
                        &lt;p&gt;New tab works the same way as iframe canvas application. First load the JavaScript &lt;span class="caps"&gt;SDK&lt;/span&gt;. Easiest is to load it synchronously using a script tag (for production you want to load &lt;span class="caps"&gt;SDK&lt;/span&gt; asynchronously since it gives impression of faster loading page).&lt;/p&gt;
                        &lt;pre class="prettyprint"&gt;&amp;lt;script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"&amp;gt;&amp;lt;/script&amp;gt;&amp;#x000A;&amp;lt;script type="text/javascript" charset="utf-8"&amp;gt;&amp;#x000A;    /* Resizing code will be here. */&amp;#x000A;&amp;lt;/script&amp;gt;  &lt;/pre&gt;
                        &lt;p&gt;When JavaScript &lt;span class="caps"&gt;SDK&lt;/span&gt; is loaded you have access to &lt;a href="http://developers.facebook.com/docs/reference/javascript/fb.canvas.setsize/"&gt;FB.Canvas.setSize&lt;/a&gt; and &lt;a href="http://developers.facebook.com/docs/reference/javascript/fb.canvas.setautoresize/"&gt;FB.Canvas.setAutoResize&lt;/a&gt; methods. If you know the content size wont change later resize the frame only once when page loads.&lt;/p&gt;
                        &lt;pre class="prettyprint"&gt;FB.Canvas.setSize();&lt;/pre&gt;
                        &lt;p&gt;You can also pass the desired size as parameter.&lt;/p&gt;
                        &lt;pre class="prettyprint"&gt;FB.Canvas.setSize({ width: 520, height: 600 });&lt;/pre&gt;
                        &lt;p&gt;If content is dynamic and size might change tell Facebook to resize the iframe periodically.&lt;/p&gt;
                        &lt;pre class="prettyprint"&gt;FB.Canvas.setAutoResize();&lt;/pre&gt;
                        &lt;p&gt;Default is to try resizing every 100ms. If you want to force resizing more often you can pass desired interval as parameter.&lt;/p&gt;
                        &lt;pre class="prettyprint"&gt;FB.Canvas.setAutoResize(50);&lt;/pre&gt;
                        &lt;h2&gt;Is There a Maximum Limit?&lt;/h2&gt;
                        &lt;p&gt;Quick search on Google did not reveal any browser limitation. Theoretically on 32 bit machines maximum page length would be &lt;nobr&gt;4 294 967 295 pixels&lt;/nobr&gt;. On 64 bit machines theoretical limit would be &lt;nobr&gt;8 446 744 073 709 551 615&lt;/nobr&gt; pixels. Now I&amp;#8217;m sure real limit is smaller but the bottom line is: there is enough pixels for marketers to create their next Facebook campaign&amp;#8230;&lt;/p&gt;
                      &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=tACJkmga5Tg:z4ypYmvClmg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=tACJkmga5Tg:z4ypYmvClmg:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/tACJkmga5Tg" height="1" width="1"/&gt;</content>
    <published>2011-02-21T10:40:00+02:00</published>
    <updated>2011-02-21T10:40:00+02:00</updated>
    <category term="facebook" />
  <feedburner:origLink>http://www.appelsiini.net/2011/how-to-make-iframe-tab-taller-than-800px</feedburner:origLink></entry>
  <entry>
    <title>Using Goo.gl With Ruby and HTTParty</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/OVBw588vE3s/using-googl-with-httparty" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-02-20:/2011/using-googl-with-httparty</id>
    <content type="html">
                          &lt;p&gt;I had totally missed that Google recently opened the &lt;span class="caps"&gt;API&lt;/span&gt; of their &lt;a href="http://code.google.com/apis/urlshortener/v1/getting_started.html"&gt;&lt;span class="caps"&gt;URL&lt;/span&gt; shortening service&lt;/a&gt;. I love short and clean APIs. Accessing it using &lt;a href="http://httparty.rubyforge.org/"&gt;HTTParty&lt;/a&gt; is just few lines of code.&lt;/p&gt;
                          &lt;pre class="prettyprint"&gt;require "rubygems"&amp;#x000A;require "httparty"&amp;#x000A;require "json"&amp;#x000A;&amp;#x000A;class Googl&amp;#x000A;  include HTTParty&amp;#x000A;  base_uri "https://www.googleapis.com"&amp;#x000A;  headers  "Content-Type" =&amp;gt; "application/json"&amp;#x000A;&amp;#x000A;  def self.shorten(url)&amp;#x000A;    post("/urlshortener/v1/url", :body =&amp;gt; {:longUrl =&amp;gt; url}.to_json)["id"]&amp;#x000A;  end&amp;#x000A;end&amp;#x000A;&amp;#x000A;puts Googl.shorten("http://www.appelsiini.net/")&lt;/pre&gt;
                          &lt;p&gt;Related articles: &lt;a href="http://www.appelsiini.net/2010/using-shrtst-with-httparty"&gt;Using Shrt.st With HTTParty&lt;/a&gt;, &lt;a href="http://www.appelsiini.net/2010/using-bitly-with-httparty"&gt;Using Bit.ly With HTTParty&lt;/a&gt;.&lt;/p&gt;
                        &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=OVBw588vE3s:7AsvKGwdx8M:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=OVBw588vE3s:7AsvKGwdx8M:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/OVBw588vE3s" height="1" width="1"/&gt;</content>
    <published>2011-02-20T21:10:00+02:00</published>
    <updated>2011-02-20T21:10:00+02:00</updated>
    <category term="ruby" />
  <feedburner:origLink>http://www.appelsiini.net/2011/using-googl-with-httparty</feedburner:origLink></entry>
  <entry>
    <title>iPhone Controlled HTML5 Logo and Color Cube</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/u028afAJJzg/iphone-controlled-html5-logo-and-cube" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-02-02:/2011/iphone-controlled-html5-logo-and-cube</id>
    <content type="html">
                            &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/html5-3d.png" alt="" /&gt;&lt;/p&gt;
                            &lt;p&gt;When Apple released iOS 4.2 end of last year I was on a boat trip to Finland. For me the most interesting features were added to Safari browser. I wanted to learn about WebSockets. New DeviceOrientation &lt;span class="caps"&gt;API&lt;/span&gt; just begged to be abused. I had an idea to control the content of laptop browser by tilting and rotating the phone. I had working but ugly code before ship arrived to Helsinki.&lt;/p&gt;
                            &lt;p class="notice"&gt;If you have short attention span go straight to the &lt;a href="http://www.appelsiini.net/demo/websocket/html5.html"&gt;HTML5 logo&lt;/a&gt; demo.&lt;/p&gt;
                            &lt;p&gt;&lt;a href="https://github.com/tuupola/demo_code/tree/master/websocket"&gt;Code&lt;/a&gt; has been sitting on my hard drive since. I cleaned it up during last couple of days. Also added additional eye candy by using the oh-so-hot-at-the-moment HTML5 logo.&lt;/p&gt;&lt;h2&gt;How Does It Look?&lt;/h2&gt;
                            &lt;p&gt;&lt;iframe src="http://player.vimeo.com/video/19451023?title=0&amp;amp;byline=0&amp;amp;portrait=0" width="688" height="510" frameborder="0" webkitAllowFullScreen allowFullScreen&gt;&lt;/iframe&gt;&lt;/p&gt;
                            &lt;h2&gt;How Does It Work?&lt;/h2&gt;
                            &lt;p&gt;First open demo page with your browser. Browser must support WebSockets. If you are using Safari which also support &lt;span class="caps"&gt;CSS&lt;/span&gt; 3D transforms check &lt;a href="http://www.appelsiini.net/demo/websocket/html5.html"&gt;HTML5 Logo&lt;/a&gt; page. If you are using Chrome try &lt;a href="http://www.appelsiini.net/demo/websocket/cube.html"&gt;Colour Cube&lt;/a&gt; page. When you open a demo page it generates a random &lt;span class="caps"&gt;PIN&lt;/span&gt; number and connects to a WebSocket server. &lt;span class="caps"&gt;PIN&lt;/span&gt; number is used as part of channel name.&lt;/p&gt;
                            &lt;pre class="prettyprint"&gt;var socket = new WebSocket("ws://ws.appelsiini.net:8080/iphone/" + pin);&lt;/pre&gt;
                            &lt;p&gt;Next open the &lt;a href="http://www.appelsiini.net/demo/websocket/iphone.html"&gt;mobile page&lt;/a&gt; with you iPhone or iPod.  Enter &lt;span class="caps"&gt;PIN&lt;/span&gt; number from previous page and press connect. Your mobile browser now connects to same WebSocket server.&lt;/p&gt;
                            &lt;pre class="prettyprint"&gt;var socket = new WebSocket("ws://ws.appelsiini.net:8080/iphone/" + pin);&lt;/pre&gt;
                            &lt;p&gt;If the &lt;span class="caps"&gt;PIN&lt;/span&gt; numbers match your browsers are effectively paired. Mobile browser then uses &lt;a href="http://goo.gl/lEYyx"&gt;DeviceOrientation &lt;span class="caps"&gt;API&lt;/span&gt;&lt;/a&gt; do determine iPhone orientation. When user tilts or turn the device new orientation is sent via WebSocket to server. Server in turn sends the same data to computer browser which is listening on same channel.&lt;/p&gt;
                            &lt;p&gt;Every time &lt;a href="http://www.appelsiini.net/demo/websocket/html5.html"&gt;HTML5 Logo&lt;/a&gt; page receives WebSocket message it animates the logo using &lt;span class="caps"&gt;CSS&lt;/span&gt; 3D transforms.&lt;/p&gt;
                            &lt;pre class="prettyprint"&gt;socket.onmessage = function(event) {&amp;#x000A;    var payload = JSON.parse(event.data);&amp;#x000A;    $("#elevator").css("-webkit-transform", "rotateX(" + payload.data.x * -1 + "deg)");&amp;#x000A;    $("#aileron").css("-webkit-transform", "rotateZ(" + payload.data.y * 1 + "deg)");&amp;#x000A;    $("#pitch").css("-webkit-transform", "rotateY(" + payload.data.z * 1 + "deg)");             &amp;#x000A;}&lt;/pre&gt;
                            &lt;p&gt;&lt;a href="http://www.appelsiini.net/demo/websocket/cube.html"&gt;Colour Cube&lt;/a&gt; is almost the same. Only difference is It uses &lt;a href="http://deanm.github.com/pre3d/"&gt;Pre3d&lt;/a&gt; JavaScript 3d rendering engine to draw and animate the cube.&lt;/p&gt;
                            &lt;div class="alert-message block-message"&gt;
                            &lt;p&gt;Note! Spinning HTML5 logo is taken from &lt;a href="http://code.bocoup.com/html5logo-3d/"&gt;HTML5 demo&lt;/a&gt;  by &lt;a href="http://boazsender.com/"&gt;Boaz Sender&lt;/a&gt;. Color cube is taken from &lt;a href="http://deanm.github.com/pre3d/"&gt;Pre3d demo gallery&lt;/a&gt; by &lt;a href="http://www.deanmcnamee.com/"&gt;Dean McNamee&lt;/a&gt;. I did not originally create them. I just adapted them to connect and animate with iPhones.&lt;/p&gt;
                            &lt;/div&gt;
                            &lt;h2&gt;WebSocket Server&lt;/h2&gt;
                            &lt;p&gt;&lt;a href="https://github.com/tuupola/em-websocket-server"&gt;WebSocket server&lt;/a&gt; itself is simple Ruby script based on &lt;a href="http://rubyeventmachine.com/"&gt;Event Machine&lt;/a&gt; and &lt;a href="https://github.com/igrigorik/em-websocket"&gt;em-websocket&lt;/a&gt;. I could have used &lt;a href="http://www.pusherapp.com/"&gt;Pusher&lt;/a&gt; which provides a hosted service. However I wanted to use this as learning experience. Also if this demo goes popular I might hit some message limits of Pusher.&lt;/p&gt;
                          &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=u028afAJJzg:ymW7zH4wlYA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=u028afAJJzg:ymW7zH4wlYA:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/u028afAJJzg" height="1" width="1"/&gt;</content>
    <published>2011-02-02T19:35:00+02:00</published>
    <updated>2011-02-02T19:35:00+02:00</updated>
    <category term="html5" />
    <category term="javascript" />
    <category term="ruby" />
  <feedburner:origLink>http://www.appelsiini.net/2011/iphone-controlled-html5-logo-and-cube</feedburner:origLink></entry>
  <entry>
    <title>Ajax Fails After Facebook.showPermissionDialog()</title>
    <link href="http://feedproxy.google.com/~r/tuupola/~3/8EHKEfBFtmE/ajax-fails-after-showpermission-dialog" rel="alternate" type="text/html" />
    <id>tag:www.appelsiini.net,2011-01-27:/2011/ajax-fails-after-showpermission-dialog</id>
    <content type="html">
                              &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/facebook.png" alt="" /&gt;&lt;/p&gt;
                              &lt;p&gt;Facebook developing is pretty much trial and error. Documentation has lately been improving a lot. Still there are lot of surprises. Especially with &lt;span class="caps"&gt;FBJS&lt;/span&gt;. Recently I got stuck with a problem of Ajax requests sometimes silently failing. &lt;a href="http://forum.developers.facebook.net/viewtopic.php?id=49023"&gt;Developer forums&lt;/a&gt; and &lt;a href="http://stackoverflow.com/questions/2050903/facebook-ajax-post-fails-after-calling-facebook-showpermissiondialog"&gt;Stack Overflow&lt;/a&gt; had the same problem but no answer.&lt;/p&gt;
                              &lt;h2&gt;Failing &lt;span class="caps"&gt;FBJS&lt;/span&gt; Code&lt;/h2&gt;
                              &lt;p&gt;First user clicks a submit button in application tab. Code will then show dialog requesting for extra permissions.&lt;/p&gt;
                              &lt;p&gt;&lt;img src="http://www.appelsiini.net/attachments/dexter-permission.png" alt="" /&gt;&lt;/p&gt;
                              &lt;p&gt;After giving the permissions code should submit form data to server. I also wanted to to receive the uid of current user. Hence the parameter &lt;em&gt;ajax.requireLogin&lt;/em&gt; is set to &lt;em&gt;true&lt;/em&gt;.&lt;/p&gt;
                              
                              &lt;pre class="prettyprint"&gt;var form      = document.getElementById("form");&amp;#x000A;var form_data = form.serialize();&amp;#x000A;    &amp;#x000A;Facebook.showPermissionDialog("publish_stream", function(permissions) {&amp;#x000A;  &amp;#x000A;    var ajax = new Ajax();&amp;#x000A;    ajax.responseType = Ajax.RAW;&amp;#x000A;    ajax.requireLogin = true;&amp;#x000A;    ajax.ondone = function(data) {&amp;#x000A;    };&amp;#x000A;    ajax.onerror = function() {            &amp;#x000A;    };&amp;#x000A;    &amp;#x000A;    ajax.post("http://www.example.com/submit", form_data);&amp;#x000A;            &amp;#x000A;    return false;&amp;#x000A;});&lt;/pre&gt;
                              &lt;p&gt;Code above worked just fine while developing. While doing final testing I noticed a problem. Ajax request was silently ignored when new user gave application extra permissions. If user had given permissions before or denied the permissions everything was fine. Reloading the page also fixed the problem.&lt;/p&gt;
                              &lt;h2&gt;Trial and Error to the Rescue!&lt;/h2&gt;
                              &lt;p&gt;Forcing a page reload would have been an ugly fix. After hour of hair pulling and trying other things the solution was quite simple:&lt;/p&gt;
                              &lt;pre class="prettyprint"&gt;    ajax.requireLogin = false;&lt;/pre&gt;
                              &lt;p&gt;This parameter simply asks for basic permissions. With basic permissions Facebook sends the user uid with Ajax request. We already ask &lt;em&gt;publish_stream&lt;/em&gt; permission using a dialog. When extra permissions are given application automatically receives also the basic permissions.&lt;/p&gt;
                              &lt;p&gt;Related articles: &lt;a href="http://www.appelsiini.net/2010/rake-tasks-for-facebook"&gt;Rake Tasks For Easier Facebook Development&lt;/a&gt;&lt;/p&gt;
                            &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=8EHKEfBFtmE:hUGcILM4YJs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/tuupola?a=8EHKEfBFtmE:hUGcILM4YJs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/tuupola?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/tuupola/~4/8EHKEfBFtmE" height="1" width="1"/&gt;</content>
    <published>2011-01-27T22:50:00+02:00</published>
    <updated>2011-01-27T22:50:00+02:00</updated>
    <category term="facebook" />
  <feedburner:origLink>http://www.appelsiini.net/2011/ajax-fails-after-showpermission-dialog</feedburner:origLink></entry>
</feed>

