{"id":4002,"date":"2016-04-30T10:19:10","date_gmt":"2016-04-30T10:19:10","guid":{"rendered":"http:\/\/johnjohnston.info\/blog\/?p=4002"},"modified":"2016-04-30T10:45:26","modified_gmt":"2016-04-30T10:45:26","slug":"a-micro-look-at-microbit","status":"publish","type":"post","link":"https:\/\/johnjohnston.info\/blog\/a-micro-look-at-microbit\/","title":{"rendered":"A micro look at microbit"},"content":{"rendered":"<p>A lot of <a href=\"https:\/\/www.microbit.co.uk\">micro:bits<\/a> from the BBC arrived in the centre where I work, ready to be distributed to North Lanarkshire schools. I&#8217;ve taken the opportunity to break one out and have a wee play.<\/p>\n<p>The devices are aimed at secondary so outside my wheelhouse, but I could not resist a wee play.<\/p>\n<p>The microbic works by creating code for it on a computer and flashing it to the device via USB (you can also use bluetooth from a mobile app). There are several different ways to create code. You can do in in <a href=\"https:\/\/www.microbit.co.uk\/create-code\">the browser<\/a>\u00a0with severe different editors, Code Kingdom&#8217;s JavaScript, The Microsoft Block Editor, Microsoft Touch Develop or Python. I&#8217;ve had a <em>quick<\/em> try of most of these. You can also use the <a href=\"http:\/\/codewith.mu\">MU python editor<\/a> that runs on\u00a0Windows, OSX, Linux and Raspberry Pi.<\/p>\n<p>Although I don&#8217;t really know any python I&#8217;ve found that the MU editor the most reliable. The browser based ones have been occasionally flaky, causing me to switch browsers a few times. I also like to have anything stored locally (the browser editor stores in local storage, but that means you need to either get an account sorted out or use the same browser on the same box all the time.)<\/p>\n<p>There are already a nice set of resource building up, I found the <a href=\"https:\/\/www.raspberrypi.org\/resources\/\">Raspberry Pi<\/a> and <a href=\"https:\/\/microbit-playground.co.uk\/\">micro:bit Playground<\/a> both useful.<\/p>\n<p>When I was looking at the <a href=\"https:\/\/microbit-playground.co.uk\/programs\/ninja\/tilty\">Tilty Game<\/a> from the micro:bit Playground I though I might be able to make a &#8216;paint&#8217; editor. This is the result. (click to start the movie, I&#8217;ve just found you can use a gif as a poster frame)<\/p>\n<div style=\"width: 480px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-4002-1\" width=\"480\" height=\"352\" poster=\"http:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-paint-1.gif\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"http:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-paint.mp4?_=1\" \/><a href=\"http:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-paint.mp4\">http:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-paint.mp4<\/a><\/video><\/div>\n<p>The code allows you to draw on the microbes LEDs, the left and right buttons move the cursor in a horizontal and vertical \u00a0directions and a double press toggle the lights.<\/p>\n<p>And here is the code, I used <a href=\"http:\/\/hilite.me\/\">hilite.me<\/a> to make it look nicer. Not exactly rocket science. I expect there are better ways of doing this.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000; font-weight: bold;\">from<\/span> <span style=\"color: #0000ff; font-weight: bold;\">microbit<\/span> <span style=\"color: #008000; font-weight: bold;\">import<\/span> <span style=\"color: #666666;\">*<\/span>\n\nMatrix <span style=\"color: #666666;\">=<\/span> [[<span style=\"color: #666666;\">0<\/span> <span style=\"color: #008000; font-weight: bold;\">for<\/span> x <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span>(<span style=\"color: #666666;\">5<\/span>)] <span style=\"color: #008000; font-weight: bold;\">for<\/span> x <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span>(<span style=\"color: #666666;\">5<\/span>)]\n\n<span style=\"color: #408080; font-style: italic;\">#set initial position<\/span>\nx <span style=\"color: #666666;\">=<\/span> <span style=\"color: #666666;\">2<\/span>\ny <span style=\"color: #666666;\">=<\/span> <span style=\"color: #666666;\">2<\/span>\n\n<span style=\"color: #008000; font-weight: bold;\">def<\/span> <span style=\"color: #0000ff;\">printmatrix<\/span>():\n    <span style=\"color: #008000; font-weight: bold;\">for<\/span> x <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span>(<span style=\"color: #666666;\">5<\/span>):\n        <span style=\"color: #008000; font-weight: bold;\">for<\/span> y <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span>(<span style=\"color: #666666;\">5<\/span>):\n            <span style=\"color: #008000; font-weight: bold;\">if<\/span> (Matrix[x][y]):\n                display<span style=\"color: #666666;\">.<\/span>set_pixel(x, y, <span style=\"color: #666666;\">6<\/span>)\n            <span style=\"color: #008000; font-weight: bold;\">else<\/span>:\n                display<span style=\"color: #666666;\">.<\/span>set_pixel(x, y, <span style=\"color: #666666;\">0<\/span>)\n    <span style=\"color: #008000; font-weight: bold;\">return<\/span>;\n            \n<span style=\"color: #408080; font-style: italic;\">#show cursor<\/span>\ndisplay<span style=\"color: #666666;\">.<\/span>set_pixel(x, y, <span style=\"color: #666666;\">9<\/span>)\n \n<span style=\"color: #008000; font-weight: bold;\">while<\/span> <span style=\"color: #008000;\">True<\/span>:\n    <span style=\"color: #008000; font-weight: bold;\">if<\/span> button_a<span style=\"color: #666666;\">.<\/span>is_pressed() <span style=\"color: #aa22ff; font-weight: bold;\">and<\/span> button_b<span style=\"color: #666666;\">.<\/span>is_pressed():\n        <span style=\"color: #008000; font-weight: bold;\">if<\/span> (Matrix[x][y]<span style=\"color: #666666;\">==0<\/span>):\n            Matrix[x][y]<span style=\"color: #666666;\">=1<\/span>\n        <span style=\"color: #008000; font-weight: bold;\">else<\/span>:\n            Matrix[x][y]<span style=\"color: #666666;\">=0<\/span>\n        printmatrix()\n        sleep(<span style=\"color: #666666;\">1000<\/span>)\n        <span style=\"color: #008000; font-weight: bold;\">continue<\/span>\n                 \n    <span style=\"color: #008000; font-weight: bold;\">elif<\/span> button_a<span style=\"color: #666666;\">.<\/span>is_pressed():\n        x <span style=\"color: #666666;\">=<\/span> x <span style=\"color: #666666;\">+<\/span> <span style=\"color: #666666;\">1<\/span>\n        <span style=\"color: #008000; font-weight: bold;\">if<\/span> (x<span style=\"color: #666666;\">&gt;4<\/span>):\n            x<span style=\"color: #666666;\">=0<\/span>\n        printmatrix()\n        display<span style=\"color: #666666;\">.<\/span>set_pixel(x, y, <span style=\"color: #666666;\">9<\/span>)\n    <span style=\"color: #008000; font-weight: bold;\">elif<\/span> button_b<span style=\"color: #666666;\">.<\/span>is_pressed():\n        y <span style=\"color: #666666;\">=<\/span> y <span style=\"color: #666666;\">+<\/span> <span style=\"color: #666666;\">1<\/span>\n        <span style=\"color: #008000; font-weight: bold;\">if<\/span> (y<span style=\"color: #666666;\">&gt;4<\/span>):\n            y<span style=\"color: #666666;\">=0<\/span>\n        printmatrix()\n        display<span style=\"color: #666666;\">.<\/span>set_pixel(x, y, <span style=\"color: #666666;\">9<\/span>)\n    sleep(<span style=\"color: #666666;\">200<\/span>)\n<\/pre>\n<\/div>\n<p>The idea is we store a matrix of which lights are on. The ones turned on are shown by the printmatrix function. They are displayed at a brightness of 6 to distinguish them from the cursor, which is full beam.<\/p>\n<p>The cursor is moved with the left and right buttons. it loops (I wonder if it would be better to bounce it?) Clicking the left and right buttons toggles the light on or of in the matrix. The reset button clears the screen.<\/p>\n<p>I had quite a lot of fun getting this to work, the formatting of the script caught me out a few times. I wonder, if I was smarter, could I take the same approach and make a noughts and crosses app?<\/p>\n<ul>\n<li>Some more resources<br \/>\n<a href=\"https:\/\/itunes.apple.com\/gb\/book\/bbc-micro-bit-and-the-ipad\/id1099530510?mt=13\">BBC micro:bit and the iPad by Steve Bunce on iBooks<\/a>\u00a0looks like an interesting resource.<\/li>\n<li>A search of gists at git hub finds some example python code: <a href=\"https:\/\/gist.github.com\/search?utf8=%E2%9C%93&amp;q=microbit&amp;ref=searchresults\">Search \u00b7 microbit<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/results?search_query=microbit\">microbit &#8211; YouTube<\/a> Search<\/li>\n<\/ul>\n<p><em>Featured image on this post a gif made from <a href=\"https:\/\/www.flickr.com\/photos\/120586634@N05\/25965963930\/in\/album-72157666779253585\/\">BBC micro:bit<\/a> by <a href=\"https:\/\/www.flickr.com\/photos\/120586634@N05\/\">Gareth Halfacree<\/a> used under a <a href=\"https:\/\/creativecommons.org\/licenses\/by-sa\/2.0\/\">Creative Commons \u2014 Attribution-ShareAlike 2.0 Generic \u2014 CC BY-SA 2.0<\/a> License.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of micro:bits from the BBC arrived in the centre where I work, ready to be distributed to North Lanarkshire schools. I&#8217;ve taken the opportunity to break one out and have a wee play. The devices are aimed at secondary so outside my wheelhouse, but I could not resist a wee play. The microbic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"mf2_syndication":[],"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"webmentions_disabled_pings":false,"webmentions_disabled":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[76,559,73],"post_format":[],"class_list":{"0":"post-4002","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-wwwd","8":"tag-coding","9":"tag-microbit","10":"tag-python","11":"kind-article","13":"h-entry","14":"hentry"},"better_featured_image":{"id":4019,"alt_text":"","caption":"","description":"","media_type":"image","media_details":{"width":668,"height":544,"file":"2016\/04\/microbit-animation-1.gif","sizes":{"thumbnail":{"file":"microbit-animation-1-150x150.gif","width":150,"height":150,"mime-type":"image\/gif","source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1-150x150.gif"},"medium":{"file":"microbit-animation-1-620x505.gif","width":620,"height":505,"mime-type":"image\/gif","source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1-620x505.gif"},"post-thumbnail":{"file":"microbit-animation-1-668x544.gif","width":668,"height":544,"mime-type":"image\/gif","source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1-668x544.gif"},"sempress-image-post":{"file":"microbit-animation-1-668x544.gif","width":668,"height":544,"mime-type":"image\/gif","source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1-668x544.gif"},"rpwe-thumbnail":{"file":"microbit-animation-1-45x45.gif","width":45,"height":45,"mime-type":"image\/gif","source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1-45x45.gif"},"jetpack-portfolio-admin-thumb":{"file":"microbit-animation-1-50x50.gif","width":50,"height":50,"mime-type":"image\/gif","source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1-50x50.gif"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":4002,"source_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1.gif"},"jetpack_featured_media_url":"https:\/\/johnjohnston.info\/blog\/wp-content\/uploads\/2016\/04\/microbit-animation-1.gif","jetpack_shortlink":"https:\/\/wp.me\/p57zFQ-12y","jetpack_likes_enabled":false,"jetpack_sharing_enabled":true,"kind":false,"_links":{"self":[{"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/posts\/4002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/comments?post=4002"}],"version-history":[{"count":10,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/posts\/4002\/revisions"}],"predecessor-version":[{"id":4021,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/posts\/4002\/revisions\/4021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/media\/4019"}],"wp:attachment":[{"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/media?parent=4002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/categories?post=4002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/tags?post=4002"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/johnjohnston.info\/blog\/wp-json\/wp\/v2\/post_format?post=4002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}