• Yeah!... got my first module running... in the sandbox... anyway... thanks for the posts Writing modular code using "require" at http://forum.espruino.com/conversations/256773. I knew about and used the Web available modules from repository only, so far... Now I can easily QA the modules locally before thinking about publishing for use from the Web.

    I created a folder, registered it in Web IDE Settings - Project - Sandbox, and placed the TOUCH.js module file in the sandbox's modules folder.

    The TOUCH module is now at a stage where I need to combine the TOUCH SCREEN with the LCD for calibration purposes to finish the implementations of the onTouch, onTrack (while touching) and onUp functions. The TOUCH module goes into calibration mode on a very long touch - 5 [s] - with 'no' movement/dragging (<10%) anywhere on the screen. Calibration mode will show spots on the LCD to touch / tap. The spots' x-y coordinates are then formula-mapped to the related, read x-y coordinate values (0.000..0.999) for the calculation of the x-y pixel coordinates of the 240 horizontal x 320 vertical (2.8" TFT 262K Color) LCD.

    The TOUCH module so far:

    // TOUCH.js [0v01] [1v70]
    
    // pins for connect in example: C0,C1,C2,C3
    // callbacks optional on connect 
    function TOUCH(xn,xp,yn,yp,onDownCB,onTrackCB,onUpCB) {
      this.xn = xn; // ...C0
      this.xp = xp; // ...C1
      this.yn = yn; // ...C2
      this.yp = yp; // ...C3
      this.onDownCB  = onDownCB;
      this.onTrackCB = onTrackCB;
      this.onUpCB    = onUpCB;
      this.upCnt = 0;
      this.x = this.x0 = this.x1 = -1;
      this.y = this.y0 = this.y1 = -1;
      this.t = this.t0 = this.t1 =  0;
      this.tc = 0;
    }
    
    var pt = TOUCH.prototype;
    
    // tracking Interval in [ms], upThreshold, required up counts
    pt.C =
    { trkIv: 100
    , upThX: 0.08
    , upThY: 0.05
    , upCnt: 2
    };
    
    pt.listen = function() {
      var _this = this;
      pinMode(this.xn,"input_pulldown");
      digitalRead(this.xp);
      digitalWrite([this.yn,this.yp],3);
      setWatch( function(){ 
        pinMode(_this.xn);
        _this.xy(_this._onDown);
       }, this.xp, {edge:rising, repeat:false} );
    };
    
    pt._onDown = function() {
      this.x0 = this.x1 = this.x; this.y0 = this.y1 = this.y;
      this.tc = this.t0 = this.t1 = this.t;
      if (this.onDownCB) { this.onDownCB(this, this.x, this.y, this.t); }
      this.track();
    };
    
    pt.onTrack = function(onTrackCB,onUpCB) {
      this.onTrackCB = onTrackCB;
      this.onUpCB = onUpCB;
    };
    
    pt.track = function() {
      var _this = this; 
      setTimeout(function(){
        _this.xy(_this._onTrack);
       },this.C.trkIv);
    };
    
    pt._onTrack = function() {
      if ((this.x > this.C.upThX) || (this.y > this.C.upThdY)) {
        this.x1 = this.x; this.y1 = this.y; this.t1 = this.t; this.upCnt = 0;
        if (this.t1 - this.tc > 5000) {
          if (    (Math.abs((this.x0 - this.x1) / this.x0) < 0.1)
               && (Math.abs((this.y0 - this.y1) / this.y0) < 0.1) ) {
            this.tc = -1;
            this.calib();
          } else {
            this.tc = this.t;
          }
        }
        if (this.tc > 0) {
          if (this.onTrackCB) { this.onTrackCB(this, this.x, this.y, this.t); }
          this.track();
        }
      } else {
        if (this.upCnt++ < this.C.upCnt) {
          this.track();
        } else {
          this.x = this.x1; this.y = this.y1; this.t = this.t1;
          if (this.onUpCB) { this.onUpCB(this, this.x, this.y, this.t); }
          this.listen();
        }
      }
    };
    
    pt.xy = function(callback) {
      this.t = new Date().getTime();
      pinMode(this.yn,"input_pulldown");
      digitalRead(this.yp);
      digitalWrite([this.xn,this.xp],2);
      this.x = (analogRead(this.yn)+analogRead(this.yn)+analogRead(this.yn))/3;   
      pinMode(this.xn,"input_pulldown");
      digitalRead(this.xp);
      digitalWrite([this.yn,this.yp],2);
      this.y = (analogRead(this.xn)+analogRead(this.xn)+analogRead(this.xn))/3;
      if (callback) { callback.call(this); }
    };
    
    pt.calib = function() { // ...(empty) placeholder
      console.log("*********** calibrate...");
      var _this = this;
      setTimeout(function(){ _this.resume(); },3000);
    };
    
    pt.resume = function() {
      console.log("...done **********");
      this.listen(); 
    };
    
    exports.connect = function(xn,xp,yn,yp,onDown) {
        var touch = new TOUCH(xn,xp,yn,yp,onDown); touch.listen();
        return touch;
    };
    

    The TOUCH module usage example (sampling/polling interval on tracking/while touching is 100 [ms] / 10Hz / 10 times per second - defined in .C.trkIv constant):

    var cnt = -1, cnt2;
    require("TOUCH").connect(C0,C1,C2,C3, // suggested default ADC pins
      function(touch,x,y,t){ // --- onDown callback function
        cnt2 = 0; console.log(++cnt,cnt2,"onDown:",x,"-",y,"@",t);
        touch.onTrack(
           function(touch,x,y,t){ // --- onTrack callback function
            console.log(cnt,++cnt2,"onTrack:",x,"-",y,"@",t);
         },function(touch,x,y,t){ // --- onUp callback function
            // x, y, t values are the same as touch.x1, .y1, and .t1 values
            console.log(cnt,++cnt2,"onUp:",touch.x0,"-",touch.y0," X ",touch.x1,"-",touch.y1);
            console.log("...in ",touch.t1 - touch.t0,"[ms]");
            touch.xy(); console.log("up:",touch.x,"-",touch.y);
         });
       });
    

    The examples output:

    The first number on line - cnt - counts the touches, second number counts the track events. Lines with 5 dots (.....) indicated removed ('redundandent/boring' ) output to keep post conciser.

    >echo(0);
    =undefined
    
    " top-left (0,0) corner touch "
    0 0 onDown: 0.13021032018 - 0.08894992497 @ 10273559.50381679460
    0 1 onTrack: 0.13037308308 - 0.08862439917 @ 10273666.97709923610
    0 2 onUp: 0.13021032018 - 0.08894992497  X  0.13037308308 - 0.08862439917
    ...in  107.47328244149 [ms]
    up: 0.04728262251 - 0.02799521883
    
    " top-right (239,0) corner touch "
    1 0 onDown: 0.84099590549 - 0.10091299814 @ 10276249.70896946638
    .....
    1 3 onUp: 0.84099590549 - 0.10091299814  X  0.84335596754 - 0.10107576104
    ...in  214.33396946452 [ms]
    
    " bottom-left (0,319) corner touch "
    2 0 onDown: 0.14705628035 - 0.85393555606 @ 10277957.31583969481
    .....
    2 3 onUp: 0.14705628035 - 0.85393555606  X  0.14062714579 - 0.85491213346
    .....
    
    " bottom-right (239,319) corner touch "
    3 0 onDown: 0.84058899824 - 0.84799471020 @ 10279814.59160305373
    .....
    3 2 onUp: 0.84058899824 - 0.84799471020  X  0.84905266905 - 0.84823885455
    .....
    
    " Diagonal touch-drag [top-left (0,0) to bottom-right (239,319)] in 0.5 sec "
    4 0 onDown: 0.14648661020 - 0.09521629663 @ 10283725.73377862572
    4 1 onTrack: 0.25952544441 - 0.22501970956 @ 10283833.24904580228
    4 2 onTrack: 0.46045624475 - 0.42757813890 @ 10283940.03148854896
    4 3 onTrack: 0.64901706467 - 0.66057323058 @ 10284046.82061068713
    4 4 onTrack: 0.76742707459 - 0.77947152920 @ 10284153.65076335892
    4 5 onTrack: 0.80779227384 - 0.82219679051 @ 10284260.44942748174
    4 6 onUp: 0.14648661020 - 0.09521629663  X  0.80779227384 - 0.82219679051
    ...in  534.71564885601 [ms]
    
    " calibration touch/tap of more than 5 secs in the center of the screen "
    5 0 onDown: 0.46851300831 - 0.47632562752 @ 10287615.08874045871
    5 1 onTrack: 0.47518628722 - 0.47193102922 @ 10287722.65458015352
    .....
    .....
    .....
    5 45 onTrack: 0.47510490577 - 0.47543043157 @ 10292422.00858778692
    5 46 onTrack: 0.47616286462 - 0.47559319447 @ 10292528.86354961805
    *********** calibrate...
    ...done **********
    
    " touch in the center of the screen " 
    6 0 onDown: 0.46590880191 - 0.51237760992 @ 10299584.01717557199
    6 1 onTrack: 0.46607156481 - 0.51172655832 @ 10299691.64503816701
    6 2 onUp: 0.46590880191 - 0.51237760992  X  0.46607156481 - 0.51172655832
    > 
    

    Notes:

    1. All short touches / taps were actually longer then the tracking 100[ms] / 0.1[s] interval / 10Hz
    2. onUp: row shows the down and up touch/untouch coordinate values.
    3. The onUp: values are the same as the last onTrack ones...If there is no onTrack event - in other words: the touch was of shorter duration than the tracking interval (of 0.1 [s]) - then the values are the same as the onDown ones.
    4. For first touch the measured (typical) values for up: are shown (not blanked out).
    5. Tracking including the (slow) console output used less than 10% of elapsed time: the code made it to 46 of theoretical 50 track points before entering the calibration (placeholder).
    6. More applications samples - from very simple to quite advanced will be posted at a later time.

    #require #module #localmodule

About

Avatar for allObjects @allObjects started