Monday, March 2, 2015

1Sheeld on a Raspberry Pi using Alamode (or not)

The previous post was about using the Arduino IDE to build and run sketches for a 1Sheeld shield with the 1Sheeld plugged directly into a pcDuino.

This post describes the changes necessary to do similar ... but in this case the 1Sheeld is sitting on an AlaMode (Arduino compatible board) which, in-turn is sitting on a Raspberry Pi.   I have a Raspberry Pi 2 B ... but this should work on any version of the Raspberry Pi that the AlaMode is compatible with.   NOTE: Further investigation shows that these changes are necessary whether you use an Alamode or you simply have an Arduino plugged in to a USB Port.

First off, changes necessary are similar to pcDuino ... so, I'll go through each file that needs to be changed.   NOTE: I'm going to start with the base OneSheeld Arduino libraries, the changes made for pcDuino are NOT included.

Make sure you have the Arduino IDE and Alamode working on the Raspberry Pi before attempting this.  NOTE: Again, this is required even for Arduinos plugged into a Pi USB Port.  Also, make sure to have a copy of the OneSheeld Arduino Libraries and Examples on the Raspberry Pi ... I just copied them to the /usr/share/arduino/libraries directory (you will need to have root privileges to copy them there though) ... you can also copy them to your sketchbook location as specified in the Arduino IDE preferences (you may have to create the directory ... and you will have to create and put them in a libraries directory under the sketchbook directory ... otherwise, the IDE won't find the libraries during compilation of any sketches).

 You may want to add this somewhere to your start-up configuration ...  
 or create an easily accessible script somewhere.  
 stty -onlcr -icrnl -F /dev/ttyS0  (or /dev/ttyS1, etc ... dependent on which is relevant)
 If you don't, using some of the 1Sheeld Shields won't work as-expected ...   
 and if you ever need to you can always do this to reset to default:   
 stty onlcr icrnl -F /dev/ttyS0  (or /dev/ttyS1, etc ... dependent on which is relevant) 
 There are a couple of shields that are affected if these changes   
 aren't in place (Terminal and Internet Shields are known to have problems).  

The only files that need to be changed are OneSheeld.h and OneSheeld.cpp.  All changes are highlighted in Red.

1. OneSheeld.h

For v1.3 libraries, after the #include "Arduino.h"
For v1.4 libraries, after the #include "ShieldParent.h"
For v1.5+ libraries, after the #include "ShieldsInstantiation.h"

add the following #defines ...

 #define ARDUINO_LINUX 1  
 #define RASPBERRYPI 1  

This will do 2 things for us ... the ARDUINO_LINUX define will allow the OneSheeld classes to compile properly (so it knows what version of string is available on the Rasberrry Pi) and the RASPBERRYPI #define will allow us to fix a couple other problems.

2. OneSheeld.cpp

Code in the begin function and where the class is instantiated both need to be changed...

 void OneSheeldClass::begin(long baudRate)  
  #if defined(__AVR_ATmega32U4__) || (defined(ARDUINO_LINUX) && !defined(RASPBERRYPI))  
 #if defined(__AVR_ATmega32U4__) || (defined(ARDUINO_LINUX) && !defined(RASPBERRYPI))  
 OneSheeldClass OneSheeld(Serial1);  
 void serialEvent1()  
 OneSheeldClass OneSheeld(Serial);  
 void serialEvent()  

In both of cases, we want to use the Serial object NOT the Serial1 object when compiling for the Raspberry Pi.  So, even though ARDUINO_LINUX is #defined, we now can check for the RASPBERRYPI #define, and create and use the Serial object, rather than the Serial1 object.


That is literally ALL you need to do.

I originally had a statement here about removing the two #defines if you were going to plug an Arduino into one of the Pi's USB ports (indicating you would have to remove the defines manually before compiling).  Further investigation has proven that these changes ARE REQUIRED for Arduino device compilation too.

A couple additional notes:   On the Raspberry Pi/Alamode combo ... you DO need to switch the UART switch when uploading to the Alamode (just as if you were communicating to an UNO)...and back when running a sketch.   Also, I just leave the power switch set to 3.3V ... it works fine.


03-04-2015 - changed text to indicate changes are required for Arduino devices connected through a USB Port as-well (not just the Alamode).

No comments:

Post a Comment