{"id":4757,"date":"2017-03-07T10:17:23","date_gmt":"2017-03-07T10:17:23","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4757"},"modified":"2017-03-07T10:17:23","modified_gmt":"2017-03-07T10:17:23","slug":"microbit-reaction-timer-in-apl-on-the-pi-and-bbc-microbit","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2017\/03\/microbit-reaction-timer-in-apl-on-the-pi-and-bbc-microbit\/","title":{"rendered":"micro:bit Reaction Timer in APL on the Pi and BBC micro:bit"},"content":{"rendered":"<div id=\"attachment_4761\" style=\"width: 160px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/03\/happy.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4761\" class=\"size-thumbnail wp-image-4761\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/03\/happy-150x150.png\" alt=\"BBC micro:bit displaying a happy face\" width=\"150\" height=\"150\" \/><\/a><p id=\"caption-attachment-4761\" class=\"wp-caption-text\">BBC micro:bit displaying a happy face<\/p><\/div>\n<p>I have a bit of a cold today, so I decided that instead of hopping in an icy car and driving to the office in order to spend the day drinking coffee and answering e-mail, I should stay at home, turn up the radiators, make lots of tea (with honey!) and have some fun writing code on my Raspberry Pi! Can there be a better way to ensure a speedy recovery?<\/p>\n<p>By the way, if you are already a user of Dyalog APL but you haven&#8217;t got a Pi yet, you should read <a href=\"http:\/\/dyalog.com\/uploads\/documents\/pi_quickstart_guide.pdf\" target=\"_blank\">The APLer\u2019s Quick-start Guide to the Raspberry Pi<\/a>, which Romilly Cocking completed a few days ago. This explains what your options are for buying hardware, and how to get going with a free copy of Dyalog APL. If you don&#8217;t read it now, your cold may be over before all the bits are delivered, so hurry up!<\/p>\n<p>I wasn&#8217;t feeling THAT energetic this morning, so I decided to ease back into things by trying to replicate <a href=\"http:\/\/blog.rareschool.com\/2017\/01\/a-simple-reaction-timer-in-micropython.html\" target=\"_blank\">Romilly&#8217;s reaction timer<\/a>, which is written in MicroPython. To begin with, I extended the <a href=\"https:\/\/github.com\/APLPi\/microbit\/blob\/master\/microbit.dyalog\" target=\"_blank\">microbit class<\/a> that I developed for the <a title=\"Morse Code \u2013 Revisited using the BBC micro:bit\" href=\"http:\/\/www.dyalog.com\/blog\/2017\/01\/morse-code-revisited-using-the-bbc-microbit\/\">Morse code display function<\/a>\u00a0with a few new methods to cover the API calls that allow me to check the state of the two buttons on each side of the micro:bit display (see the image above).<\/p>\n<p>First, I added a method called <code>is_true<\/code>, which takes a MicroPython expression as an argument, evaluates it using the PyREPL function, and returns 0 or 1 depending on whether the expression returns False or True (and fails if it returns something else):<\/p>\n<pre><code class=\" language-apl\">\u00a0\u2207 r\u2190is_true expr;t;z\r\n :Access Public\r\n r\u21901\u2283t\u2190'True' 'False'\u220a\u2282z\u2190PyREPL expr\r\n :If ~\u2228\/t \u22c4 \u2395\u2190'True\/False expected, got: ',z \u22c4 \u2218\u2218\u2218 \u22c4 :EndIf\r\n \u2207<\/code><\/pre>\n<p>This is a useful building block, which allows the simple construction of functions like <code>was_pressed<\/code>, which takes &#8216;a&#8217; or &#8216;b&#8217; as an argument and tells you whether the button in question has been pressed since you last asked:<\/p>\n<pre><code class=\" language-apl\">\u2207 r\u2190was_pressed button\r\n :Access Public\r\n r\u2190is_true 'button_',button,'.was_pressed()' \r\n\u2207<\/code><\/pre>\n<p>Once this is done, writing the <code>Play<\/code> function in the <code>ReactionTimer<\/code> namespace is easy, you can <a href=\"https:\/\/github.com\/APLPi\/microbit\/blob\/master\/ReactionTimer.dyalog\" target=\"_blank\">find the rest of the code on GitHub<\/a>. The APL version is different from the MicroPython version in that \u2013 once the user presses button B and stops the game \u2013 the reaction times (in milliseconds) are returned as an integer vector. So now we can have some fun with data!<\/p>\n<p>In the spirit of the micro:bit, I thought I&#8217;d produce a low tech character based chart, trying to remember the skills that were start-of-the-art when I started using APL. The <code>AsciiChart<\/code> function in the <code>ReactionTimer<\/code> namespace takes a vector of timings as the right argument, and produces a chart:<\/p>\n<pre><code class=\" language-apl\">\u00a0 \u00a0 \u00a0 times\u2190ReactionTimer.Play\r\n      times\r\n251 305 294 415 338 298 294 251 378\r\n      ReactionTimer.AsciiChart times\r\n425| \r\n400|    * \r\n375|         *\r\n350+ \r\n325|     * \r\n300|  * \r\n275|   *  ** \r\n250+ *      * \r\n<\/code><\/pre>\n<p>The code (which is also on GitHub) is listed below. Because this is a pure function, I used the modern\u00a0<a href=\"http:\/\/www.dyalog.com\/uploads\/documents\/Papers\/dfns.pdf\" target=\"_blank\">dfns style of function definition<\/a>\u00a0rather than the old style procedural form that I&#8217;ve used for the object oriented code. The function works by allocating each value to a bucket of the size defined by the variable scale. The fun part is the use of the outer product with equals (<code class=\" language-apl\">\u2218.=<\/code>) between the list of buckets on the left (rb) and the scaled values on the right (<code class=\" language-apl\">\u230a\u2375\u00f7scale<\/code>) \u2013 and then using this Boolean array to index into a two-element character vector to produce the chart. The rest is scaling calculations and finally decorating with &#8220;tik marks&#8221;:<\/p>\n<pre><code class=\" language-apl\"> AsciiChart\u2190{\r\n     scale\u219025 \u235d size of each row\r\n     tiks\u21904 \u235d tik spacing\r\n     (max min)\u2190(\u2308\/ , \u230a\/) \u2375 \u235d maximum and minimum\r\n     base\u2190\u230amin\u00f7scale \u235d round down to nearest scale unit\r\n     rb\u2190base+0,\u2373\u2308(max-min)\u00f7scale \u235d row base values\r\n     r\u2190' *'[1+rb\u2218.=\u230a\u2375\u00f7scale] \u235d our chart\r\n     r\u2190((\u2262rb)\u2374'+',(tiks-1)\u2374'|'),' ',r \u235d add tiks\r\n     r\u2190(\u2355\u236arb\u00d7scale),r \u235d add base values\r\n     \u2296r \u235d low values last (humans!)\r\n }<\/code><\/pre>\n<p>Although I might be feeling better tomorrow, I expect to be back soon. I have a couple of plastic bags full of resistors, LEDs, potentiometers and cameras (yes, plural) that I have not opened yet!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a bit of a cold today, so I decided that instead of hopping in an icy car and driving to the office in order to spend the day drinking coffee and answering e-mail, I should stay at home, &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2017\/03\/microbit-reaction-timer-in-apl-on-the-pi-and-bbc-microbit\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,27,9],"tags":[],"class_list":["post-4757","post","type-post","status-publish","format-standard","hentry","category-apl-language","category-cxo","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=4757"}],"version-history":[{"count":21,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4757\/revisions"}],"predecessor-version":[{"id":4780,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4757\/revisions\/4780"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}