Trackball based on the NXP LPC11U24 and the ADNS-9500

Dependencies:   ADNS9500 USBDevice mbed 25LCxxx_SPI

Files at this revision

API Documentation at this revision

Comitter:
xxann5
Date:
Sun Jan 06 19:48:26 2013 +0000
Parent:
5:c7056267daa7
Child:
7:d6ee49a89009
Commit message:
Reworked main to allow for a programming mode via USB. Also added licensing to the main.cpp and main.h files

Changed in this revision

PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Sun Jan 06 19:48:26 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- a/USBDevice.lib	Sun Dec 16 18:38:20 2012 +0000
+++ b/USBDevice.lib	Sun Jan 06 19:48:26 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/xxann5/code/USBDevice/#d95e52d47216
+http://mbed.org/users/xxann5/code/USBDevice/#01e3466e25ff
--- a/main.cpp	Sun Dec 16 18:38:20 2012 +0000
+++ b/main.cpp	Sun Jan 06 19:48:26 2013 +0000
@@ -1,62 +1,191 @@
+/* Copyright (c) 2012-2013 Chris Majoros(chris@majoros.us), 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.
+*/
+
 #include "main.h"
 
-/*
- * type, VID, PID, release
- */
- 
-USBMouse mouse( REL_MOUSE, 0x192f, 0x0000, 0x0001 ) ;
+int main(void)
+{
+
+    uint8_t o;
+    uint8_t i;
+    MOUSE_TYPE mtype;
 
-//We declare a USBHID device. By default input and output reports are 64 bytes long.
-USBHID hid(8, 8);
+    if( btn_program ){
+        mtype = RAW;
+        o = 64;
+        i = 64;
+    }
+    else{
+        mtype = REL_MOUSE;
+        o = 0;
+        i = 0;
+    }
 
 
-/* 
- * mosi miso sclk ncs FREQ, motion
- */
-adns9500::ADNS9500 sensor(p5, p6, p7, p8, adns9500::MAX_SPI_FREQUENCY, p21);
+    mouse = new USBMouse( mtype, 0x192f, 0x0000, 0x0001, o, i ) ;
+
+    if( mtype == RAW ){
+        program();
+    }
+    else{
+        track();
+    }
+}
+
+void track(){
+    /* 
+    * mosi miso sclk ncs FREQ, motion
+    */
+    sensor = new adns9500::ADNS9500(p5, p6, p7, p8, adns9500::MAX_SPI_FREQUENCY, p21);
+    
+    send_rep.length = 8;
+    
+    btn_hr.mode(PullUp);
+    btn_hr.attach_asserted(&btn_hr_press);
+    btn_hr.attach_deasserted(&btn_hr_release);
+    btn_hr.setSampleFrequency();
+
+    btn_z.mode(PullUp);
+    btn_z.attach_asserted(&btn_z_press);
+    btn_z.attach_deasserted(&btn_z_release);
+    btn_z.setSampleFrequency();
+/*
+    btn_l.mode(PullUp);
+    btn_l.attach_asserted(&btn_l_press);
+    btn_l.attach_deasserted(&btn_l_release);
+    btn_l.setSampleFrequency();
+*/
+    btn_m.mode(PullUp);
+    btn_m.attach_asserted(&btn_m_press);
+    btn_m.attach_deasserted(&btn_m_release);
+    btn_m.setSampleFrequency();
+
+    btn_r.mode(PullUp);
+    btn_r.attach_asserted(&btn_r_press);
+    btn_r.attach_deasserted(&btn_r_release);
+    btn_r.setSampleFrequency();
+    //btn_r.setAssertValue( 0 );
+    //btn_r.setSamplesTillAssert( 5 );
+    
+    int dx, dy;
 
-void motionCallback()
-{
+    sensor->attach(&motionCallback);
+    
+    sensor->reset();
+    
+    uint16_t crc = sensor->sromDownload(adns9500FWArray, (uint16_t)ADNS9500_FIRMWARE_LEN );
+      
+    if( (uint16_t)ADNS6010_FIRMWARE_CRC != crc )
+    {
+        error( "CRC does not match: [%x] [%x], Exiting.\r\n", (uint16_t)ADNS6010_FIRMWARE_CRC, crc );
+    }
+
+    sensor->enableLaser();
+    
+    sensor->setResolution( default_motion_cpi );
+    int btn_l_avg = 0;
+    while (true){
+
+        btn_l_avg=(btn_l_avg * 9 + btn_l * 10)/10;
+        if (btn_l_avg > 5){
+            btn_l_press();
+        }
+        else{
+            btn_l_release();
+        }
+
+        
+        if( motion_triggered ){
+            led1 = !led1;
+            motion_triggered = false;
+
+            sensor->getMotionDelta(dx, dy);
+
+            /*
+             * The sensor does not know its upside down and backwords
+             * so we are helping it out with the y axis.
+             */
+            if( z_axis_active ){
+                mouse->scroll( - dy );
+            }
+            else{
+                mouse->move( dx, - dy ); 
+            }
+        }
+    }
+}
+
+void program(){
+    led3 = !led3;
+               switch (recv_rep.data[0]){
+                    case CHAT_SET:
+                        set(recv_rep.data[1],&recv_rep.data[2]);
+                        break;
+                    case CHAT_GET:
+                        //get(recv_rep.data[1]);
+                        break;
+                default:
+                    // FIXME: error handling.
+                    printf("crap\r\n");
+            }
+}
+
+void motionCallback(){
     motion_triggered = true;
 }
 
 void btn_hr_press(){
     high_rez_active = true;
-    sensor.setResolution( default_hirez_cpi ); 
+    sensor->setResolution( default_hirez_cpi ); 
 }
 void btn_hr_release(){
     high_rez_active = false;
-    sensor.setResolution( default_motion_cpi );
+    sensor->setResolution( default_motion_cpi );
 }
 
 void btn_z_press(){
     z_axis_active = true;
-    sensor.setResolution( default_z_cpi );
+    sensor->setResolution( default_z_cpi );
 }
 void btn_z_release(){
     z_axis_active = false;
-    sensor.setResolution( default_motion_cpi );
+    sensor->setResolution( default_motion_cpi );
 }
 
 void btn_l_press(){
-    mouse.press(MOUSE_LEFT);
+    mouse->press(MOUSE_LEFT);
 }
 void btn_l_release(){
-    //mouse.release(MOUSE_LEFT);
+    mouse->release(MOUSE_LEFT);
 }
 
 void btn_m_press(){
-    //mouse.press(MOUSE_MIDDLE);
+    mouse->press(MOUSE_MIDDLE);
 }
 void btn_m_release(){
-    //mouse.release(MOUSE_MIDDLE);
+    mouse->release(MOUSE_MIDDLE);
 }
 
 void btn_r_press(){
-    //mouse.press(MOUSE_RIGHT);
+    mouse->press(MOUSE_RIGHT);
 }
 void btn_r_release(){
-    //mouse.release(MOUSE_RIGHT);
+    mouse->release(MOUSE_RIGHT);
 }
 
 void set( uint8_t attrib, uint8_t *val ){
@@ -73,81 +202,3 @@
     }
 }
 
-void get( uint8_t attrib ){
-}
-
-int main(void)
-{
-
-    send_rep.length = 8;
-    
-    btn_hr.rise(&btn_hr_press);
-    btn_hr.fall(&btn_hr_release);
-
-    btn_z.rise(&btn_z_press);
-    btn_z.fall(&btn_z_release);
-
-    btn_l.rise(&btn_l_press);
-    btn_l.fall(&btn_l_release);
-
-    btn_m.rise(&btn_m_press);
-    btn_m.fall(&btn_m_release);
-
-    btn_r.rise(&btn_r_press);
-    btn_r.fall(&btn_r_release);
-    
-    int dx, dy;
-
-    sensor.attach(&motionCallback);
-    
-    sensor.reset();
-    
-    uint16_t crc = sensor.sromDownload(adns9500FWArray, (uint16_t)ADNS9500_FIRMWARE_LEN );
-      
-    if( (uint16_t)ADNS6010_FIRMWARE_CRC != crc )
-    {
-        error( "CRC does not match: [%x] [%x], Exiting.\r\n", (uint16_t)ADNS6010_FIRMWARE_CRC, crc );
-    }
-
-    sensor.enableLaser();
-    
-    sensor.setResolution( default_motion_cpi );
-    
-    while (true)
-    {
-        //try to read a msg
-        if(hid.readNB(&recv_rep)) {
-            led4 = !led4;
-            switch (recv_rep.data[0]){
-                case CHAT_SET:
-                    set(recv_rep.data[1],&recv_rep.data[2]);
-                    break;
-                case CHAT_GET:
-                    get(recv_rep.data[1]);
-                    break;
-                default:
-                    // FIXME: error handling.
-                    printf("crap\r\n");
-            }
-        }
-            
-        if (motion_triggered) {
-            led1 = !led1;
-            motion_triggered = false;
-            
-            sensor.getMotionDelta(dx, dy);
-
-            /*
-             * The sensor does not know its upside down and backwords
-             * so we are helping it out with the y axis.
-             */
-            if( z_axis_active ){
-                //////mouse.scroll( - dy );
-            }
-            else{
-                //////mouse.move( dx, - dy ); 
-            }
-        }
-    }
-}
-
--- a/main.h	Sun Dec 16 18:38:20 2012 +0000
+++ b/main.h	Sun Jan 06 19:48:26 2013 +0000
@@ -1,9 +1,36 @@
+/* Copyright (c) 2012-2013 Chris Majoros(chris@majoros.us), 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.
+*/
+
 #include "mbed.h"
 #include "USBHID.h"
 #include "USBMouse.h"
+
 #include <math.h>
 #include <stdint.h>
 
+
+#define PINDETECT_PIN_ASSTERED  0    //setAssertValue
+#define PINDETECT_SAMPLE_PERIOD 100 //setSampleFrequency
+#define PINDETECT_ASSERT_COUNT  10    //setSamplesTillAssert
+#define PINDETECT_HOLD_COUNT    500
+
+#include "PinDetect.h"
+
 #define ADNS9500_SROM_91
 
 #define DEBUG
@@ -28,6 +55,9 @@
 
 #define CHAT_LED_BEHAVIOR       0x0a
 
+#define CHAT_BTN_TICKER_FREQ    0x0b
+#define CHAT_BTN_ASSERT_COUNT   0x0c
+
 
   
 //This report will contain data to be sent
@@ -39,11 +69,17 @@
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 
-InterruptIn btn_hr(p16);
-InterruptIn btn_z(p17);
-InterruptIn btn_l(p18);
-InterruptIn btn_m(p19);
-InterruptIn btn_r(p20);
+PinDetect btn_hr(p16);
+PinDetect btn_z(p17);
+DigitalIn btn_l(p18);
+PinDetect btn_m(p19);
+PinDetect btn_r(p20);
+
+
+DigitalIn btn_program(p22);
+
+adns9500::ADNS9500 *sensor;
+USBMouse *mouse;
 
 bool motion_triggered = false;
 bool z_axis_active    = false;
@@ -53,9 +89,25 @@
 int default_z_cpi      = 90;
 int default_hirez_cpi  = 180;
 
+void track();
+void program();
 
+void motionCallback( void );
+
+void btn_hr_press( void );
+void btn_hr_release( void );
 
+void btn_z_press( void );
+void btn_z_release( void );
 
+void btn_l_press(void);
+void btn_l_release( void );
+
+void btn_m_press( void );
+void btn_m_release( void );
+
+void btn_r_press( void );
+void btn_r_release( void );
 
 void set( uint8_t attrib, uint8_t *val );
 void get( uint8_t attrib);
\ No newline at end of file