Shop OBEX P1 Docs P2 Docs Learn Events
QuickStart Touchpad Tutorial demo for PROPGCC — Parallax Forums

QuickStart Touchpad Tutorial demo for PROPGCC

mindrobotsmindrobots Posts: 6,506
edited 2011-11-15 21:07 in Propeller 1
This is a propgcc port of the SPIN only version of the Quickstart button demo by JonnyMac from this thread:

http://forums.parallax.com/showthread.php?132048-QuickStart-module&p=1007660&viewfull=1#post1007660

Nothing exciting but we need simple examples for tutorial purposes. It could probably be trimmed down a bit but I didn't want to lose the essence of how the touchpads worked.
//
// file = QS_padscan.c
// propgcc conversion of JonnyMac's SPIN version of Quickstart Button Demo
// simple propgcc tutorial demo
//
// copyright 2011 by Rick Post
// terms of use below
// 
#include <propeller.h>

int scan_pads(long int delay)
{
      OUTA |= 0x000000ff;                        // "charge" the pads - force them HIGH
      DIRA |= 0x000000ff;                        // set them as output
      DIRA &= 0xffffff00;                        // release touch pads back to input
    waitcnt(delay+CNT);                     // delay for a "touch"
                                            // a touched pad goes to LOW
                                            // so if you invert the input bits you can mask them out
                                            // and return the "touched" pads as HIGHS
    return ((~INA) & 0x000000ff);             // return the touched pads
}


int main(void)
{
    int pads = 0;
    DIRA |= 0x00ff0000;                        // set LEDs for output and pads for input
    DIRA &= 0xffffff00;
    while (1)
    {
        pads = scan_pads(CLKFREQ / 100);    // scan the pads passing delay to wait for user touch
        pads = pads << 16;                    // shift the pad pins (0..7) to the LED pins (16..23)
        OUTA &= 0xff00ffff;                    // clear out the scanned pads from last time
        OUTA |= pads;                        // set the most recent scanned pads
    }
    return(0);
}

/*
    +--------------------------------------------------------------------
      TERMS OF USE: MIT License
    +--------------------------------------------------------------------
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files
    (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    +--------------------------------------------------------------------
*/ 

It exhibits the same flicker and instability issues most of the touchpad demos do.

Beau Schwabe's Sigma-Delta might be my next victim of porting. It's a very nice implementation of "sticky buttons".
c
c

Comments

  • jazzedjazzed Posts: 11,803
    edited 2011-11-11 10:53
    Hi Rick,

    I ran into the same button problems. There is a way to "debounce" the pads.
    This is what I used. It's a port of the code on the ParallaxSemiconductor web site.
    int getButtons()
    {
        int n = 16;
        int result  = -1;
        int reading =  0;
        int pins = 0xFF;
        OUTA |= pins;               //output high to buttons
        while(n--)
        {
            DIRA |= pins;           //output high to buttons
            DIRA ^= pins;           //switch to input
            reading = pins;
            msleep(2);              //wait for RC time
            reading &= INA;          //return button states
            result  &= reading;
        }
        result = ~result & 0xFF;
        return result;
    }
    
  • mindrobotsmindrobots Posts: 6,506
    edited 2011-11-11 11:00
    Haha! I saw your much more elegant and functional implementation after I uploaded mine. YOURS is going into my propgcc code library.....MINE was a learning experience! :lol:
  • Ken GraceyKen Gracey Posts: 7,395
    edited 2011-11-15 21:07
    Rick,

    Works well for me. I'd also like to see this in the library distribution if it meets the "standard. . . " whatever that is. I don't know what good C code looks, but I can identify well-written Spin and PBASIC. I see a fair amount of similarity between the C and Spin.

    The comments are really helpful for me and I appreciate them.

    Great project. Simple. Want to buy a Mac just to blink LEDs on a Propeller now, but I'll just pretend.

    Thanks,

    Ken Gracey
  • laurent974laurent974 Posts: 77
    edited 2020-05-01 07:47
    hello ,
    i would like to understand where does the n=16 come from ?

    is it because of the debounce ? if so why 16 ?
  • laurent974 wrote: »
    hello ,
    i would like to understand where does the n=16 come from ?

    is it because of the debounce ? if so why 16 ?
    The pad variable represents 32 possible I/O pins on the Propeller chip, in binary. I/O Pins 16 thru 23 are hooked up to the touchpad buttons. The code "pads = pads << 16" shifts the bits that were received from the scan_pads function, over to the bits that represent pins 16 thru 23...
    32 bit binary representation of 2 buttons being pressed (buttons 16 & 17)
    
    scan_pads returns pads var as:
    
        31                                 0
        b00000000_00000000_00000000_00000011
    
    after pads = pads << 16, pads looks like:
    
        pins 17 & 16----||
                        ||     
                        vv                       
        b00000000_00000011_00000000_00000000  
    

    dgately
  • Thank you but my question wasn't about "pads = pads << 16"
    but on the code posted above by @jazzed
    int getButtons()
    {
        int n = 16;
        int result  = -1;
        int reading =  0;
        int pins = 0xFF;
        OUTA |= pins;               //output high to buttons
        while(n--)
        {
            DIRA |= pins;           //output high to buttons
            DIRA ^= pins;           //switch to input
            reading = pins;
            msleep(2);              //wait for RC time
            reading &= INA;          //return button states
            result  &= reading;
        }
        result = ~result & 0xFF;
        return result;
    }
    

    There's a loop of 16 that i don't understand.
Sign In or Register to comment.