{"id":454,"date":"2015-05-04T22:25:35","date_gmt":"2015-05-04T20:25:35","guid":{"rendered":"http:\/\/heppg.de\/ikg\/wordpress\/?p=454"},"modified":"2015-05-08T17:33:42","modified_gmt":"2015-05-08T15:33:42","slug":"scratchclient-and-piface","status":"publish","type":"post","link":"https:\/\/heppg.de\/ikg\/wordpress\/?p=454","title":{"rendered":"scratchClient and piFace"},"content":{"rendered":"<p>piFace uses MCP23S17-chip, which is supported by scratchClient.<\/p>\n<p>The chip is a 16bit port expander; in piFace one group is used as an input, the other group of 8 bits are used as outputs, connected to a open collector driver.<br \/>\nOne of the interrupt pins of the chip &#8216;intB&#8217; is connected to\u00a0 GPIO25. This is not used by scratchClient (but does not allow to use this pin for other purpose.<\/p>\n<p>The hardware setup: two LED, a green one and red one, and a small motor powered by a battery and controlled with the relais.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/hardware.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-493\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/hardware.png\" alt=\"hardware\" width=\"488\" height=\"377\" \/><\/a><\/p>\n<p>This post demonstrates the use of scratchClient as a &#8216;hardware abstraction layer&#8217;, assigning meaningful names to scratch variables.<\/p>\n<p>In this example, I connected a red and green LED to piFace.<br \/>\nWith default configuration, the default names are &#8216;out_2&#8217;, &#8216;out_3&#8217;.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_default.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-455 size-full\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_default.png\" alt=\"config_default\" width=\"393\" height=\"153\" \/><\/a><\/p>\n<p>Here a sample of a blinking LED, using these definitions.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_default.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-459\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_default.png\" alt=\"script_default\" width=\"167\" height=\"169\" \/><\/a><\/p>\n<p>This works, but the variable names are not very conclusive. There is the need to keep a record on what this &#8216;out_2&#8217; is meaning. But scratchClient allows to tweak the configuration files and provide more meaningful names for the variables controlling the hardware.<\/p>\n<p>When editing scratchClient\/config\/config_mcp23s17.xml, the names of the variables in scratch can be changed. &#8216;green_led&#8217; and &#8216;red_led&#8217; are better describing what is connected to the hardware.<br \/>\n<a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_renamed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-458 size-full\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_renamed.png\" alt=\"config_renamed\" width=\"386\" height=\"151\" \/><\/a><\/p>\n<p>Restarting scratchClient is needed when configuration is changed.<\/p>\n<p>Side topic: the &#8216;all&#8217;-variable, common to all outputs here, can be used to set all values immediately to either &#8216;0&#8217; or &#8216;1&#8217;. Useful for a &#8217;emergency shutdown&#8217; or alike.<\/p>\n<p>With the meaningful names, a scratch script looks like this:<br \/>\n<a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_renamed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-461\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_renamed.png\" alt=\"script_renamed\" width=\"191\" height=\"175\" \/><\/a><\/p>\n<p>The script gets more expressive. There is no need to map the abstract names to the thing connected.<\/p>\n<p>Another example. Suppose a small motor is connected to relais_1.<a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_relais.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-457\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_relais.png\" alt=\"config_relais\" width=\"389\" height=\"92\" \/><\/a><\/p>\n<p>Here, the variables &#8216;relais_1&#8217;, &#8216;out_1&#8217;, &#8216;all&#8217; can be used to drive this output.<\/p>\n<p>Setting this variable is straightforward, but &#8216;relais_1&#8217; is not a descriptive name.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_motor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-460\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_motor.png\" alt=\"script_motor\" width=\"201\" height=\"103\" \/><\/a><\/p>\n<p>When editing the configuration, the scratch variable names can be changed to &#8216;motor&#8217;.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_motor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-456\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/config_motor.png\" alt=\"config_motor\" width=\"388\" height=\"84\" \/><\/a><\/p>\n<p>This changes the names in scratch to\u00a0 &#8216;motor&#8217;.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_motor_renamed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-464\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/script_motor_renamed.png\" alt=\"script_motor_renamed\" width=\"184\" height=\"86\" \/><\/a><\/p>\n<p>The following snippets show the changes: left side are the default definitions, right side the more meaningful names.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/variables_default.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-462\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/variables_default.png\" alt=\"variables_default\" width=\"200\" height=\"124\" \/><\/a> <a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/variables_renamed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-463\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/05\/variables_renamed.png\" alt=\"variables_renamed\" width=\"192\" height=\"124\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>piFace uses MCP23S17-chip, which is supported by scratchClient. The chip is a 16bit port expander; in piFace one group is used as an input, the other group of 8 bits are used as outputs, connected to a open collector driver. One of the interrupt pins of the chip &#8216;intB&#8217; is connected to\u00a0 GPIO25. This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,3],"tags":[],"class_list":["post-454","post","type-post","status-publish","format-standard","hentry","category-scratch","category-scratchclient"],"_links":{"self":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=454"}],"version-history":[{"count":5,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":495,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions\/495"}],"wp:attachment":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}