Pages

Monday, May 20, 2013

fongtoy module v1.0 building notes


So I have started building my very first Python modules, called ftmain.py, ftio.py, and ftprint.py.

It is more messy than I thought, because I need modules call each other, so I need to do imports and qualify functions with their module names.


# ftmain v1.0 tlfong01 2013may20

# *** Program Description ***

ProgramTitle = "FongToy v1.0 tlfong01 2013may20 "

# *** System Imports ***

import sys 
import time 
import smbus 
import pdb 
import spidev 
import wiringpi
import wiringpi2
import RPIO as GPIO  
from RPIO import PWM 
from enum import Enum 
from subprocess import call

# *** Fongtoy imports ***

import ftio
import ftprint

# *** Main function ***

ftprint.StartProgram(ProgramTitle)

ftprint.StopProgram()

#.END


# Basic_GPIO v.10 tlfong01 2013may2001

import RPIO as GPIO
import time

I2cBaseAddress20 = 0x20
I2cBaseAddress21 = 0x21
I2cBaseAddress22 = 0x22
I2cBaseAddress23 = 0x23
I2cBaseAddress24 = 0x24
I2cBaseAddress25 = 0x25
I2cBaseAddress26 = 0x26
I2cBaseAddress27 = 0x27

Mcp23017BaseAddress1 = 0x22 # LED, button
Mcp23008BaseAddress1 = 0x24 # stepping motors
Mcp23008BaseAddress2 = 0x25 # keypad
Mcp23008BaseAddress3 = 0x26 # LCD1602

Mcp23017BaseAddressSystemB1 = 0x20 
Mcp23008BaseAddressSystemB1 = 0x21 
Mcp23008BaseAddressSystemB1 = 0x21 

RPiGPIOgen2  = 27 # P1-13 Test LED
RPiGPIOgen9  = 30 # P5-05 Buzzer  
RPiGPIOgen10 = 31 # P5-06 Button  

LEDpin = RPiGPIOgen2
BuzzerPin = RPiGPIOgen9 
ButtonPin = RPiGPIOgen10

RPiTxD       = 14 # P1-08 UART TxD
RPiRxD       = 15 # P1-10 UART RxD

TxdPin = RPiTxD
RxdPin = RPiRxD 

# * PCM, Clock, Interrupt *

RPiGPIOgen1  = 18 # P1-12 PCM_CLK 
RPiGPIOGclk  = 04 # P1-07 GPIO_GCLK 
RPiGPIOgen6  = 25 # P1-22 IOx/keypad interrupt

RPiPcm       = RPiGPIOgen1  
RPiGpclk0    = RPiGPIOGclk

RpiGpioSpiSelect0  =  7 # SPI_CE1_N P1-26
RpiGpioSpiSelect1  =  8 # SPI_CE0_N P1-24
RpiGpioSpiMiso     =  9 # SPI_MISO P1-21
RpiGpioSpiMosi     = 10 # SPI_MOSI P1-10
RpiGpioSpiClk      = 11 # SPI_SCLK P1-23

SpiClockPin = RpiGpioSpiClk
SpiMosiPin = RpiGpioSpiMosi
SpiMisoPin = RpiGpioSpiMiso
SpiSelect0Pin = RpiGpioSpiSelect0
SpiSelect1Pin = RpiGpioSpiSelect1

RpiGpioGen0 = 17 # GPIO_GEN0 P1-11 Jtag TCK
RpiGpioGen3 = 22 # GPIO_GEN0 P1-15 Jtag TDO
RpiGpioGen4 = 23 # GPIO_GEN4 P1-16 Jtag TDI
RpiGpioGen5 = 24 # GPIO_GEN5 P1-18 Jtag TMS

JtagTckPin = RpiGpioGen0
JtagTdoPin = RpiGpioGen3
JtagTdiPin = RpiGpioGen4
JtagTmsPin = RpiGpioGen5

# * GPIO Input/Output/Interrupt pin list ***************************************

OutputPinList = [LEDpin, BuzzerPin, TxdPin, RPiGpclk0, RPiPcm]
InputPinWithPullUpList = [ButtonPin, RxdPin, RPiGPIOgen6]
InputPinWithNoPullUpList = []

# * Input/Output pin list for debugging only *
# OutputPinList = [LEDpin, BuzzerPin, TxdPin, SpiClockPin, SpiMosiPin, SpiSelect0Pin, SpiSelect1Pin]
# InputPinWithPullUpList = [ButtonPin, RxdPin, RPiGPIOgen6, SpiMisoPin]
# JtagOutputPinList = [JtagTckPin, JtagTdoPin, JtagTmsPin, JtagTdiPin] 
# JtagInputPinList = [] 
# JtagOutputPinList = [JtagTckPin, JtagTdoPin, JtagTmsPin]
# JtagInputPinList = [JtagTdiPin]

# *** Global constants *********************************************************

OnTime = 0.1 # On time = 0.01 second (10mS)
OffTime = 0.25 # Off time = 0.25 second (25 mS)

# *** Basic GPIO Setup/Read/Write Functions ***********************************

# * Nibble names * 

LowNibble = 0
HighNibble = 1
# BothNibble = 2 # full byte of 8 bits

# * Nibble constants *
HighNibble1LowNibble0 = 0xf0
HighNibble0LowNibble1 = 0x0f

# * LED and buzzer states *
Off = False
On = True

# * Button states *
ButtonPressed = False
ButonReleased = True

# * Interrupt states *
Low = False
High = True

# * Setup, Read/Write GPIO pins *

# * Individual pins *

setupOutputPin = lambda oPin: GPIO.setup(oPin, GPIO.OUT) # set GPIO pin as output 
setupInputPinWithNoPullUp = lambda iPin: GPIO.setup(iPin, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # set GPIO pin as input, no pull up
setupInputPinWithPullUp = lambda iPin: GPIO.setup(iPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO pin as input, with pull up
writeOutputPin = lambda oPin, oValue: GPIO.output(oPin, oValue) # write value to output pin
readInputPin = lambda iPin: GPIO.input(ButtonPin) # read value from input pin
setupWriteOutputPin = lambda oPin, oValue: (setupOutputPin(oPin), writeOutputPin(oPin, oValue)) # set and write

def SetupGPIO(): 
    SetupGPIOpins(OutputPinList, InputPinWithNoPullUpList, InputPinWithPullUpList )

def CleanUpGpio():      
    GPIO.cleanup()

def SetupGPIOpins(outputPinList, inputPinWithNoPullUpList, inputPinWithPullUpList): 
    for oPin in outputPinList:
       setupWriteOutputPin(oPin, Off)
    for iPin in inputPinWithNoPullUpList:
        setupInputPinWithNoPullUp(iPin)
    for iPin in inputPinWithPullUpList:
        setupInputPinWithPullUp(iPin)

# * Basic LED/Buzzer/Button Functions * 

def pulsePin(oPin, onTime, offTime): # blink LED or beep buzzer
    writeOutputPin(oPin, On)
    time.sleep(onTime)
    writeOutputPin(oPin, Off)    
    time.sleep(offTime)

def echoPin(iPin, oPin): # echo input pin to output pin, e.g. button to LED or buzzer
    while True:
        if readInputPin(iPin) == ButonReleased:
            pass
        else:
            pulsePin(oPin, OnTime, OffTime)
            break
        continue

def togglePin(oPin, toggleTime): # toggle pin
    writeOutputPin(oPin, On)
    time.sleep(toggleTime)
    writeOutputPin(oPin, Off)    
    time.sleep(toggleTime)

# * Testing Buzzer/LED/Button/UART/JTAG/Interrupt *

def TestBuzzer(): # beep 4 times
    SetupGPIO()
    for i in range (4):
        pulsePin(BuzzerPin, OnTime, OffTime)

def TestLED(): # blink 4 times
    SetupGPIO()
    for i in range (4): 
        pulsePin(LEDpin, OnTime, OffTime)

def TestGpioPin(oPin, toggleTime, toggleCount): 
    for i in range(toggleCount):
        togglePin(oPin, toggleTime)

def TestButtonEchoBuzzer(): #
    SetupGPIO()
    print "\n", "Press button 4 times.", "\n"
    for i in range (4):
        echoPin(ButtonPin, BuzzerPin)          

def TestButtonEchoTxD(): #
    SetupGPIO()
    print "\n", "Press button 4 times.", "\n"
    for i in range (4):
        echoPin(ButtonPin, TxdPin)  

def TestButtonEchoRxD(): #
    SetupGPIO()
    print "\n", "Press button 4 times.", "\n"
    for i in range (4):
        echoPin(ButtonPin, RxdPin)  

def TestRxdEchoTxD(): #
    SetupGPIO()
    print "\n", "RxD Echo TxD - Press button 4 times.", "\n"
    for i in range (4):
        echoPin(RxdPin, TxdPin) 

def TestButtonEchoLED(): #
    SetupGPIO()
    print "\n", "Press button 4 times.", "\n"    
    for i in range (4):
        echoPin(ButtonPin, LEDpin)

def TestTxdPin(): # blink 4 times
    SetupGPIO()
    for i in range (4): 
        pulsePin(TxdPin, OnTime, OffTime)

def TestRxdPin(): # blink 4 times
    SetupGPIO()
    for i in range (4): 
        pulsePin(RxdPin, OnTime, OffTime)

def TestTxdPinRxdPin1(): # blink, read 4 times
    SetupGPIO()
    for i in range (4): 
        writeOutputPin(TxdPin, On)
        if readInputPin(RxdPin) == High:
   print "RxD input = High"
        else:
   print "RxD input = Low"
        time.sleep(1)
        writeOutputPin(TxdPin, Off)   
if readInputPin(RxdPin) == High:
   print "RxD input = High"
        else:
   print "RxD input = Low"
        time.sleep(1)

def TestTxdPinRxdPin2(): #
    SetupGPIO()
    print "\n", "Short to Ground RxdPin 4 times.", "\n"    
    for i in range (4):
        echoPin(RxdPin, TxdPin)

def SetupJtagGpio():
    SetupGPIOpins(outputPinList = JtagOutputPinList, inputPinWithNoPullUpList = JtagInputPinList, inputPinWithPullUpList = []) 

def TestJtagPins(toggleTime, testCount): 
    SetupJtagGpio()
    Beep(1)
    for i in range(testCount):
        togglePin(JtagTckPin, toggleTime) 
    Beep(1)
    for i in range(testCount):
        togglePin(JtagTdoPin, toggleTime) 
    Beep(1)
    for i in range(testCount):
        togglePin(JtagTmsPin, toggleTime) 

    for i in range(testCount):
        writeOutputPin(JtagTdoPin, 1)
        if readInputPin(JtagTdiPin) == 1:
   print "JtagTdiPin = 1"
        else:
   print "JtagTdiPin = 0"
        time.sleep(1)
writeOutputPin(JtagTdoPin, 0)
        if readInputPin(JtagTdiPin) == 1:
   print "JtagTdiPin = 1"
        else:
   print "JtagTdiPin = 0"
        time.sleep(1)

    Beep(3)

    GPIO.cleanup()

def TestInterruptPinFallingEdgeDetection(): # !!! OUT OF DATE, not sure if still working !!!
    GPIO.cleanup() # set all input pins no pull up, disable all interutp detection setting
    SetupGPIO()   
    GPIO.set_low_event(InterruptPin) # set up low level detection 

    for i in range(30):
        if GPIO.event_detected(InterruptPin):
   break
        else:
            print "No interrupt detected.", i
            time.sleep(1)
   continue

    GPIO.set_low_event(InterruptPin, enable = False)  # disable detection
    print "End of test, or interrupt detected"

#.END

# ftprint v1.0 tlfong01 2013may20

import time
import RPIO as GPIO
import ftio

# *** Printing and debugging functions ***

# * Set/Reset one bit of a byte *

def SetDataBit(dataByte, bitIndex):
    setDataByte = 0x01 << bitIndex
    dataByte = dataByte | setDataByte
    return dataByte

def ResetDataBit(dataByte, bitIndex):
    resetDataByte = ~(0x01 << bitIndex)
    dataByte = dataByte & resetDataByte
    return dataByte

# * Print nibble/byte as 4/8 bit pattern for debugging *

def PrintFourBitPattern(message, dataByte):
    fourBitPattern = ConvertIntegerToFourBitPattern(dataByte)
    print message, fourBitPattern

def PrintEightBitPattern(message, dataByte):
    eightBitPattern = ConvertIntegerToEightPattern(dataByte)
    print message, eightBitPattern

def ConvertIntegerToFourBitPattern(integer):
    FourBitPattern = [""]
    for k in range(4): 
        FourBitPattern = [i+j for i in ['0','1'] for j in FourBitPattern]
    return FourBitPattern[integer]

def ConvertIntegerToEightPattern(integer):
    EightBitPattern = [""]
    for k in range(8): 
        EightBitPattern = [i+j for i in ['0','1'] for j in EightBitPattern]
    return EightBitPattern[integer]

# * Beep/Start/Stop program functions *

def StartProgram(programTitle):
    ftio.CleanUpGpio()
    ftio.SetupGPIO()
    StartBeep()
    message =  "\n" + "*** Start Program - " + programTitle + " ***" + "\n"
    print message

def StopProgram():
    ftio.SetupGPIO()
    StopBeep()
    # print "\n" + "*** Resetting GPIO input, no pull up/down, no event detect. ***" + "\n"
    ftio.CleanUpGpio()
    print "\n" + "*** Stop Program ***" + "\n"    

def Beep(count):
    for i in range(count):
        ftio.pulsePin(ftio.BuzzerPin, ftio.OnTime, ftio.OffTime)
    
def StartBeep():
    Beep(2)
    time.sleep(1)

def StopBeep():
    Beep(2)

def OneBeep():
   Beep(1)

def FourBeeps():
   Beep(4)

# .END

No comments:

Post a Comment