{"id":288,"date":"2019-02-18T19:40:47","date_gmt":"2019-02-18T19:40:47","guid":{"rendered":"http:\/\/www.hardwarehacker.co.uk\/blog\/?p=288"},"modified":"2021-04-07T08:29:57","modified_gmt":"2021-04-07T08:29:57","slug":"giving-wall-e-a-screen","status":"publish","type":"post","link":"https:\/\/www.hardwarehacker.co.uk\/blog\/giving-wall-e-a-screen\/","title":{"rendered":"Giving Wall-e a screen"},"content":{"rendered":"<p>Wall-e wouldn&#8217;t be complete without a screen.\u00a0 We&#8217;re using <a href=\"https:\/\/www.ebay.co.uk\/itm\/1-5-128x128-SPI-Full-Colour-SSD1351-OLED-Display-Module-for-Arduino-Pi\/282899471697?hash=item41de1f5d51:g:VvoAAOSwJqVayenM\">this\u00a0 128&#215;128 colour OLED screen<\/a> that works with the PiOLED kernel driver.<\/p>\n<p>After enabling SPI using raspi-config and wiring it up to the SPI bus and a couple of GPIOs needed to access its reset and data\/command pins:<\/p>\n<p><a href=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/Screenshot-from-2019-02-10-17-35-06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-292\" src=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/Screenshot-from-2019-02-10-17-35-06-267x300.png\" alt=\"\" width=\"267\" height=\"300\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/Screenshot-from-2019-02-10-17-35-06-267x300.png 267w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/Screenshot-from-2019-02-10-17-35-06.png 390w\" sizes=\"(max-width: 267px) 100vw, 267px\" \/><\/a><\/p>\n<p>we were able to get it working with the <a href=\"https:\/\/github.com\/notro\/fbtft\/wiki\/fbtft_device\">fbtft driver<\/a>, which exposes the screen as a standard framebuffer device.<\/p>\n<figure id=\"attachment_290\" aria-describedby=\"caption-attachment-290\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/IMG_20190210_122743.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-290 size-medium\" src=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/IMG_20190210_122743-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/IMG_20190210_122743-300x225.jpg 300w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/IMG_20190210_122743-768x576.jpg 768w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/IMG_20190210_122743-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-290\" class=\"wp-caption-text\">Figuring out the colour map<\/figcaption><\/figure>\n<p>I hadn&#8217;t worked with the framebuffer before but it turned out to be fairly simple to use.\u00a0 Basically, it exposes the screen as a special type of file; if you open that file and write a couple of bytes to it, it updates a pixel on the screen and then moves the cursor to the next pixel.\u00a0 Once you&#8217;ve written 128 pixels, it moves to the next line.\u00a0 You can use the seek operation to move the cursor to a different place in the file, which is the same as moving the cursor to a different place on screen.<\/p>\n<p>This particular screen supports 16-bit colour, with 5 bits for red, 6 bits for green and 5 for blue, so the process for writing a colour to the screen is something like this:<\/p>\n<ul>\n<li>Calculate your red, green and blue intensity.<\/li>\n<li>Scale red and blue to the range 0-31 (i.e. 5 bits of precision)<\/li>\n<li>Scale green to 0-63 (i.e. 6 bits).<\/li>\n<li>Pack the bits into a 16 bits: rrrrrggggggbbbbb and then break the 16-bits up into two bytes: rrrrrggg and gggbbbbb<\/li>\n<li>Write those two bytes to the address of the pixel; first the gggbbbbb and then the rrrrrggg byte.<\/li>\n<\/ul>\n<p>Since we&#8217;re writing our code in golang, I searched around for a golang drawing library and found the <a href=\"https:\/\/github.com\/fogleman\/gg\">gg library<\/a>.<br \/>\nAs a prototype, I used that to draw a mock-up of Wall-e&#8217;s screen and then scanned the resulting gg Image, extracting the pixels and writing them to the frame buffer in the 16-bit format:<\/p>\n<div style=\"width: 660px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-288-1\" width=\"660\" height=\"371\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/VID_20190210_141150.mp4?_=1\" \/><a href=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/VID_20190210_141150.mp4\">http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2019\/02\/VID_20190210_141150.mp4<\/a><\/video><\/div>\n<p>The code for the above looks like this:<\/p>\n<pre>func drawOnScreen() {\r\n\t\/\/ Open the frame buffer.\r\n\tf, err := os.OpenFile(\"\/dev\/fb1\", os.O_RDWR, 0666)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\r\n\t\/\/ Loop, simulating a change to battery charge every half second.\r\n\tcharge := 0.0\r\n\tfor range time.NewTicker(500 * time.Millisecond).C {\r\n\t\t\/\/ Create a drawing context of the right size\r\n\t\tconst S = 128\r\n\t\tdc := gg.NewContext(S, S) \r\n\t\tdc.SetRGBA(1, 0.9, 0, 1) \/\/ Yellow\r\n\r\n\t\t\/\/ Get the current heading\r\n\t\theadingLock.Lock()\r\n\t\tj := headingEstimate\r\n\t\theadingLock.Unlock()\r\n\r\n\t\t\/\/ Move the current origin over to the right.\r\n\t\tdc.Push()\r\n\t\tdc.Translate(60, 5)\r\n\t\tdc.DrawString(\"CHARGE LVL\", 0, 10)\r\n\r\n\t\t\/\/ Draw the larger power bar at the bottom. Colour depends on charge level.\r\n\t\tif charge &lt; 0.1 {\r\n\t\t\tdc.SetRGBA(1, 0.2, 0, 1)\r\n\t\t\tdc.Push()\r\n\t\t\tdc.Translate(14, 80)\r\n\t\t\tDrawWarnign(dc)\r\n\t\t\tdc.Pop()\r\n\t\t}\r\n\r\n\t\tdc.DrawRectangle(36, 70, 30, 10)\r\n\r\n\t\tfor n := 2; n &lt; 13; n++ { if charge &gt;= (float64(n) \/ 13) {\r\n\t\t\t\tdc.DrawRectangle(38, 75-float64(n)*5, 26, 3)\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tdc.Fill()\r\n\r\n\t\tdc.DrawString(fmt.Sprintf(\"%.1fv\", 11.4+charge), 33, 93)\r\n\r\n\t\tdc.SetRGBA(1, 0.9, 0, 1)\r\n\r\n\t\t\/\/ Draw the compass\r\n\t\tdc.Translate(14, 30)\r\n\t\tdc.Rotate(gg.Radians(j))\r\n\t\tdc.Scale(0.5, 1.0)\r\n\t\tdc.DrawRegularPolygon(3, 0, 0, 14, 0)\r\n\t\tdc.Fill()\r\n\r\n\t\tdc.Pop()\r\n\r\n\t\tcharge += 0.1\r\n\t\tif charge &gt; 1 {\r\n\t\t\tcharge = 0\r\n\t\t}\r\n\r\n\t\t\/\/ Copy the colours over to the frame buffer.\r\n\t\tvar buf [128 * 128 * 2]byte\r\n\t\tfor y := 0; y &lt; S; y++ {\r\n\t\t\tfor x := 0; x &lt; S; x++ { c := dc.Image().At(x, y) r, g, b, _ := c.RGBA() \/\/ 16-bit pre-multiplied rb := byte(r &gt;&gt; (16 - 5))\r\n\t\t\t\tgb := byte(g &gt;&gt; (16 - 6)) \/\/ Green has 6 bits\r\n\t\t\t\tbb := byte(b &gt;&gt; (16 - 5))\r\n\r\n\t\t\t\tbuf[(127-y)*2+(x)*128*2+1] = (rb &lt;&lt; 3) | (gb &gt;&gt; 3)\r\n\t\t\t\tbuf[(127-y)*2+(x)*128*2] = bb | (gb &lt;&lt; 5)\r\n\t\t\t}\r\n\t\t}\r\n\t\t_, err = f.Seek(0, 0)\r\n\t\tif err != nil {\r\n\t\t\tpanic(err)\r\n\t\t}\r\n\r\n\t\tlock.Lock()\r\n\t\t_, err = f.Write(buf[:])\r\n\t\tlock.Unlock()\r\n\t\tif err != nil {\r\n\t\t\tpanic(err)\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wall-e wouldn&#8217;t be complete without a screen.\u00a0 We&#8217;re using this\u00a0 128&#215;128 colour OLED screen that works with the PiOLED kernel driver. After enabling SPI using raspi-config and wiring it up to the SPI bus and a couple of GPIOs needed to access its reset and data\/command pins: we were able to get it working with &hellip; <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/giving-wall-e-a-screen\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Giving Wall-e a screen<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,5,10],"tags":[8,2],"_links":{"self":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/288"}],"collection":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":2,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":293,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/288\/revisions\/293"}],"wp:attachment":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}