Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

introducing touchtest, driver for the prototype

- based on capsense.
- works with four channels.
  • Loading branch information...
commit aa3741e6cf22e2be5e20c167f11c744adb49cc92 1 parent 3e10828
Giles Hall authored

Showing 2 changed files with 97 additions and 0 deletions. Show diff stats Hide diff stats

  1. +1 0  .gitignore
  2. +96 0 touchtest/touchtest.pde
1  .gitignore
... ... @@ -0,0 +1 @@
  1 +.*.swp
96 touchtest/touchtest.pde
... ... @@ -0,0 +1,96 @@
  1 +#include <CapSense.h>
  2 +#include <string.h>
  3 +
  4 +class Average
  5 +{
  6 +public:
  7 + Average(uint8_t sz) :
  8 + _sz(sz), _idx(0)
  9 + {
  10 + _data = (long*)malloc(_sz * sizeof(long));
  11 + //bzero(_data, _sz * sizeof(long));
  12 + }
  13 +
  14 + void push(long val)
  15 + {
  16 + _data[_idx] = val;
  17 + _idx = (_idx + 1) % _sz;
  18 + }
  19 +
  20 + long pull()
  21 + {
  22 + long sum = 0;
  23 + for(uint8_t sum_idx = 0; sum_idx < _sz; ++sum_idx)
  24 + {
  25 + sum += _data[sum_idx];
  26 + }
  27 + return (sum / ((float)_sz));
  28 + }
  29 +
  30 + void set(uint8_t offset)
  31 + {
  32 + _level = pull() + offset;
  33 + }
  34 +
  35 + bool trigger()
  36 + {
  37 + return (pull() > _level);
  38 + }
  39 +
  40 +private:
  41 + uint8_t _sz;
  42 + uint8_t _idx;
  43 + long* _data;
  44 + long _level;
  45 +};
  46 +
  47 +CapSense cs_4_8 = CapSense(4,8);
  48 +Average avg_4_8 = Average(10);
  49 +CapSense cs_4_9 = CapSense(4,9);
  50 +Average avg_4_9 = Average(10);
  51 +CapSense cs_4_10 = CapSense(4,10);
  52 +Average avg_4_10 = Average(10);
  53 +CapSense cs_4_11 = CapSense(4,11);
  54 +Average avg_4_11 = Average(10);
  55 +
  56 +void sample()
  57 +{
  58 + avg_4_8.push(cs_4_8.capSense(50));
  59 + avg_4_9.push(cs_4_9.capSense(50));
  60 + avg_4_10.push(cs_4_10.capSense(50));
  61 + avg_4_11.push(cs_4_11.capSense(50));
  62 +}
  63 +
  64 +void setup()
  65 +{
  66 + Serial.begin(9600);
  67 + for(uint8_t x = 0; x < 100; ++x)
  68 + {
  69 + sample();
  70 + }
  71 + avg_4_8.set(25);
  72 + avg_4_9.set(25);
  73 + avg_4_10.set(25);
  74 + avg_4_11.set(25);
  75 +}
  76 +
  77 +void loop()
  78 +{
  79 + sample();
  80 + if (avg_4_8.trigger())
  81 + {
  82 + Serial.println("up");
  83 + }
  84 + if (avg_4_9.trigger())
  85 + {
  86 + Serial.println("down");
  87 + }
  88 + if (avg_4_10.trigger())
  89 + {
  90 + Serial.println("left");
  91 + }
  92 + if (avg_4_11.trigger())
  93 + {
  94 + Serial.println("right");
  95 + }
  96 +}

0 comments on commit aa3741e

Please sign in to comment.
Something went wrong with that request. Please try again.