In a previous article we introduced the Pltouch, a brand new Android-based HMI with an additional set of I/O pins addressable directly from the Java source code.
In this post, we would like to explain the rationale behind the Android IO library that is provided free of charge in every Pltouch. This Android Java library acts as a bridge between the Android world and the low level I/O pins.
Once the library has been imported in an Eclipse workspace there are two ways in order to structure your app’s code. In both cases you have to
- Create an instance of the Android IO library
- Store this reference in the activity class (like for instance using private/public class field)
- Call the initialization method
Let’s go through every point and expand each one with examples.
Create an instance of the library and store the reference into a class field.
Suppose yout project contains an Android activity called MainActivity. In order to create an instance of the Pltouch I/O support library you need to create a new PltouchManager object and store the reference in a class’ private field, like the following:
public class MainActivity extends Activity { private PltouchManager pManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pManager = new PltouchManager(); } }
If you want that the library updates automatically some designated TextView when the analog inputs changes, you have to provide the activity reference during the object creation:
public class MainActivity extends Activity { private PltouchManager pManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pManager = new PltouchManager(this); } }
Call the library’s initialization method.
Once the PltouchManager class has been created, you need to call the initialization method. The PltouchManager initialize() method has two overloads. The first one that does not require parameters (this is the default scenario).
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pManager = new PltouchManager(); pManager.initialize(); }
Conversely, the second one accepts (at maximum) four TextView that will be used by the library that to display the analog inputs values when they change. An example with two analog inputs is provided by the following code snippet:
public class MainActivity extends Activity{ private PltouchManager pManager; private TextView analog1; private TextView analog2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); analog1 = (TextView)findViewById(R.id.txtAnalog1); analog2 = (TextView)findViewById(R.id.txtAnalog2); pManager = new PltouchManager(this); pManager.initialize(analog1, analog2); } }
These are the only mandatory steps required to use the library. After the library initialization, you can use the four key methods to get the I/O pins status, namely:
- digitalRead
- digitalWrite
- analogRead
- analogWrite
Suppose for instance to add some security check in the Activity’s onCreate callback where once a digital input is high the corresponding digital output will be set to low:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); analog1 = (TextView)findViewById(R.id.txtAnalog1); analog2 = (TextView)findViewById(R.id.txtAnalog2); pManager = new PltouchManager(this); pManager.setDebug(false); pManager.initialize(analog1, analog2); for (int i=1; i < 5; ++i){ try { if (pManager.digitalRead(i) == DigitalValues.HIGH){ pManager.digitalWrite(i, DigitalValues.LOW); } } catch (IndexOutOfBounds e) { Log.d(“Pltouch”, “Index not allowed”); } } }
From the code you can note that the HIGH and LOW values have been coded as enums within the library. Indeed, you can see the use of the PltouchManager’s setDebug method to enable/disable the log produced by the library. Every log entry has pltouch as tag.