Danilo's Tech Blog

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 20 November 2012

Python For Android (Py4A)

Posted on 07:01 by Unknown
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.


Read More
Posted in Android, Linux, Python | No comments

Monday, 5 November 2012

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

Posted on 14:16 by Unknown
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 can send you the PCB Artist version by email. 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);
}

Read More
Posted in Arduino | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • How to build Python-4-Android for the ARM Neon
    Currently the Py4A project does not compile for the ARM Neon architecture. If you try to run ndk-build on the project by setting the APP_A...
  • Problems with new version of rpmbuild
    The Problem With the new version of rpmbuild installed on CentOS 6.x, if you try to use an old RPM spec file, you will get an error like the...
  • How to build the gcc Fortran cross-compiler for Android (ARM and x86)
    If you need to cross-compile for Android a program written in Fortran, you know already that the official Android NDK does not come with the...
  • Upgrading mid-2007 24in iMac with an SSD and a 2.5in HDD in the optical bay
    I own a mid-2007 24in iMac with a 2.4 GHz Intel Core 2 Duo processor, 4GB of DDR2 DRAM, running Mac OS Mountain Lion, and for the past few m...
  • Porting your Legacy C/C++ project to Android
    This is a recurring problem people have often: trying to port a big C/C++ project to the Android platform. You have thousands of lines of te...
  • How to inspect expanded C macros with gcc/gcc+
    Sometimes you get compilation errors in gcc/g++ and you'd like to inspect the output from the C compiler pre-processor to figure out why...
  • Android: Trying to load native library results in a process terminated by signal (11)
    Symptoms You are trying to load your native C/C++ library in an Android application and when at runtime your app calls the System.loadLibrar...
  • 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 ...
  • How to build Python-4-Android for the x86
    Currently the Py4A project only compiles for the ARM architecture. The following patch for the python-build sub-directory allows you to cros...
  • How to port Mozilla SpiderMonkey 1.7 to Android
    Another third-party package I needed to cross-compile for Android was Mozilla's SpiderMonkey 1.7 Javascript engine. I found two issues h...

Categories

  • Android
  • Apple
  • Arduino
  • ARM
  • busybox
  • CentOS
  • DHCP
  • Fortran
  • GNU
  • GPON
  • iMac
  • Javascript
  • ksh
  • Linux
  • MacOSX
  • Mips
  • Network
  • Python
  • Router
  • UNIX
  • Windows
  • x86

Blog Archive

  • ►  2013 (12)
    • ►  October (1)
    • ►  August (1)
    • ►  May (1)
    • ►  February (3)
    • ►  January (6)
  • ▼  2012 (11)
    • ►  December (2)
    • ▼  November (2)
      • Python For Android (Py4A)
      • Arduino and 7-segment LED counter driven by two ta...
    • ►  October (7)
Powered by Blogger.

About Me

Unknown
View my complete profile