Tuesday, November 20, 2012

Python For Android (Py4A)

A better solution for cross-compiling Python for Android is to use the Py4A project which is made to be used together with SL4A (Scripting Layer For Android). If you are only interested in the Python interpreter and the runtime Python library, you can also use it standalone.
Get a local copy of the source code using  the following command:

   $ hg clone https://code.google.com/p/python-for-android/

Just focus on the python-build subdirectory and make sure the  python-build/python-src subdirectory is not present (remove it if it came with the Mercurial repository, or else the compilation will fail).
Set up your environment so that the python-for-android build script can pick up the ndk-build script from the Android NDK:

  $ export ANDROID_NDK_ROOT=/home/<your-directory>/android-ndk-r8
  $ export PATH=$ANDROID_NDK_ROOT:$PATH

Finally build Python for Android by issuing the following command:

  $ cd python-for-android/python-build
  $ rm -rf python-src
  $ bash build.sh

Note that on my Ubuntu 12.04 machine I had initially the following compilation error:

Traceback (most recent call last):
  File "build.py", line 161, in <module>
    os.path.join(pwd, 'output.temp', 'usr'))
  File "build.py", line 89, in zipup
    zip_file = zipfile.ZipFile(out_path, 'w', compression=zipfile.ZIP_DEFLATED)
  File "/home/danilo/python-for-android/python-build/host/lib/python2.6/zipfile.py", line 660, in __init__
    "Compression requires the (missing) zlib module"
RuntimeError: Compression requires the (missing) zlib module


I identified the problem in having the zlib library in my system installed under /lib/x86_64-linux-gnu/ instead of one of the traditional lib directories covered by the Python setup.py script. Also on my system I only had libz.so.1 and not libz.so. So to fix both problems I just created a symlink in the standard /usr/lib directory as follows:

  $ cd /usr/lib
  $ sudo ln -s /lib/x86_64-linux-gnu/libz.so.1 libz.so

With this fix the build.sh script was able to successfully build the zlib module for the host environment and create the following zipped files:

  • python_extras_r14.zip
  • python-lib_r16.zip
  • python_r16.zip
  • python_scripts_r13.zip

Of these I only used the python_r16.zip which contains the stripped python interpreter and the runtime libraries, and the python-lib_r16.zip which contains the include header files such as Python.h that can be used to compile Python bindings at development time.


Monday, November 5, 2012

Arduino and 7-segment LED counter driven by two tactile switches

I have posted on YouTube a couple of videos about a project I made with the Arduino prototype board. The circuit uses an Arduino mini and has a simple gear counter based on a 7-segment LED and controlled by two switches. It can be mounted on your bike and used for example to visualize which gear you are in at any time.


If you are interested in the schematics I have the PCB Artist version downloadable here:




The Arduino code is instead available below.

/*
  Blink
 
 Turns on an LED on for one second, then off for one second, repeatedly.
 
 The circuit:
 * LED connected from digital pin 13 to ground.
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 Created 1 June 2005
 By David Cuartielles
 
 http://arduino.cc/en/Tutorial/Blink
 
 based on an orginal by H. Barragan for the Wiring i/o board
 
 */

#define BUTTON_DOWN 10
#define BUTTON_UP 12

int ledPinA =  6;    // LED connected to digital pin 3
int ledPinB =  9;    // LED connected to digital pin 4
int ledPinC =  4;    // LED connected to digital pin 5
int ledPinD =  3;    // LED connected to digital pin 6
int ledPinE =  2;    // LED connected to digital pin 7
int ledPinF =  7;    // LED connected to digital pin 8
int ledPinG =  8;    // LED connected to digital pin 9
int ledPinDP =  5;   // LED connected to digital pin 10

int val_down = 0;
int old_val_down = 0;
int val_up = 0;
int old_val_up = 0;
int state = 0;
int gear = 1;

// The setup() method runs once, when the sketch starts

void setup()   {             
  pinMode(BUTTON_DOWN, INPUT);
  pinMode(BUTTON_UP, INPUT);
  
  // initialize the digital pin as an output:
  pinMode(ledPinA, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(ledPinC, OUTPUT);
  pinMode(ledPinD, OUTPUT);
  pinMode(ledPinE, OUTPUT);
  pinMode(ledPinF, OUTPUT);
  pinMode(ledPinG, OUTPUT);  
  pinMode(ledPinDP, OUTPUT);
  
  // initialize serial communication:
  Serial.begin(9600);
}

void led0()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW);  
  digitalWrite(ledPinF, LOW);  
  digitalWrite(ledPinG, HIGH); 
  digitalWrite(ledPinDP, HIGH);
}

void led1()
{
  digitalWrite(ledPinA, HIGH);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, HIGH);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, HIGH);  
  digitalWrite(ledPinG, HIGH); 
  digitalWrite(ledPinDP, HIGH);
}

void led2()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, HIGH);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW);  
  digitalWrite(ledPinF, HIGH);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
}

void led3()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, HIGH);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
}

void led4()
{
  digitalWrite(ledPinA, HIGH);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, HIGH);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, LOW);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
} 

void led5()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, HIGH);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, LOW);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
} 

void led6()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, HIGH);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW);  
  digitalWrite(ledPinF, LOW);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
} 

void led7()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, HIGH);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, HIGH);  
  digitalWrite(ledPinG, HIGH); 
  digitalWrite(ledPinDP, HIGH);
} 

void led8()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW);  
  digitalWrite(ledPinF, LOW);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
} 

void led9()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, LOW);  
  digitalWrite(ledPinG, LOW); 
  digitalWrite(ledPinDP, HIGH);
} 

void ledDP()
{
  digitalWrite(ledPinA, HIGH);
  digitalWrite(ledPinB, HIGH);
  digitalWrite(ledPinC, HIGH);
  digitalWrite(ledPinD, HIGH);
  digitalWrite(ledPinE, HIGH);  
  digitalWrite(ledPinF, HIGH);  
  digitalWrite(ledPinG, HIGH); 
  digitalWrite(ledPinDP, LOW);
} 

void setLed(int number)
{
  switch (number) {
    case 0:  
      ledDP();
      break;
    case 1:  
      led1();
      break;
    case 2:  
      led2();
      break;
    case 3:  
      led3();
      break;
    case 4:  
      led4();
      break;
    case 5:  
      led5();
      break;
    case 6:  
      led6();
      break;
    case 7:  
      led7();
      break;
    case 8:  
      led8();
      break;
    case 9:  
      led9();
      break;
  }
}
              
// the loop() method runs over and over again,
// as long as the Arduino has power

void oldloop()                     
{
  ledDP();
  delay(1000);                  // wait for a second
  led0();
  delay(1000);                  // wait for a second
  led1();
  delay(1000);                  // wait for a second
  led2();
  delay(1000);                  // wait for a second
  led3();
  delay(1000);                  // wait for a second
  led4();
  delay(1000);                  // wait for a second
  led5();
  delay(1000);                  // wait for a second
  led6();
  delay(1000);                  // wait for a second
  led7();
  delay(1000);                  // wait for a second
  led8();
  delay(1000);                  // wait for a second
  led9();
  delay(1000);                  // wait for a second
}

void loop()
{
    val_up = digitalRead(BUTTON_UP);
    if ((val_up == HIGH) && (old_val_up == LOW)) {
      gear += 1;
      Serial.print("gear number:  ");
      Serial.println(gear, DEC);
      delay(100);
    }
    old_val_up = val_up;
 
    val_down = digitalRead(BUTTON_DOWN);
    if ((val_down == HIGH) && (old_val_down == LOW)) {
      gear -= 1;
      Serial.print("gear number:  ");
      Serial.println(gear, DEC);
      delay(100);
    }
    old_val_down = val_down;
    
    if (gear >= 6) {
      gear = 6;
    }
    if (gear <= 1) {
      gear = 1;
    }
    
    setLed(gear);
}