Tuesday, July 7, 2015

Python - GPIO control on Pidora for Raspberry Pi Model B+

Introduction

On the Raspberry Pi Model B+ board, there are many GPIO ports, here we make a simple test, and try to understand how to control GPIO ports by python?


Concept of hardware control

Generally, an IO port of an IC (e.g. MCU, ARM, GPU) could be have multiple functions. So, when we write a control program to control these IO ports, it can be in 3 steps:
1) Configure the hardware function
2) Execute the control function
3) Release the hardware function



Install RPi.GPIO package

There are many libraries(such as RPi.GPIO, WiringPi, or pigpio library) can do the GPIO control. At here, we are using RPi.GPIO library.

Usually, read or write GPIO, the user must be root.

Install packages:
# yum update
# yum install python-setuptools
# yum install python-pip
# easy_install pyerial   <== serial communication package
# yum install python-devel
# pip install RPi.GPIO   <== GPIO control package
# pip install cython
# reboot


Write a GPIO input/output control program

We write a simple test program to test the GPIO functions. We define functions:
GPIO12 (pin32) - output pin
GPIO13 (pin33) - input pin

Example code:
====================================================================
import time
import RPi.GPIO as GPIO
import kbhit # read keybard value, source code is at bottom

def InputEvent(channel):
     # got input event
     print "[INPUT] Got input event."

if __name__ == '__main__':
    # configure the GPIO hardware condition
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(12, GPIO.OUT) # GPIO12, pin32, output control
    GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # GPIO13, pin33, input control
    GPIO.add_event_detect(13, GPIO.FALLING, callback=InputEvent)

    kb = KBHit()

    bPin32On = False

    while True:
        # if the 'ESC' key is pressed, stop the while loop
        if kb.kbhit():
            c = kb.getch()
            if ord(c) == 27: # ESC 
                break        # Set On/Off pin32
        print "[OUTPUT] Set output event."
        if bPin32On: # True
            GPIO.output(12,GPIO.HIGH)
        else: # False
            GPIO.output(12,GPIO.LOW)
        # switch signal
        bPin32On = not bPin32On 
        time.sleep(2.0)

    kb.set_normal_term()

    # release the hardware configuration 
    GPIO.cleanup()
====================================================================


Get the keyboard value

The KBHit class is used to monitor the non-blocking keyboard event.
Source code:
====================================================================
import os

# Windows
if os.name == 'nt':
    import msvcrt
# Posix (Linux, OS X)
else:
    import sys
    import termios
    import atexit
    from select import select

class KBHit:
     def __init__(self):
    '''Creates a KBHit object that you can call to do various keyboard things.
    '''
    if os.name == 'nt':
        pass
    else: 
        # Save the terminal settings
        self.fd = sys.stdin.fileno() 
        self.new_term = termios.tcgetattr(self.fd)
        self.old_term = termios.tcgetattr(self.fd)
        # New terminal setting unbuffered
        self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO) 
        termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term) 
        # Support normal-terminal reset at exit
        atexit.register(self.set_normal_term)

    def set_normal_term(self):
        ''' Resets to normal terminal. On Windows this is a no-op.          ''' 
        if os.name == 'nt': 
            pass 
        else:
            termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)

    def set_curses_term(self):
        '''          switch to unbuffered terminal 
        :return:
        ''' 
        if os.name == 'nt':
            pass
        else:
            termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)

    def putch(self, ch):
        if os.name == 'nt':
            msvcrt.putch(ch.encode('ascii', 'ignore'))
        else:
            sys.stdout.write(ch)

    def getch(self):
        ''' Returns a keyboard character after kbhit() has been called.
          Should not be called in the same program as getarrow(). 
        '''
        s = ''

        if os.name == 'nt':
            return msvcrt.getch().decode('utf-8')
        else:
            return sys.stdin.read(1)

    def getche(self): 
        ch = self.getch()
        self.putch(ch) 
        return ch

    def getarrow(self):
        ''' Returns an arrow-key code after kbhit() has been called. Codes are
        0 : up
        1 : right
        2 : down 
        3 : left
        Should not be called in the same program as getch().
        '''

        if os.name == 'nt':
            msvcrt.getch() # skip 0xE0 
            c = msvcrt.getch()
            vals = [72, 77, 80, 75]
        else:
            c = sys.stdin.read(3)[2]
            vals = [65, 67, 66, 68]

        return vals.index(ord(c.decode('utf-8')))

    def kbhit(self):
        ''' Returns True if keyboard character was hit, False otherwise.
        '''
        if os.name == 'nt':
            return msvcrt.kbhit()
        else:
            dr,dw,de = select([sys.stdin], [], [], 0)
        return dr != []
====================================================================


Conclusion

Python is a powerful programming tool, it can satisfy the most requirements of our daily tasks. If you want to get the high performance of a program, assember, C, or C++ would be the best choice.


Reference
RPi.GPIO
Python.org

Thursday, July 2, 2015

Run Pidora on Raspberry Pi Model B+

Inroduction

Pidora is a Fedora Remix optimized for the Raspberry Pi computer. So, it is a good candidate choice for implementation if you are familiar with Fedora.
Here, I present the installation steps of installing Pidora 20 on Raspberry Pi Model B+ board. All the preparing works are done on Fedora 20.


Download image file

http://pidora.ca/pidora/releases/20/images/Pidora-2014-R3.zip


Write image file to SD card

dd (convert and copy a file) is a command-line utility for Unix/Linux, I use dd to write Pidora-2014-R3.img file to SD card.

# unzip Pidora-2014-R3.zip (uncompress to Pidora-2014-R3.img)
# dd if=Pidora-2014-R3.img of=/dev/sdb bs=4M (The SD card is at /dev/sdb)

On Windows, you can write image file by Win32 Disk Image.

After the dd command has done, remove the SD card and plug into socket, again. It would be mounted at 2 path:
/run/media/{user}/BOOT
/run/media/{user}/rootfs



Extra works

The default running job is display X window on TV/Monitor via HDMI interface, and the default network is using dynamic IP address. If you want to run it on console mode with static IP address, there is some extra work that you should do.

# vi /run/media/{user}/BOOT/headless
IPADDR=192.168.1.19         # the static IP address
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
RESIZE         # extend the SD card size to its real size
SWAP=512

# vi /run/media/{user}/rootfs/etc/sysconfig/network-scripts/ifcfg-Wired_connection_1
TYPE=Ethernet
BOOTPROTO=none
IPADDR0=192.168.1.19    # the static IP address
PREFIX0=24
GATEWAY0=192.168.1.1
DNS1=168.95.1.1
DNS2=168.95.192.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME="Wired connection 1"
UUID=b2df4348-a75e-4e80-ab3f-470154e1fc8c
ONBOOT=yes


Install/Update application packages

Some of my works is done by python with serial communication, therefore, I have to install some packages:


# yum update
# yum install python-setuptools
# yum install python-pip
# easy_install pyerial   <== serial communication package
# yum install python-devel
# yum install libxml2 libxslt
# yum install libxslt-devel
# pip install RPi.GPIO   <== GPIO control package
# pip install cython
# reboot


Conclusion

Except the write image to SD card process and disk partition policy is different from Fedora on PC, other works are same as you work on Fedora environment.


Reference

Pidroa official site
Win32 Disk Image

Tuesday, December 10, 2013

Use USB camera as barcode reader - image resolution is 1280 x 720

    This is a test, I use my USB camera (1280x720 pixels) to analyze barcode informaiton. It works!

    This test program implements DirectShow filters, capture USB camera image by UVC(USB video class) interface.





Thursday, December 5, 2013

Porting Android 4.0.1 on BeagleBoard Rev C3

    I try to port android 4.0.1 on BeagleBoard Rev C3, it works!
The video of system booting is on youtube:



    I download the android source code from site: https://bitbucket.org/sola/android_manifest, here, Android 4.0 on BeagleBoard and Beagleboard-xM, is a good tutorial of building linux kernel, android, and root file system.

    The BeagleBoard Rev C3 (CPU is OMAP3530 includes ARM Cortex A8).
    The locked screen.
    The apps list.
    OS kernel version.

Monday, October 21, 2013

Sensory System by Zigbee and Embedded Linux

    This is a prototype of sensory system that it integrates sensor devices, embedded linux device, and zigbee wireless communication.




    The application is portable, it can run on Windows, Linux, and embedded linux.


    The sensor devices are light sensor and power meter. The development kits are TI ZNP mini kit and CC2530 module.


    The embedded linux is running on RaspBerry Pi board, almost all the application functions could be run except the performance is poor.


Wizign Co., Ltd.

Email: sales@wizign.com

Wednesday, November 28, 2012

Capture Image Toolkit for UVC Camera

    Develop an application to capture image from an UVC camera for Windows 7/XP is a complex processing steps.

    Base on DirectShow APIs, the major processes are:


Source Filter <--- camera="" p=""> |
V
Filters
 |
V
Video Render Filter ---> Display

    For easily capturing image from a camera device, I have developed a toolkit to simplify the programming steps.



Sunday, November 4, 2012

USB camera and image processing + image analysis

    The experience of developing an USB camera and the demo program is an exciting activities. Here I show that I have done for it.



    After the decision of chipset solution, functions, and components size, I began to design the circuit schematics.


    One of the components is the development board, all my development and test are done by this development board.


    Finally, the finished USB camera.


    After I had made the USB camera work, the next step is to develop a demo program. You can download it, CameraCaptureDemo.




    Special functions for Wizign's USB camera: external/software triggering one shot mode capture.