PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
jm_touchpads.c
1 //
2 // automatically generated by spin2cpp v1.93 on Tue Aug 11 09:49:47 2015
3 // spin2cpp --ccode --main demo.spin
4 //
5 
6 // io control
7 // =================================================================================================
8 //
9 // File....... touch.spin
10 // Purpose.... Touchpad reader for Parallax boards (Quickstart, DC22 badge, eBadge)
11 // Author..... Jon "JonnyMac" McPhalen
12 // Copyright (C) 2015 Jon McPhalen
13 // -- see below for terms of use
14 // E-mail..... jon@jonmcphalen.com
15 // Started....
16 // Updated.... 07 JUL 2015
17 //
18 // =================================================================================================
19 #include <propeller.h>
20 //#include "jm_touchpads.h"
21 #include "badgetools.h"
22 #include "simpletools.h"
23 /*
24 #ifdef __GNUC__
25 #define INLINE__ static inline
26 //define PostEffect__(X, Y) __extension__({ int32_t tmp__ = (X); (X) = (Y); tmp__; })
27 #else
28 #define INLINE__ static
29 static int32_t tmp__;
30 #define PostEffect__(X, Y) (tmp__ = (X), (X) = (Y), tmp__)
31 #define waitcnt(n) _waitcnt(n)
32 #define coginit(id, code, par) _coginit((unsigned)(par)>>2, (unsigned)(code)>>2, id)
33 #define cognew(code, par) coginit(0x8, (code), (par))
34 #define cogstop(i) _cogstop(i)
35 #endif
36 */
37 static touch badgeTouch;
38 static touch *tself;
39 unsigned char TPPins[7];
40 
41 int32_t touch_start(int32_t count, unsigned char *p_pins, int32_t dms)
42 {
43  tself = &badgeTouch;
44  int32_t _parm__0000[4];
45  _parm__0000[0] = count;
46  _parm__0000[1] = (uint32_t) p_pins;
47  _parm__0000[2] = dms;
48  // Setup object for input of count pins
49  // -- count is # pins to scan
50  // -- p_pins is a pointer to a list of pins (byte array)
51  // * pins in list are arranged MSB to LSB
52  // -- ms is the discharge timing in milliseconds
53  // copy parameters
54  memmove( (void *)&tself->pincount, (void *)&_parm__0000[0], 4*(3));
55  // build mask of input pins
56  tself->pinsmask = 0;
57  {
58  int32_t _limit__0024 = (tself->pincount - 1);
59  int32_t _step__0025 = 1;
60  _parm__0000[3] = 0;
61  if (_parm__0000[3] >= _limit__0024) _step__0025 = -_step__0025;
62  do {
63  tself->pinsmask = tself->pinsmask | (1 << ((uint8_t *)tself->p_pinslist)[((tself->pincount - 1) - _parm__0000[3])]);
64  _parm__0000[3] = _parm__0000[3] + _step__0025;
65  } while (((_step__0025 > 0) && (_parm__0000[3] <= _limit__0024)) || ((_step__0025 < 0) && (_parm__0000[3] >= _limit__0024)));
66  }
67  return 0;
68 }
69 
70 int button( int pad )
71 {
72  char *addr = (char *) tself->p_pinslist;
73  int pin = addr[6-pad];
74  int ctr = 0;
75  for(int i = 0; i < 5; i++)
76  {
77  high(pin);
78  pause(1);
79  input(pin);
80  pause(5);
81  int pb = input(pin);
82  if(pb) ctr++;
83  low(pin);
84  pause(1);
85  }
86  return !(ctr == 5);
87 }
88 
89 
90 int buttons(void)
91 {
92  int pb = 0;
93  int mask = tself->pinsmask;
94  for(int i = 0; i < 5; i++)
95  {
96  //high(pin);
97  OUTA |= mask;
98  DIRA |= mask;
99  pause(1);
100  //input(pin);
101  DIRA &= (~mask);
102  pause(5);
103  //int pb = input(pin);
104  pb |= ~INA;
105  //low(pin);
106  OUTA &= (~mask);
107  DIRA |= mask;
108  pause(1);
109  }
110  char *addr = (char *) tself->p_pinslist;
111  int pads = 0, pin;
112  for(int i = 0; i < 7; i++)
113  {
114  pads <<= 1;
115  pin = addr[i];
116  int bit = (pb >> pin) & 1;
117  pads |= bit;
118  }
119  return pads;
120 }
121 
122 /*
123 
124  Copyright (C) 2015 Jon McPhalen
125 
126  Terms of Use: MIT License
127 
128  Permission is hereby granted, free of charge, to any person obtaining a copy of this
129  software and associated documentation files (the "Software"), to deal in the Software
130  without restriction, including without limitation the rights to use, copy, modify,
131  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
132  permit persons to whom the Software is furnished to do so, subject to the following
133  conditions:
134 
135  The above copyright notice and this permission notice shall be included in all copies
136  or substantial portions of the Software.
137 
138  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
139  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
140  PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
141  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
142  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
143  OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
144 
145  */
OUTA
#define OUTA
Use to set output pin states when corresponding DIRA bits are 1.
Definition: propeller1.h:157
INA
#define INA
Use to read the pins when corresponding DIRA bits are 0.
Definition: propeller1.h:153
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition: libws2812.c:125
low
void low(int pin)
Set an I/O pin to output-low.
Definition: low.c:19
buttons
int buttons(void)
Gets the states of all seven touch buttons, and returns them in a value with 1s and 0s that correspon...
Definition: jm_touchpads.c:90
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
input
int input(int pin)
Set an I/O pin to input and return 1 if pin detects a high signal, or 0 if it detects low.
Definition: input.c:19
count
long count(int pin, long duration)
Count number of low to high transitions an external input applies to an I/O pin over a certain period...
Definition: count.c:19
badgetools.h
This library provides convenient functions for a variety of Parallax eBadge operations.
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
high
void high(int pin)
Set an I/O pin to output-high.
Definition: high.c:19
touch
Definition: badgetools.h:1257
button
int button(int pad)
Gets the state of a touch button (1) pressed, (0) not pressed. Numbering {6, 5, 4,...
Definition: jm_touchpads.c:70