Pages

Saturday, June 08, 2013

GPi 16o8i refactoring notes





































The GPi wiring was intermingling with the camera and LCD monitor and looks messy.  So this morning I tried to do some tidying up.  Now the GPi with 3V3 to 5V0 level shifter are stacked in one tower.

I uesed the Test4StepMotors() to make sure the 16 LEDs are blinking, which shows the following are more or less working.

(1) RPi, GPi, and SPI connection (JTAG and PWM/CLK0 are tested for now.

(2) 3V3 to 5V0 level shifter and LED indicators are OK (8i harware not tested for now)

Next step is to connect 2 step motors to make sure that the GPi 16o 5V0 ULN2803 x2 are more or less OK. 

ftguzuntypi.Test4StepMotors(stepSequence = ftguzuntypi.Wave,  
                            stepAngle = ftguzuntypi.Px245stepAngle,
                            stepTime = ftguzuntypi.MilliSeconds100,
                            direction = ftguzuntypi.Clockwise,  
                            degrees = 45)

ftguzuntypi.Test4StepMotors(stepSequence = ftguzuntypi.Wave,  
                            stepAngle = ftguzuntypi.Px245stepAngle,
                            stepTime = ftguzuntypi.MilliSeconds50,
                            direction = ftguzuntypi.CounterClockwise,  
                            degrees = 45)


def Test4StepMotors(stepSequence, stepAngle, stepTime, direction, degrees):

    ftprint.PrintDoubleSpaceLine("*** Start testing stepping motor ***")    

    print "Step sequence = ", stepSequence
    print "Step angle = ", stepAngle
    print "Step time = ", stepTime
    print "Direction = ", direction
    print "Degrees = ", degrees

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 0) 

    stepCount = int(degrees / stepAngle) 

    if (direction == Clockwise):
        for i in range(stepCount):
            for stepPattern in stepSequence:
       stepPattern1 = stepPattern
stepPattern2 = stepPattern << 4
stepPattern3 = stepPattern1 | stepPattern2
spiChannel.xfer2([stepPattern3, stepPattern3])
                time.sleep(stepTime)
    else:
        for i in range(stepCount):
            for stepPattern in reversed(stepSequence):
                stepPattern1 = stepPattern
stepPattern2 = stepPattern << 4
stepPattern3 = stepPattern1 | stepPattern2
                spiChannel.xfer2([stepPattern3, stepPattern3])
                time.sleep(stepTime)

    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing stepping motor ***")


# fongtoy v1.5 tlfong01 2013jun08

ProgramTitle = "FongToy v1.5 tlfong01 2013jun08"

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

import ftgpio
import ftprint
import ftspi
import ftiox
import fteeprom
import ftguzuntypi
import ftdemux
import fttest
import ftadc

# *** Troubleshooting functions ***

# *** GPIO tests v1.3 tlfong01 2013may23 ***
# ftgpio.TestLed()
# ftgpio.TestBuzzer()
# ftgpio.TestButtonEchoBuzzer()
# ftgpio.TestButtonEchoLed()

# *** SPI Tests v1.3 tlfong01 2013may23 ***

# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 2, testStartAddress = 0x0123, testWriteDataByte = 0x5a)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)

# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0411, testWriteDataByte = 0x4c)
# fttest.TestDemuxGuzuntyClock(mcp23s17SubAddress = 0, guzuntyClockDemuxAddress = 2, secondCount = 10)

# *** Main program ***

# *** Start program message ***
ftprint.StartProgram(ProgramTitle)

# *** Main tests ***

# *** SPI loop back ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60000)

# *** ADC ***
# ftadc.TestMcp3208v03() 
# ftadc.TestMcp3201v04()

# *** EEPROM ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x4100, testDataByte = 0x55)

# *** Mcp23s17 ***
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)

# *** Demux ***
# ftdemux.TestDemuxV01(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)

# *** Guzunty Pi Step Motor ***

ftguzuntypi.Test4StepMotors(stepSequence = ftguzuntypi.Wave,  
                            stepAngle = ftguzuntypi.Px245stepAngle,
                            stepTime = ftguzuntypi.MilliSeconds100,
                            direction = ftguzuntypi.Clockwise,  
                            degrees = 45)

ftguzuntypi.Test4StepMotors(stepSequence = ftguzuntypi.Wave,  
                            stepAngle = ftguzuntypi.Px245stepAngle,
                            stepTime = ftguzuntypi.MilliSeconds50,
                            direction = ftguzuntypi.CounterClockwise,  
                            degrees = 45)

#ftguzuntypi.MoveStepMotor(motorStateList = ftguzuntypi.MotorStateListTest,
#                               motorNumber = 2,
#                               stepSequence = ftguzuntypi.Wave,  
#                               stepAngle = ftguzuntypi.Byj48stepAngle,
#                               stepTime = ftguzuntypi.MilliSeconds100,
#                               direction = ftguzuntypi.Clockwise,  
#                               angularDistance = 10)

# *** Stop program message ***

ftprint.StopProgram()

#.END

# ftguzuntypi.py v1.7 tlfong01 2013jun08

# *****************************************************************************
# Module - ftguzuntypi.py
# Description - Step motor, Led driver 
# *****************************************************************************

# *****************************************************************************
# Imports
# *****************************************************************************

import time
import spidev
from subprocess import call

import ftspi
import ftprint

# *****************************************************************************
# Function - MoveStepMotor()
# Description - 
#   Guzunty Pi P1 pin assignment 
#     Motor #0
#       P1-01 = Winding 1 (A-)
#       P1-02 = Winding 2 (A+)
#       P1-03 = Winding 3 (B-)
#       P1-04 = Winding 4 (B+) 
#     Motor #1 P1 pins 5 ~ 8, Motor # P1 pins 9 ~ 12, Morot #3 P1 pins 13 ~ 16
# Activation sequences
#   Wave = [1, 3, 2, 4] 1-3-2-4 (A-, B-, A+, B+)
#   FullStep =  [[1, 3], [1, 4], [2, 4], [2, 3]]  13-14-24-23 (A-B-, A-B+, A+B+, A+B-)
#   HalfStep =  [[1], [1,4], [4], [2,4], [2], [2,3], [3]] 13-1-14-4-24-2-23-3
# Motor windings list
#   --------------------------------------------------------------
#   Motor          A-(1)    A+(2)    B-(3)   B+(4)    COM (+5V)
#   --------------------------------------------------------------
#   28BYJ48        Pink     Orange   Blue    Yellow   Red  
#   NPM PF35       Black    Yellow   Brown   Orange   Red
#   PX245          Black    Green    Blue    Red      Yelow/White
#   --------------------------------------------------------------
# BYJ series geared step motor - 28byj48-12-300-01
#   Rated voltage - 12V
#   Number of phases - 2
#   Resistance per phase 300R
#   Gear reduction ratio - 1:64
#   Step angle - 5.625 degree /64 ??? or / 48
#   Pull-in torque at 500pps - >= 300 gm
#   Pull-in rate - >= 500 pps
#   Detent torque - >= 350
#   Noise - <= 35dB
# Sample call - 
#   ftguzuntypi.MoveStepMotor(motorStateList = ftguzuntypi.MotorStateListInit,
#                             motorNumber = 1,
#                             stepSequence = ftguzuntypi.Wave,  
#                             stepAngle = ftguzuntypi.Byj48stepAngle,
#                             stepTime = ftguzuntypi.MilliSeconds50,
#                             direction = ftguzuntypi.Clockwise,  
#                             angularDistance = 10)
# *****************************************************************************

Wave = [0x1, 0x4, 0x2, 0x8]
WaveClockwise = [0x1, 0x4, 0x2, 0x8]
WaveCounterClockwise = list.reverse(WaveClockwise)
Clockwise = 0
CounterClockwise = 1
MilliSeconds50 = 0.05
MilliSeconds100 = 0.1
Byj48stepAngle = 5.625 
Px245stepAngle = 1.8
MotorStateListInit = [0xf, 0xf, 0xf, 0xf]
MotorStateListTest = [0x1, 0x4, 0x2, 0x2] 
MotorStateQuadNibbleWordInit = 0xffff
MotorStateQuadNibbleWordTest = 0x1234

def rotateRight(stepSequence, stepNumber):
    return stepSequence[-stepNumber:] + stepSequence[:-stepNumber]

def getStepNumber(stepSequence, stepState):
    for stepNumber in range(4):
        if (stepSequence[stepNumber] == stepState):
   break
    return stepNumber

def MoveStepMotor(motorStateList, motorNumber, stepSequence, stepAngle, stepTime, direction, angularDistance):

    # *** Function parameters ***
    ftprint.PrintDoubleSpaceLine("*** Start MoveStepMotor() ***")    
    print "Motor state list = ", hex(motorStateList[0]), hex(motorStateList[1]), hex(motorStateList[2]), hex(motorStateList[3])
    print "Motor number = ", motorNumber
    print "Step sequence = ", stepSequence
    print "Step angle = ", stepAngle
    print "Step time = ", stepTime
    print "Direction = ", direction
    print "Angular distance = ", angularDistance

    # *** Winding activation pattern sequence adjustment for direction and state synchronization
    print "\nStep sequence original = ", stepSequence
    if (direction == Clockwise):
        stepSequence == list.reverse(stepSequence)
    print "\nStep sequence reversed = ", stepSequence

    stepNumber = getStepNumber(stepSequence, motorStateList[motorNumber])
    stepSequence = rotateRight(stepSequence, stepNumber)
    print "\nStep sequence rotated = ", stepSequence

    # *** Motor state quad nibble (double byte) word testing ***
    motorStateQuadNibbleWord = MotorStateQuadNibbleWordTest # = 0x1234
    print "MotorStateQuadNibbleWord = ", hex(motorStateQuadNibbleWord)

    motorStateHighByte = motorStateQuadNibbleWord >> 8
    motorStateLowByte  = motorStateQuadNibbleWord & 0x00ff
    print "motorStateHighByte = ", hex(motorStateHighByte)
    print "motorStateLowByte = ", hex(motorStateLowByte)

    # *** Motor state quad nibble (double byte) word setting for SPI ***
    motorStateQuadNibbleWord = (motorStateQuadNibbleWord & (~(0x000f << (motorNumber * 4)))) | (0x0005 << (motorNumber * 4))
    print "StateQuadNibbleWord = ", hex(motorStateQuadNibbleWord)

    motorStateHighByte = motorStateQuadNibbleWord >> 8
    motorStateLowByte  = motorStateQuadNibbleWord & 0x00ff
    print "motorStateHighByte = ", hex(motorStateHighByte)
    print "motorStateLowByte = ", hex(motorStateLowByte)

    # *** Spi to GPi *** 

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 0)  
    
    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing stepping motor N ***")
    return motorStateList


# *****************************************************************************
# *****************************************************************************
# *****************************************************************************
# *****************************************************************************
# *****************************************************************************

def TestSteppingMotor(stepSequence, stepAngle, stepTime, direction, degrees):

    ftprint.PrintDoubleSpaceLine("*** Start testing stepping motor ***")    

    print "Step sequence = ", stepSequence
    print "Step angle = ", stepAngle
    print "Step time = ", stepTime
    print "Direction = ", direction
    print "Degrees = ", degrees

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 0) 

    stepCount = int((degrees / stepAngle) * 32) # why not /64 ???

    if (direction == Clockwise):
        for i in range(stepCount):
            for stepPattern in stepSequence:
                spiChannel.xfer2([stepPattern])
                time.sleep(stepTime)
    else:
        for i in range(stepCount):
            for stepPattern in reversed(stepSequence):
                spiChannel.xfer2([stepPattern])
                time.sleep(stepTime)

    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing stepping motor ***")


def Test2StepMotors(stepSequence, stepAngle, stepTime, direction, degrees):

    ftprint.PrintDoubleSpaceLine("*** Start testing stepping motor ***")    

    print "Step sequence = ", stepSequence
    print "Step angle = ", stepAngle
    print "Step time = ", stepTime
    print "Direction = ", direction
    print "Degrees = ", degrees

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 0) 

    stepCount = int((degrees / stepAngle) * 32) # why not /64 ???

    if (direction == Clockwise):
        for i in range(stepCount):
            for stepPattern in stepSequence:
       stepPattern1 = stepPattern
stepPattern2 = stepPattern << 4
stepPattern3 = stepPattern1 | stepPattern2
spiChannel.xfer2([stepPattern3])
                time.sleep(stepTime)
    else:
        for i in range(stepCount):
            for stepPattern in reversed(stepSequence):
                stepPattern1 = stepPattern
stepPattern2 = stepPattern << 4
stepPattern3 = stepPattern1 | stepPattern2
                spiChannel.xfer2([stepPattern3])
                time.sleep(stepTime)

    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing stepping motor ***")


def Test4StepMotors(stepSequence, stepAngle, stepTime, direction, degrees):

    ftprint.PrintDoubleSpaceLine("*** Start testing stepping motor ***")    

    print "Step sequence = ", stepSequence
    print "Step angle = ", stepAngle
    print "Step time = ", stepTime
    print "Direction = ", direction
    print "Degrees = ", degrees

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 0) 

    stepCount = int(degrees / stepAngle) 

    if (direction == Clockwise):
        for i in range(stepCount):
            for stepPattern in stepSequence:
       stepPattern1 = stepPattern
stepPattern2 = stepPattern << 4
stepPattern3 = stepPattern1 | stepPattern2
spiChannel.xfer2([stepPattern3, stepPattern3])
                time.sleep(stepTime)
    else:
        for i in range(stepCount):
            for stepPattern in reversed(stepSequence):
                stepPattern1 = stepPattern
stepPattern2 = stepPattern << 4
stepPattern3 = stepPattern1 | stepPattern2
                spiChannel.xfer2([stepPattern3, stepPattern3])
                time.sleep(stepTime)

    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing stepping motor ***")


# *****************************************************************************
# Function - 
# Description - 
# Sample call -  
#   ftguzuntypi.TestGuzuntyPi4digit7segmentLedModule(spiChannelNumber = 0, spiChipEnableNumber = 1)
# Sample output -
#   *** Start Program - FongToy v1.0 tlfong01 2013may20  ***
#   Tue May 21 08:43:18 2013
#   Hour   digit 1 =  0
#   Hour   digit 2 =  8
#   Minute digit 1 =  4
#   Minute digit 2 =  3
#   Second digit 1 =  1
#   Second digit 2 =  8
#   hourDoubleDigit 0 =  0
#   hourDoubleDigit 1 =  8
#   minuteDoubleDigit 0 =  4
#   minuteDoubleDigit 1 =  3
#   secondDoubleDigit 0 =  1
#   secondDoubleDigit 1 =  8
#   hour =  0 8
#   minute =  4 3
#   second =  1 8
#   *** Stop Program ***
# ***************************************************************************** 

def SetRpiPin7GpClk0Frequency4688Hz():
    returnCode = call(["gpio mode 7 clock"], shell = True)
    returnCode = call("gpio clock 7 4688", shell=True)

def TestGuzuntyClock(spiChannel, secondCount):

    SetRpiPin7GpClk0Frequency4688Hz()

    # *** 7 segment patterns ***
    #PatternDigit0     = ~0x3f
    #PatternDigit1     = ~0x06
    #PatternDigit2     = ~0x5b
    #PatternDigit3     = ~0x4f
    #PatternSpace      = 0xff
    #PatternAllOn      = 0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternDigit0     = 0x3f
    PatternDigit1     = 0x06
    PatternDigit2     = 0x5b
    PatternDigit3     = 0x4f
    PatternSpace      = ~0xff
    PatternAllOn      = ~0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternListAllOff = [PatternSpace, PatternSpace, PatternSpace, PatternSpace]
    PatternListAllOn  = [PatternAllOn, PatternAllOn, PatternAllOn, PatternAllOn]
    PatternList0123   = [PatternDigit0, PatternDigit1, PatternDigit2, PatternDigit3]
    PatternListGood   = [PatternBigG, PatternSmallO, PatternSmallO, PatternSmallD]     
  
    # *** time digits ***
    timeString =  time.asctime(time.localtime(time.time()))
    print timeString

    print "Hour   digit 1 = ", timeString[11:12]
    print "Hour   digit 2 = ", timeString[12:13]
    print "Minute digit 1 = ", timeString[14:15]
    print "Minute digit 2 = ", timeString[15:16]
    print "Second digit 1 = ", timeString[17:18]
    print "Second digit 2 = ", timeString[18:19]

    hourDoubleDigit = [timeString[11:12], timeString[12:13]]
    print "hourDoubleDigit 0 = ", hourDoubleDigit[0]
    print "hourDoubleDigit 1 = ", hourDoubleDigit[1]

    minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
    print "minuteDoubleDigit 0 = ", minuteDoubleDigit[0]
    print "minuteDoubleDigit 1 = ", minuteDoubleDigit[1]

    secondDoubleDigit = [timeString[17:18], timeString[18:19]]
    print "secondDoubleDigit 0 = ", secondDoubleDigit[0]
    print "secondDoubleDigit 1 = ", secondDoubleDigit[1]

    timeHexDigit = [hourDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "hour = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [minuteDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "minute = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [secondDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "second = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    # *** 4 digit 7-segment LED displays ***    

    ftspi.SpiWrite(spiChannel, PatternListAllOff)
    time.sleep(0.5)

    ftspi.SpiWrite(spiChannel, PatternListAllOn)    
    time.sleep(0.5)

    ftspi.SpiWrite(spiChannel, PatternList0123)    
    time.sleep(1)

    ftspi.SpiWrite(spiChannel, PatternListGood)    
    time.sleep(1)

    # SevenSegmentPatternNumbers = [~0x3f, ~0x06, ~0x5b, ~0x4f, ~0x66, ~0x6d, ~0x7d, ~0x07, ~0x7f, ~0x6f]
    SevenSegmentPatternNumbers = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f]
    
    decimalPoint = 0

    for i in range(secondCount):

        print "Guzunty Clock second count = ", i
        timeString =  time.asctime(time.localtime(time.time()))

        hourDoubleDigit = [timeString[11:12], timeString[12:13]]
        minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
secondDoubleDigit = [timeString[17:18], timeString[18:19]]

        minuteDigit0 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[0])]
        minuteDigit1 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[1])]
        secondDigit0 = SevenSegmentPatternNumbers[int(secondDoubleDigit[0])]
        secondDigit1 = SevenSegmentPatternNumbers[int(secondDoubleDigit[1])]

        if (decimalPoint == 0):
   #minuteDigit1 &= ~0x80
   minuteDigit1 |= 0x80
   decimalPoint = 1
        else:
            decimalPoint = 0

        minuteSecondQuadDigitList = [minuteDigit0, minuteDigit1, secondDigit0, secondDigit1]        
ftspi.SpiWrite(spiChannel, minuteSecondQuadDigitList)            
time.sleep(1)

def TestGuzuntyPiLedDriver00(spiChannel, channelNumber, chipEnableNumber):

    SetRpiPin7GpClk0Frequency4688Hz()
    
    spiGuzuntyPi = spidev.SpiDev() 
    spiGuzuntyPi.open(0, 1)

    # *** 7 segment patterns ***
    #PatternDigit0     = ~0x3f
    #PatternDigit1     = ~0x06
    #PatternDigit2     = ~0x5b
    #PatternDigit3     = ~0x4f
    #PatternSpace      = 0xff
    #PatternAllOn      = 0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternDigit0     = 0x3f
    PatternDigit1     = 0x06
    PatternDigit2     = 0x5b
    PatternDigit3     = 0x4f
    PatternSpace      = ~0xff
    PatternAllOn      = ~0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternListAllOff = [PatternSpace, PatternSpace, PatternSpace, PatternSpace]
    PatternListAllOn  = [PatternAllOn, PatternAllOn, PatternAllOn, PatternAllOn]
    PatternList0123   = [PatternDigit0, PatternDigit1, PatternDigit2, PatternDigit3]
    PatternListGood   = [PatternBigG, PatternSmallO, PatternSmallO, PatternSmallD]     
  
    # *** time digits ***
    timeString =  time.asctime(time.localtime(time.time()))
    print timeString

    print "Hour   digit 1 = ", timeString[11:12]
    print "Hour   digit 2 = ", timeString[12:13]
    print "Minute digit 1 = ", timeString[14:15]
    print "Minute digit 2 = ", timeString[15:16]
    print "Second digit 1 = ", timeString[17:18]
    print "Second digit 2 = ", timeString[18:19]

    hourDoubleDigit = [timeString[11:12], timeString[12:13]]
    print "hourDoubleDigit 0 = ", hourDoubleDigit[0]
    print "hourDoubleDigit 1 = ", hourDoubleDigit[1]

    minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
    print "minuteDoubleDigit 0 = ", minuteDoubleDigit[0]
    print "minuteDoubleDigit 1 = ", minuteDoubleDigit[1]

    secondDoubleDigit = [timeString[17:18], timeString[18:19]]
    print "secondDoubleDigit 0 = ", secondDoubleDigit[0]
    print "secondDoubleDigit 1 = ", secondDoubleDigit[1]

    timeHexDigit = [hourDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "hour = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [minuteDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "minute = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [secondDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "second = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    # *** 4 digit 7-segment LED displays ***    

    spiGuzuntyPi.xfer2(PatternListAllOff)    
    time.sleep(1)

    spiGuzuntyPi.xfer2(PatternListAllOn)    
    time.sleep(1)

    spiGuzuntyPi.xfer2(PatternList0123)    
    time.sleep(2)

    spiGuzuntyPi.xfer2(PatternListGood)    
    time.sleep(2)

    # SevenSegmentPatternNumbers = [~0x3f, ~0x06, ~0x5b, ~0x4f, ~0x66, ~0x6d, ~0x7d, ~0x07, ~0x7f, ~0x6f]
    SevenSegmentPatternNumbers = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f]
    
    decimalPoint = 0

    for i in range(2):
        timeString =  time.asctime(time.localtime(time.time()))

        hourDoubleDigit = [timeString[11:12], timeString[12:13]]
        minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
secondDoubleDigit = [timeString[17:18], timeString[18:19]]

        minuteDigit0 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[0])]
        minuteDigit1 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[1])]
        secondDigit0 = SevenSegmentPatternNumbers[int(secondDoubleDigit[0])]
        secondDigit1 = SevenSegmentPatternNumbers[int(secondDoubleDigit[1])]

        if (decimalPoint == 0):
   #minuteDigit1 &= ~0x80
   minuteDigit1 |= 0x80
   decimalPoint = 1
        else:
            decimalPoint = 0

        minuteSecondQuadDigitList = [minuteDigit0, minuteDigit1, secondDigit0, secondDigit1]
        
spiGuzuntyPi.xfer2(minuteSecondQuadDigitList)    
        
time.sleep(1)


def TestGuzuntyPi4digit7segmentLedModule(spiChannelNumber, spiChipEnableNumber):

    spiChannel = spidev.SpiDev() 
    spiChannel.open(spiChannelNumber, spiChipEnableNumber)

    SetRpiPin7GpClk0Frequency4688Hz()

    # *** 7 segment patterns ***
    #PatternDigit0     = ~0x3f
    #PatternDigit1     = ~0x06
    #PatternDigit2     = ~0x5b
    #PatternDigit3     = ~0x4f
    #PatternSpace      = 0xff
    #PatternAllOn      = 0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternDigit0     = 0x3f
    PatternDigit1     = 0x06
    PatternDigit2     = 0x5b
    PatternDigit3     = 0x4f
    PatternSpace      = ~0xff
    PatternAllOn      = ~0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternListAllOff = [PatternSpace, PatternSpace, PatternSpace, PatternSpace]
    PatternListAllOn  = [PatternAllOn, PatternAllOn, PatternAllOn, PatternAllOn]
    PatternList0123   = [PatternDigit0, PatternDigit1, PatternDigit2, PatternDigit3]
    PatternListGood   = [PatternBigG, PatternSmallO, PatternSmallO, PatternSmallD]     
  
    # *** time digits ***
    timeString =  time.asctime(time.localtime(time.time()))
    print timeString

    print "Hour   digit 1 = ", timeString[11:12]
    print "Hour   digit 2 = ", timeString[12:13]
    print "Minute digit 1 = ", timeString[14:15]
    print "Minute digit 2 = ", timeString[15:16]
    print "Second digit 1 = ", timeString[17:18]
    print "Second digit 2 = ", timeString[18:19]

    hourDoubleDigit = [timeString[11:12], timeString[12:13]]
    print "hourDoubleDigit 0 = ", hourDoubleDigit[0]
    print "hourDoubleDigit 1 = ", hourDoubleDigit[1]

    minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
    print "minuteDoubleDigit 0 = ", minuteDoubleDigit[0]
    print "minuteDoubleDigit 1 = ", minuteDoubleDigit[1]

    secondDoubleDigit = [timeString[17:18], timeString[18:19]]
    print "secondDoubleDigit 0 = ", secondDoubleDigit[0]
    print "secondDoubleDigit 1 = ", secondDoubleDigit[1]

    timeHexDigit = [hourDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "hour = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [minuteDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "minute = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [secondDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "second = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    # *** 4 digit 7-segment LED displays ***    

#    spiGuzuntyPi.xfer2(PatternListAllOff)    
#    time.sleep(1)
#    spiGuzuntyPi.xfer2(PatternListAllOn)    
#    time.sleep(1)
#    spiGuzuntyPi.xfer2(PatternList0123)    
#    time.sleep(2)
#    spiGuzuntyPi.xfer2(PatternListGood)    
#    time.sleep(2)

    ftspi.SpiWrite(spiChannel, PatternListAllOff)
    time.sleep(1)

    ftspi.SpiWrite(spiChannel, PatternListAllOn)    
    time.sleep(1)

    ftspi.SpiWrite(spiChannel, PatternList0123)    
    time.sleep(2)

    ftspi.SpiWrite(spiChannel, PatternListGood)    
    time.sleep(2)

    # SevenSegmentPatternNumbers = [~0x3f, ~0x06, ~0x5b, ~0x4f, ~0x66, ~0x6d, ~0x7d, ~0x07, ~0x7f, ~0x6f]
    SevenSegmentPatternNumbers = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f]
    
    decimalPoint = 0

    for i in range(2):
        timeString =  time.asctime(time.localtime(time.time()))

        hourDoubleDigit = [timeString[11:12], timeString[12:13]]
        minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
secondDoubleDigit = [timeString[17:18], timeString[18:19]]

        minuteDigit0 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[0])]
        minuteDigit1 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[1])]
        secondDigit0 = SevenSegmentPatternNumbers[int(secondDoubleDigit[0])]
        secondDigit1 = SevenSegmentPatternNumbers[int(secondDoubleDigit[1])]

        if (decimalPoint == 0):
   #minuteDigit1 &= ~0x80
   minuteDigit1 |= 0x80
   decimalPoint = 1
        else:
            decimalPoint = 0

        minuteSecondQuadDigitList = [minuteDigit0, minuteDigit1, secondDigit0, secondDigit1]        
ftspi.SpiWrite(spiChannel, minuteSecondQuadDigitList)            
time.sleep(1)


def TestGuzuntyPiLedDriver00(spiChannel, channelNumber, chipEnableNumber):

    SetRpiPin7GpClk0Frequency4688Hz()
    
    spiGuzuntyPi = spidev.SpiDev() 
    spiGuzuntyPi.open(0, 1)

    # *** 7 segment patterns ***
    #PatternDigit0     = ~0x3f
    #PatternDigit1     = ~0x06
    #PatternDigit2     = ~0x5b
    #PatternDigit3     = ~0x4f
    #PatternSpace      = 0xff
    #PatternAllOn      = 0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternDigit0     = 0x3f
    PatternDigit1     = 0x06
    PatternDigit2     = 0x5b
    PatternDigit3     = 0x4f
    PatternSpace      = ~0xff
    PatternAllOn      = ~0x00

    PatternBigG       = 0x3d
    PatternSmallG     = 0x6f
    PatternSmallO     = 0x5c
    PatternSmallD     = 0x5e

    PatternListAllOff = [PatternSpace, PatternSpace, PatternSpace, PatternSpace]
    PatternListAllOn  = [PatternAllOn, PatternAllOn, PatternAllOn, PatternAllOn]
    PatternList0123   = [PatternDigit0, PatternDigit1, PatternDigit2, PatternDigit3]
    PatternListGood   = [PatternBigG, PatternSmallO, PatternSmallO, PatternSmallD]     
  
    # *** time digits ***
    timeString =  time.asctime(time.localtime(time.time()))
    print timeString

    print "Hour   digit 1 = ", timeString[11:12]
    print "Hour   digit 2 = ", timeString[12:13]
    print "Minute digit 1 = ", timeString[14:15]
    print "Minute digit 2 = ", timeString[15:16]
    print "Second digit 1 = ", timeString[17:18]
    print "Second digit 2 = ", timeString[18:19]

    hourDoubleDigit = [timeString[11:12], timeString[12:13]]
    print "hourDoubleDigit 0 = ", hourDoubleDigit[0]
    print "hourDoubleDigit 1 = ", hourDoubleDigit[1]

    minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
    print "minuteDoubleDigit 0 = ", minuteDoubleDigit[0]
    print "minuteDoubleDigit 1 = ", minuteDoubleDigit[1]

    secondDoubleDigit = [timeString[17:18], timeString[18:19]]
    print "secondDoubleDigit 0 = ", secondDoubleDigit[0]
    print "secondDoubleDigit 1 = ", secondDoubleDigit[1]

    timeHexDigit = [hourDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "hour = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [minuteDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "minute = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    timeHexDigit = [secondDoubleDigit, minuteDoubleDigit, secondDoubleDigit]
    print "second = ", (timeHexDigit[0])[0], (timeHexDigit[0])[1]

    # *** 4 digit 7-segment LED displays ***    

    spiGuzuntyPi.xfer2(PatternListAllOff)    
    time.sleep(1)

    spiGuzuntyPi.xfer2(PatternListAllOn)    
    time.sleep(1)

    spiGuzuntyPi.xfer2(PatternList0123)    
    time.sleep(2)

    spiGuzuntyPi.xfer2(PatternListGood)    
    time.sleep(2)

    # SevenSegmentPatternNumbers = [~0x3f, ~0x06, ~0x5b, ~0x4f, ~0x66, ~0x6d, ~0x7d, ~0x07, ~0x7f, ~0x6f]
    SevenSegmentPatternNumbers = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f]
    
    decimalPoint = 0

    for i in range(2):
        timeString =  time.asctime(time.localtime(time.time()))

        hourDoubleDigit = [timeString[11:12], timeString[12:13]]
        minuteDoubleDigit = [timeString[14:15], timeString[15:16]]
secondDoubleDigit = [timeString[17:18], timeString[18:19]]

        minuteDigit0 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[0])]
        minuteDigit1 = SevenSegmentPatternNumbers[int(minuteDoubleDigit[1])]
        secondDigit0 = SevenSegmentPatternNumbers[int(secondDoubleDigit[0])]
        secondDigit1 = SevenSegmentPatternNumbers[int(secondDoubleDigit[1])]

        if (decimalPoint == 0):
   #minuteDigit1 &= ~0x80
   minuteDigit1 |= 0x80
   decimalPoint = 1
        else:
            decimalPoint = 0

        minuteSecondQuadDigitList = [minuteDigit0, minuteDigit1, secondDigit0, secondDigit1]
        
spiGuzuntyPi.xfer2(minuteSecondQuadDigitList)    
        
time.sleep(1)

# .END

No comments:

Post a Comment