Shop OBEX P1 Docs P2 Docs Learn Events
Need some help with case statement code — Parallax Forums

Need some help with case statement code

Don MDon M Posts: 1,653
edited 2011-07-13 06:11 in Propeller 1
I am trying to build a case statement. During my main loop when I press a key it is supposed to assign the key value to a global variable. I was thinking the way I have this that it would then do something when a key is pressed but it doesn't work.

Any suggestions?
con

  _clkmode = xtal1 + pll16x
  _clkfreq = 80_000_000

  MS_001 = _clkfreq / 1_000

  RS = 9                        ' VFD pin assignments                   
  RW = 10                        
  E  = 11      
  DBLow  = 12   
  DBHigh = 15
                                
  PWR = 20                      ' power sense voltage divider input 

  EE_DEVICE_ADDR = $A0          ' EEPROM assigments
  EE_BASE_ADDR   = $9000

  RLY_UP = 22                   ' table relay outputs. up counts down
  RLY_DN = 23

con

  #1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR, #16, CLS      ' PST formmatting control

'  0.003515185598214                                            ' multiplier for inches / pulse

obj

  term  : "fullduplexserialdp"                          ' for terminal output
  level : "jm_grayenc2"                                 ' 2-bit, graycode encoder 
  kp    : "Keypad_encoder"                              ' keypad driver object P0 - P7
  vfd   : "LCD_Nx2"                                     ' VFD driver
  simp  : "Simple_Numbers_plus"                         ' used for formating decimal places
  i2c   : "Basic_I2C_Driver_1"                          ' EEPROM driver
  
var
   byte key_pressed
   long key_stack_space[50]  

pub main | newlevel, oldlevel, Pressed_Key

  term.start(31, 30, %0000, 115_200)                    ' start terminal for test
  pause(2000)                                           ' wait 2 seconds
  term.tx(CLS)                                          ' clear terminal screen
  
  cognew(Keys, @key_stack_space)                        ' launch new cog for keypad interface
  
  dira[PWR] := 0                                        ' set PWR as input
  dira[RLY_UP..RLY_DN] := 1                             ' set relay ports as outputs
  outa[RLY_UP..RLY_DN] := 0                             ' initialize relay outputs to off
  
  i2c.initialize(28)                                    ' initialize I2C object
  level.init(16, true, 0, 1707, i2c.ReadLong(i2c#BOOTPIN, EE_DEVICE_ADDR, constant(EE_BASE_ADDR + $0)))                             ' detented encoder on p16/p17
                                                        ' initialize counter input object and get starting value from EEPROM
  vfd.Init( E, RS, RW, DBHigh, DBLow )                  ' initialize VFD driver
  kp.start(4, 4, 0, 4, @table)                          ' start keypad driver

  pause(1)
    
  vfd.clear                                             ' clear vfd screen
  term.str(string("Table Controller", 13))              ' print to PST for debug
  term.str(string("Version 0.08"))                      ' print to PST for debug
  vfd.PrintStr(string("Table Controller"))              ' print to VFD
  vfd.SetRowCol(2, 0)                                   ' set position to next row on VFD
  vfd.PrintStr(string("Version 0.08"))                  ' print to VFD
  pause(4000)                                           ' wait 4 seconds
  vfd.clear                                             ' clear VFD display
  term.tx(CLS)                                          ' clear PST screen for debug
  term.tx(LF)                                           ' set position to next line in PST
  term.str(string("Press PROG for Menu"))               ' print to PST screen for debug
  vfd.SetRowCol(2, 0)                                   ' set position to second row on VFD
  vfd.PrintStr(string("Press PROG for Menu"))           ' print to VFD
  pause(500)                                            ' wait 1/2 second

  
  repeat                                                ' Main program loop
  
    oldlevel := newlevel                                ' assign newlevel to oldlevel 
    term.tx(HOME)                                       ' set PST screen to home position
    vfd.SetRowCol(0, 0)                                 ' set position on VFD to first line and home
    vfd.PrintStr(string("Position: "))                  ' print string to VFD
    term.str(string("Position: "))                      ' print string to PST screen for debug
    vfd.SetPos(16)                                      ' set postion on VFD for inches character
    vfd.PrintChr($22)                                   ' print inches character on VFD
    newlevel *= 3515                                    ' multiply count input by scale factor to convert to inches        
    newlevel /= 100                                     ' divide new count value to scale for display
    term.decdp(newlevel, 4)                             ' print inches value to PST screen with 4 decimal places
    vfd.SetPos(10)                                      ' set position on VFD to display inches value
    vfd.PrintStr(simp.decf(newlevel, 4))                ' print inches value to VFD with 4 decimal places

     repeat
       if ina[PWR] == 0                                 ' Looks at P20 for low to indicate loss of power                                              ' 
         i2c.WriteLong(i2c#BOOTPIN, EE_DEVICE_ADDR, constant(EE_BASE_ADDR + $0), newlevel) 
         abort                                          ' if PWR low detected then write current value to EEPROM then stop
                                                        
       Pressed_Key := kp.getkey                         ' read key press assign that value to local pressed_key variable
        if Pressed_Key > 0                              ' if that variable is greater than 0
          key_pressed := Pressed_Key                    ' then assign that value to key_pressed global variable

       newlevel := level.read                           ' read count input and assign to newlevel
                                       
     until (newlevel <> oldlevel)                       ' go to main loop and display values whenever they differ  

 

pub Keys | buff[6], i 

  repeat

    case key_pressed

      "X" :                                             ' STOP key pressed
        outa[RLY_UP..RLY_DN] := 0                       ' turn off table motor relays
        
      "N" :                                             ' MINUS key pressed
        outa[RLY_UP] := 1                               ' turn on table up relay
        vfd.SetRowCol(2, 0)                             ' position cursor on VFD to second line
        vfd.PrintStr(string("Table UP"))                ' display message on VFD

      "P" :                                             ' PLUS key pressed
        outa[RLY_DN] := 1                               ' turn on table down relay
        vfd.SetRowCol(2, 0)                             ' position cursor on VFD to second line
        vfd.PrintStr(string("Table DOWN"))              ' display message on VFD

      "R" :                                             ' PROG button pressed
        i := 0                                          ' set buffer pointer to 0
        repeat 6                                        ' repeat 6 times to fill buffer with key stokes
          buff[i] := kp.getKey                          ' waiting for next key press
          i := i + 1                                    ' increment buffer pointer after each key press

      "S" :                                             ' START key pressed    
          
pub pause(ms) | t

  t := cnt
  repeat ms
    waitcnt(t += MS_001)
    

dat   ' keypad translation table

table   byte  "1", "2", "3", "S"
        byte  "4", "5", "6", "R"
        byte  "7", "8", "9", "E"
        byte  "N", "0", "P", "X"

' Keypad layout
'
' 1  2  3  Start
' 4  5  6  Prog
' 7  8  9  Set
' -  0  +  Stop 

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-12 07:52
    Don,

    The repeat loop in Keys will process the same key press over and over because it can't distinquish an old value of key_pressed from a new value. Also, you are calling kp.getKey from two different cogs, which will cause problems. I'm not sure why you need to start Keys in a separate cog, but it might be better to just call it from the main routine. Add some debug prints to see how far it gets.

    Oh, there is also a problem with calling the vfd methods from multiple cogs. That will cause problems.

    Dave
  • Don MDon M Posts: 1,653
    edited 2011-07-12 20:24
    Still not sure what to do...
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-07-12 21:25
    You need to change
    dira[RLY_UP..RLY_DN] := 1
    

    to
    dira[RLY_UP..RLY_DN] := %11
    

    The first version is only setting the RLY_DN pin as an output.
  • Don MDon M Posts: 1,653
    edited 2011-07-13 04:41
    JonnyMac wrote: »
    You need to change
    dira[RLY_UP..RLY_DN] := 1
    

    to
    dira[RLY_UP..RLY_DN] := %11
    

    The first version is only setting the RLY_DN pin as an output.

    If thats true then does
     outa[RLY_UP..RLY_DN] := 0
    

    make both outputs low?
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-07-13 06:11
    Only when both are set to output mode. I tend to favor binary #s when setting IOs so that I can visualize what I'm doing, though some will use Spin operators like this:
    dira[RLY_UP..RLY_DN]~~
      outa[RLY_UP..RLY_DN]~
    

    The double tilde sets all bits in the group, the single tilde clears all bits in the group.
Sign In or Register to comment.