{"id":464,"date":"2013-12-14T20:02:41","date_gmt":"2013-12-14T19:02:41","guid":{"rendered":"http:\/\/hirt.se\/blog\/?p=464"},"modified":"2013-12-16T17:51:03","modified_gmt":"2013-12-16T16:51:03","slug":"using-the-adafruit-lcd-shield-from-java","status":"publish","type":"post","link":"https:\/\/hirt.se\/blog\/?p=464","title":{"rendered":"Using the Adafruit LCD Shield from Java"},"content":{"rendered":"<p><em>We\u2019re interrupting the ordinary programming for a series of Raspberry PI related articles. Yep, I finally bought one and started having some fun.<\/em><\/p>\n<p>I know I am sooo late to the party. My excuse is that having really young kids translates into a constant time shortage. That said, I bought myself a Raspberry Pi just in time for some vacation. One of the first things I did was to get an LCD shield for the Raspberry PI. I settled on this particular kit:<br \/><a title=\"http:\/\/www.adafruit.com\/products\/1115\" href=\"http:\/\/www.adafruit.com\/products\/1115\">http:\/\/www.adafruit.com\/products\/1115<\/a><\/p>\n<p>It\u2019s a really nice little LCD kit which also features a few buttons. The shield uses a port extender (<a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/21952b.pdf\">MCP23017<\/a>) to get another 16 GPIO ports over <a href=\"http:\/\/en.wikipedia.org\/wiki\/I&amp;sup2;C\">I\u00b2C<\/a>. This is quite nice, since you probably want to use your GPIO ports for other things. Also, you can hook up several I\u00b2C devices on the same bus, so using the I\u00b2C pins for the LCD shield doesn\u2019t mean that you\u2019ve used them up.<\/p>\n<p>The LCD shield can be run in various different modes. My little example library uses 16&#215;2 characters by default.<\/p>\n<h2>Building the Kit<\/h2>\n<p>The <a href=\"http:\/\/learn.adafruit.com\/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi\/assembly\">instructions<\/a> for building the kit are excellent, and there are just a few things I\u2019d like to note:<\/p>\n<ol>\n<li>Get a <a href=\"http:\/\/www.adafruit.com\/products\/1112\">stacking header<\/a><br \/>I fortunately came to the conclusion I would need a stacking header before soldering everything together, but unfortunately after I had ordered the LCD shield. I am oh so happy I decided to wait for the header to arrive before finally putting the kit together.\n<li>If you want to attach a standard flat 26 pin cable, trim out some space in the LCD PCB for the little orientation peg on the cable header&nbsp; <br \/>This I did not do \u2013 I ended up trimming down the cable header with my Dremel instead, since it\u2019s too hard to access that area after soldering on the LCD.\n<li>(Optional) Unused GPIO pins for free!<br \/>E.g. <a href=\"http:\/\/learn.adafruit.com\/system\/assets\/assets\/000\/003\/861\/original\/pilcdplate.png?1360171818\">GPA5 appears to not be in use<\/a>. Feel free to use it for your own purposes. The library initializes it as yet another input pin by default, but that is easily changed.\n<li>(Optional) Use interrupts for the buttons<br \/>This will cost you a GPIO pin (and you cannot use one of the unused ones on the LCD shield\u2019s MCP23017), but will allow you to poll the button pins a lot less often without risking missing a button press. You may want to do some additional soldering on pin 20 of the MCP23017 \u2013 INTA is not hooked up by default. This also requires you to hack my Java code a little bit to initialize GPINTEN, and to read from INTF and INTCAP, <\/li>\n<\/ol>\n<h2>Testing the Kit<\/h2>\n<p>Once you\u2019ve built the kit, follow the instructions <a href=\"http:\/\/learn.adafruit.com\/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi\/usage\">here<\/a> to try out the LCD kit using the demo python code. Once you have your LCD shield up and running you can move on to getting everything to run from Java.<\/p>\n<h2>Using the LCD Shield from Java<\/h2>\n<p>Once everything is working from python, you are ready to run things from Java. I mostly did a straight port of the Python library. <\/p>\n<ol>\n<li>First download and install <a href=\"http:\/\/pi4j.com\/download.html\">Pi4J<\/a><br \/>This will be required for most future posts I might put on the blog.\n<li>Next download <a href=\"http:\/\/hirt.se\/downloads\/software\/rpi\/lcd.jar\">lcd.jar<\/a><br \/>The source is available with <a href=\"http:\/\/hirt.se\/downloads\/software\/rpi\/lcd_src.zip\">lcd_src.zip<\/a>.<\/li>\n<\/ol>\n<p>Now you can run the included demos by just invoking the jar like this:<br \/><code>sudo java \u2013Xbootclasspath\/a:\/opt\/pi4j\/lib\/pi4j-core.jar \u2013jar lcd.jar<\/code><\/p>\n<p><a href=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2013\/12\/lcd2.png\"><img loading=\"lazy\" decoding=\"async\" title=\"lcd2\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"lcd2\" src=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2013\/12\/lcd2_thumb.png\" width=\"244\" height=\"184\"><\/a> <\/p>\n<p>When running the demos, use the up\/down buttons to select a demo, then press select to run a demo. When the demo is done, use up\/down to select a new demo. <\/p>\n<h2>API Examples<\/h2>\n<p>Here is how the API can be used to show some text:<\/p>\n<p><code>LCD lcd = new LCD(); <br \/><\/code><code>lcd.setText(\"Hello World!\\n2nd Hello World!\"); <\/code><\/p>\n<p><code><\/code>Here is an example with some buttons:<\/p>\n<p><code>final LCD lcd = new LCD(); <br \/>lcd.setText(\"LCD Test!\\nPress a button...\"); <br \/>ButtonPressedObserver observer = new ButtonPressedObserver(lcd); <br \/>observer.addButtonListener(new ButtonListener() { <br \/>&nbsp;&nbsp;&nbsp; @Override public void onButtonPressed(Button button) { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lcd.clear(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code><code>lcd.setText(button.toString()); <br \/><\/code><code>&nbsp;&nbsp;&nbsp; } <br \/>});<\/code><\/p>\n<p>For more examples, check out the demos in the <font face=\"Courier New\">se.hirt.pi.adafruitlcd.demo<\/font> package. I currently have most of the implementation in my python port LCD class. Anything that isn\u2019t purely reading\/setting registers, I\u2019ve handled outside of that class (such as the polling of buttons). If I ever find some time I might clean up the API and move away from the current python port.<\/p>\n<h2>Tips and Tricks<\/h2>\n<ul>\n<li>If you want to teach the LCD to do additional tricks, the LCD controller used is the <a title=\"https:\/\/www.sparkfun.com\/datasheets\/LCD\/HD44780.pdf\" href=\"https:\/\/www.sparkfun.com\/datasheets\/LCD\/HD44780.pdf\">HD44780<\/a><br \/>Note that it might be valuable to read the spec if for no other reason than to understand the limits (e.g. quite small DDRAM buffer).<\/li>\n<li>MCP23017 is an awesome little thing <br \/>Ever wanted more GPIO ports? Using the MCP23017 is one way to get plenty more. Just remember to provide a unique address by configuring A0-A2 (<a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/21952b.pdf\">see page 8 of the spec<\/a>). Connecting them all to ground will yield 0x20 (which is what the LCD shield uses). Hooking them all up to 5v will yield 0x27.<\/li>\n<\/ul>\n<ul>\n<li>Be careful with how much current you source\/sink, both using the MCP23017 (25mA) and the RPi (16mA GPIO, 50mA 3.3v rail, 5V rail pretty much depends on your power supply). If you need to source\/sink more than the IO pins can handle, you can use a Darlington Array, for example ULN2803.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019re interrupting the ordinary programming for a series of Raspberry PI related articles. Yep, I finally bought one and started having some fun. I know I am sooo late to the party. My excuse is that having really young kids translates into a constant time shortage. That said, I bought myself a Raspberry Pi just [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-464","post","type-post","status-publish","format-standard","hentry","category-java-mission-control-fair-dj"],"_links":{"self":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=464"}],"version-history":[{"count":3,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions"}],"predecessor-version":[{"id":470,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions\/470"}],"wp:attachment":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}