Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
pat9125_mbed/pat9125_mbed.cpp@15:089f1d28d152, 2017-10-20 (annotated)
- Committer:
- pixus_mbed
- Date:
- Fri Oct 20 10:58:39 2017 +0000
- Revision:
- 15:089f1d28d152
- Parent:
- 5:61318505e528
1. Separate LCD and UART mode.; 2. Configure BTN with default pull-up.; 3. NULL handle.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pixus_mbed | 0:411244c71423 | 1 | |
| pixus_mbed | 0:411244c71423 | 2 | #include "pat9125_mbed.h" |
| pixus_mbed | 0:411244c71423 | 3 | #define delay(ms) wait_ms(ms) |
| pixus_mbed | 0:411244c71423 | 4 | |
| pixus_mbed | 0:411244c71423 | 5 | //define OTS state - X |
| pixus_mbed | 0:411244c71423 | 6 | #define OTS_ROT_NO_CHANGE 0x00 |
| pixus_mbed | 0:411244c71423 | 7 | #define OTS_ROT_UP 0x01 |
| pixus_mbed | 0:411244c71423 | 8 | #define OTS_ROT_DOWN 0x02 |
| pixus_mbed | 0:411244c71423 | 9 | |
| pixus_mbed | 0:411244c71423 | 10 | //define downscale factor for rotation of shaft |
| pixus_mbed | 0:411244c71423 | 11 | #define EXPECTED_COUNT_PER_ROUND 360 |
| pixus_mbed | 0:411244c71423 | 12 | #define REAL_AVG_COUNT_PER_ROUND 446 //base on: sensor Reg0x0d=0x65, shaft diameter=2mm, sensor-to-shaft distance=2mm |
| pixus_mbed | 0:411244c71423 | 13 | |
| pixus_mbed | 0:411244c71423 | 14 | //define OTS state - Y |
| pixus_mbed | 0:411244c71423 | 15 | #define OTS_BTN_NO_CHANGE 0x00 |
| pixus_mbed | 0:411244c71423 | 16 | #define OTS_BTN_RELEASE 0x01 |
| pixus_mbed | 0:411244c71423 | 17 | #define OTS_BTN_PRESS 0x02 |
| pixus_mbed | 0:411244c71423 | 18 | |
| pixus_mbed | 0:411244c71423 | 19 | #define LOW 0 |
| pixus_mbed | 0:411244c71423 | 20 | #define HIGH 1 |
| pixus_mbed | 0:411244c71423 | 21 | #define digitalRead(pin) *pin |
| pixus_mbed | 0:411244c71423 | 22 | |
| pixus_mbed | 0:411244c71423 | 23 | static pat9125_mbed_state_s *gp_state ; |
| pixus_mbed | 0:411244c71423 | 24 | |
| pixus_mbed | 0:411244c71423 | 25 | #define PIN_BTN_L gp_state->pBTN_L |
| pixus_mbed | 0:411244c71423 | 26 | #define PIN_BTN_R gp_state->pBTN_R |
| pixus_mbed | 0:411244c71423 | 27 | #define PIN_SEN_MOTION gp_state->pINT |
| pixus_mbed | 0:411244c71423 | 28 | |
| pixus_mbed | 0:411244c71423 | 29 | #define PIN_RLED gp_state->pRLED |
| pixus_mbed | 0:411244c71423 | 30 | #define PIN_GLED gp_state->pGLED |
| pixus_mbed | 0:411244c71423 | 31 | #define digitalWrite(pin,level) *pin = level |
| pixus_mbed | 0:411244c71423 | 32 | #define LED_RED_ON digitalWrite(PIN_RLED,LOW) |
| pixus_mbed | 0:411244c71423 | 33 | #define LED_RED_OFF digitalWrite(PIN_RLED,HIGH) |
| pixus_mbed | 0:411244c71423 | 34 | #define LED_GREEN_ON digitalWrite(PIN_GLED,LOW) |
| pixus_mbed | 0:411244c71423 | 35 | #define LED_GREEN_OFF digitalWrite(PIN_GLED,HIGH) |
| pixus_mbed | 0:411244c71423 | 36 | #define attachInterrupt(pin,b,c) //pin->enable_irq() |
| pixus_mbed | 0:411244c71423 | 37 | #define digitalPinToInterrupt(pin) pin |
| pixus_mbed | 0:411244c71423 | 38 | #define detachInterrupt(pin) //pin->disable_irq() |
| pixus_mbed | 15:089f1d28d152 | 39 | //#define LCM_DisplayString_Reset gp_state->pLCM->LCM_DisplayString_Reset |
| pixus_mbed | 15:089f1d28d152 | 40 | //#define LCM_DisplayDecimal(a,b,c,d) gp_state->pLCM->LCM_DisplayDecimal(a,b,c,d) |
| pixus_mbed | 15:089f1d28d152 | 41 | //#define LCM_DisplayString(a,b,c) gp_state->pLCM->LCM_DisplayString(a,b,c) |
| pixus_mbed | 0:411244c71423 | 42 | |
| pixus_mbed | 0:411244c71423 | 43 | #define I2C_RESET gp_state->p_i2c = gp_state->p_i2c->reset(); //workaround for nRF51 mbed |
| pixus_mbed | 5:61318505e528 | 44 | |
| pixus_mbed | 5:61318505e528 | 45 | |
| pixus_mbed | 15:089f1d28d152 | 46 | static void LCM_DisplayString(unsigned char line, unsigned char position, const char *ptr) |
| pixus_mbed | 15:089f1d28d152 | 47 | { |
| pixus_mbed | 15:089f1d28d152 | 48 | if(gp_state->pLCM == NULL) return ; |
| pixus_mbed | 15:089f1d28d152 | 49 | gp_state->pLCM->LCM_DisplayString(line, position, ptr); |
| pixus_mbed | 15:089f1d28d152 | 50 | } |
| pixus_mbed | 15:089f1d28d152 | 51 | static void LCM_DisplayDecimal(unsigned char line, unsigned char position, unsigned int hex_word, unsigned char digits) |
| pixus_mbed | 15:089f1d28d152 | 52 | { |
| pixus_mbed | 15:089f1d28d152 | 53 | if(gp_state->pLCM == NULL) return ; |
| pixus_mbed | 15:089f1d28d152 | 54 | gp_state->pLCM->LCM_DisplayDecimal(line, position, hex_word, digits) ; |
| pixus_mbed | 15:089f1d28d152 | 55 | } |
| pixus_mbed | 15:089f1d28d152 | 56 | static void LCM_DisplayString_Reset(void) |
| pixus_mbed | 15:089f1d28d152 | 57 | { |
| pixus_mbed | 15:089f1d28d152 | 58 | if(gp_state->pLCM == NULL) return ; |
| pixus_mbed | 15:089f1d28d152 | 59 | gp_state->pLCM->LCM_DisplayString_Reset(); |
| pixus_mbed | 15:089f1d28d152 | 60 | } |
| pixus_mbed | 15:089f1d28d152 | 61 | |
| pixus_mbed | 5:61318505e528 | 62 | unsigned char xy2uart_enh=0; |
| pixus_mbed | 5:61318505e528 | 63 | |
| pixus_mbed | 0:411244c71423 | 64 | //for OTS |
| pixus_mbed | 0:411244c71423 | 65 | signed int deltaX16; |
| pixus_mbed | 0:411244c71423 | 66 | signed int deltaY16; |
| pixus_mbed | 0:411244c71423 | 67 | unsigned char OTS_ROT_Status; |
| pixus_mbed | 0:411244c71423 | 68 | unsigned char OTS_BTN_Status; |
| pixus_mbed | 0:411244c71423 | 69 | |
| pixus_mbed | 0:411244c71423 | 70 | signed long x_sum=0; |
| pixus_mbed | 0:411244c71423 | 71 | signed long ds_x_sum=0; |
| pixus_mbed | 0:411244c71423 | 72 | signed long pre_dsCountX=0; |
| pixus_mbed | 0:411244c71423 | 73 | unsigned int OTS_BTN_Press_Cnt=0; |
| pixus_mbed | 0:411244c71423 | 74 | |
| pixus_mbed | 0:411244c71423 | 75 | volatile unsigned char MotionPinEventTriggered=0; |
| pixus_mbed | 0:411244c71423 | 76 | |
| pixus_mbed | 0:411244c71423 | 77 | // Register write function |
| pixus_mbed | 0:411244c71423 | 78 | void OTS_Write_Reg(unsigned char address, unsigned char value) |
| pixus_mbed | 0:411244c71423 | 79 | { |
| pixus_mbed | 0:411244c71423 | 80 | int ret ; |
| pixus_mbed | 0:411244c71423 | 81 | char data_write[2]; |
| pixus_mbed | 0:411244c71423 | 82 | |
| pixus_mbed | 0:411244c71423 | 83 | data_write[0] = address; |
| pixus_mbed | 0:411244c71423 | 84 | data_write[1] = value; |
| pixus_mbed | 0:411244c71423 | 85 | ret = gp_state->p_i2c->write(gp_state->slave_id, data_write, 2, 0); |
| pixus_mbed | 0:411244c71423 | 86 | } |
| pixus_mbed | 0:411244c71423 | 87 | |
| pixus_mbed | 0:411244c71423 | 88 | // Register Read function |
| pixus_mbed | 0:411244c71423 | 89 | unsigned char OTS_Read_Reg(unsigned char address) |
| pixus_mbed | 0:411244c71423 | 90 | { |
| pixus_mbed | 0:411244c71423 | 91 | unsigned char rdata = 0; |
| pixus_mbed | 0:411244c71423 | 92 | gp_state->p_i2c->write(gp_state->slave_id, (char *)&address, 1, 0); |
| pixus_mbed | 0:411244c71423 | 93 | gp_state->p_i2c->read(gp_state->slave_id, (char *)&rdata, 1, 0); |
| pixus_mbed | 0:411244c71423 | 94 | |
| pixus_mbed | 0:411244c71423 | 95 | return(rdata); |
| pixus_mbed | 0:411244c71423 | 96 | } |
| pixus_mbed | 0:411244c71423 | 97 | |
| pixus_mbed | 0:411244c71423 | 98 | // Register write & read back check function |
| pixus_mbed | 0:411244c71423 | 99 | void OTS_WriteRead_Reg(unsigned char address, unsigned char wdata) |
| pixus_mbed | 0:411244c71423 | 100 | { |
| pixus_mbed | 0:411244c71423 | 101 | unsigned char rdata; |
| pixus_mbed | 0:411244c71423 | 102 | do |
| pixus_mbed | 0:411244c71423 | 103 | { |
| pixus_mbed | 0:411244c71423 | 104 | OTS_Write_Reg(address, wdata); // Write data to specified address |
| pixus_mbed | 0:411244c71423 | 105 | rdata = OTS_Read_Reg(address); // Read back previous written data |
| pixus_mbed | 0:411244c71423 | 106 | } while(rdata != wdata); // Check if the data is correctly written |
| pixus_mbed | 0:411244c71423 | 107 | |
| pixus_mbed | 0:411244c71423 | 108 | } |
| pixus_mbed | 0:411244c71423 | 109 | |
| pixus_mbed | 0:411244c71423 | 110 | boolean OTS_Sensor_Init(void) |
| pixus_mbed | 0:411244c71423 | 111 | { |
| pixus_mbed | 0:411244c71423 | 112 | unsigned char sensor_pid=0; |
| pixus_mbed | 0:411244c71423 | 113 | boolean read_id_ok=false; |
| pixus_mbed | 0:411244c71423 | 114 | |
| pixus_mbed | 0:411244c71423 | 115 | // Read sensor_pid in address 0x00 to check if the serial link is valid, PID should be 0x31 |
| pixus_mbed | 0:411244c71423 | 116 | sensor_pid = OTS_Read_Reg(0x00); |
| pixus_mbed | 0:411244c71423 | 117 | if(sensor_pid == 0x31) |
| pixus_mbed | 0:411244c71423 | 118 | { |
| pixus_mbed | 0:411244c71423 | 119 | read_id_ok = true; |
| pixus_mbed | 0:411244c71423 | 120 | |
| pixus_mbed | 0:411244c71423 | 121 | //PAT9125 sensor recommended settings as below: |
| pixus_mbed | 0:411244c71423 | 122 | OTS_Write_Reg(0x7F, 0x00); // switch to bank0, not allowed to perform OTS_WriteRead_Reg |
| pixus_mbed | 0:411244c71423 | 123 | OTS_Write_Reg(0x06, 0x97); // Software Reset (i.e. set bit7 to 1), then it will reset to 0 automatically |
| pixus_mbed | 0:411244c71423 | 124 | |
| pixus_mbed | 0:411244c71423 | 125 | I2C_RESET; |
| pixus_mbed | 0:411244c71423 | 126 | |
| pixus_mbed | 0:411244c71423 | 127 | delay(1); // delay 1ms |
| pixus_mbed | 0:411244c71423 | 128 | OTS_Write_Reg(0x06, 0x17); // ensure the sensor has left the reset state. |
| pixus_mbed | 0:411244c71423 | 129 | |
| pixus_mbed | 0:411244c71423 | 130 | OTS_WriteRead_Reg(0x09, 0x5A); // disable write protect |
| pixus_mbed | 0:411244c71423 | 131 | OTS_WriteRead_Reg(0x0D, 0x65); // set X-axis resolution (depends on application) |
| pixus_mbed | 0:411244c71423 | 132 | OTS_WriteRead_Reg(0x0E, 0xFF); // set Y-axis resolution (depends on application) |
| pixus_mbed | 0:411244c71423 | 133 | OTS_WriteRead_Reg(0x19, 0x04); // set 12-bit X/Y data format (depends on application) |
| pixus_mbed | 0:411244c71423 | 134 | //OTS_WriteRead_Reg(0x4B, 0x04); // ONLY for VDD=VDDA=1.7~1.9V: for power saving |
| pixus_mbed | 0:411244c71423 | 135 | |
| pixus_mbed | 0:411244c71423 | 136 | OTS_WriteRead_Reg(0x09, 0x00); // enable write protect |
| pixus_mbed | 0:411244c71423 | 137 | } |
| pixus_mbed | 0:411244c71423 | 138 | |
| pixus_mbed | 0:411244c71423 | 139 | return read_id_ok; |
| pixus_mbed | 0:411244c71423 | 140 | } |
| pixus_mbed | 0:411244c71423 | 141 | |
| pixus_mbed | 0:411244c71423 | 142 | // Read motion |
| pixus_mbed | 0:411244c71423 | 143 | void OTS_Read_Motion(signed int *dx16, signed int *dy16) |
| pixus_mbed | 0:411244c71423 | 144 | { |
| pixus_mbed | 0:411244c71423 | 145 | int shift = (sizeof(signed int) << 3) - 12 ; |
| pixus_mbed | 0:411244c71423 | 146 | signed int deltaX_l=0, deltaY_l=0, deltaXY_h=0; |
| pixus_mbed | 0:411244c71423 | 147 | signed int deltaX_h=0, deltaY_h=0; |
| pixus_mbed | 0:411244c71423 | 148 | char motion = OTS_Read_Reg(0x02) ; |
| pixus_mbed | 0:411244c71423 | 149 | if(motion & 0x80) //check motion bit in bit7 |
| pixus_mbed | 0:411244c71423 | 150 | { |
| pixus_mbed | 0:411244c71423 | 151 | deltaX_l = OTS_Read_Reg(0x03); |
| pixus_mbed | 0:411244c71423 | 152 | deltaY_l = OTS_Read_Reg(0x04); |
| pixus_mbed | 0:411244c71423 | 153 | deltaXY_h = OTS_Read_Reg(0x12); |
| pixus_mbed | 0:411244c71423 | 154 | |
| pixus_mbed | 0:411244c71423 | 155 | deltaX_h = (deltaXY_h<<4) & 0xF00; |
| pixus_mbed | 0:411244c71423 | 156 | deltaX_h = (deltaX_h << shift) >> shift ; |
| pixus_mbed | 0:411244c71423 | 157 | //if(deltaX_h & 0x800) deltaX_h |= 0xfffff000; // 12-bit data convert to 16-bit |
| pixus_mbed | 0:411244c71423 | 158 | |
| pixus_mbed | 0:411244c71423 | 159 | deltaY_h = (deltaXY_h<<8) & 0xF00; |
| pixus_mbed | 0:411244c71423 | 160 | //if(deltaY_h & 0x800) deltaY_h |= 0xfffff000; // 12-bit data convert to 16-bit |
| pixus_mbed | 0:411244c71423 | 161 | deltaY_h = (deltaY_h << shift) >> shift ; |
| pixus_mbed | 0:411244c71423 | 162 | |
| pixus_mbed | 0:411244c71423 | 163 | } |
| pixus_mbed | 0:411244c71423 | 164 | *dx16 = -(deltaX_h | deltaX_l); //inverse the data (depends on sensor's orientation and application) |
| pixus_mbed | 0:411244c71423 | 165 | *dy16 = -(deltaY_h | deltaY_l); //inverse the data (depends on sensor's orientation and application) |
| pixus_mbed | 0:411244c71423 | 166 | } |
| pixus_mbed | 0:411244c71423 | 167 | |
| pixus_mbed | 0:411244c71423 | 168 | void OTS_Reset_Variables(void) |
| pixus_mbed | 0:411244c71423 | 169 | { |
| pixus_mbed | 0:411244c71423 | 170 | //reset variables |
| pixus_mbed | 0:411244c71423 | 171 | x_sum=0; |
| pixus_mbed | 0:411244c71423 | 172 | ds_x_sum=0; |
| pixus_mbed | 0:411244c71423 | 173 | pre_dsCountX=0; |
| pixus_mbed | 0:411244c71423 | 174 | |
| pixus_mbed | 0:411244c71423 | 175 | OTS_BTN_Press_Cnt=0; |
| pixus_mbed | 0:411244c71423 | 176 | LCM_DisplayString_Reset(); |
| pixus_mbed | 0:411244c71423 | 177 | } |
| pixus_mbed | 0:411244c71423 | 178 | |
| pixus_mbed | 0:411244c71423 | 179 | unsigned char Detect_Rotation(signed long dsCountX) |
| pixus_mbed | 0:411244c71423 | 180 | { |
| pixus_mbed | 0:411244c71423 | 181 | #define EVENT_NUM_PER_ROUND 360//10 |
| pixus_mbed | 0:411244c71423 | 182 | #define EVENT_COUNT_TH (EXPECTED_COUNT_PER_ROUND / EVENT_NUM_PER_ROUND) //360/10=36 //360/360=1 |
| pixus_mbed | 0:411244c71423 | 183 | |
| pixus_mbed | 0:411244c71423 | 184 | signed long diff_count = 0; |
| pixus_mbed | 0:411244c71423 | 185 | unsigned char OutRotState = OTS_ROT_NO_CHANGE; |
| pixus_mbed | 0:411244c71423 | 186 | |
| pixus_mbed | 0:411244c71423 | 187 | diff_count = dsCountX - pre_dsCountX; |
| pixus_mbed | 0:411244c71423 | 188 | if( diff_count >= EVENT_COUNT_TH ) |
| pixus_mbed | 0:411244c71423 | 189 | { |
| pixus_mbed | 0:411244c71423 | 190 | pre_dsCountX = dsCountX; |
| pixus_mbed | 0:411244c71423 | 191 | OutRotState = OTS_ROT_UP; |
| pixus_mbed | 0:411244c71423 | 192 | } |
| pixus_mbed | 0:411244c71423 | 193 | else if( diff_count <= (-EVENT_COUNT_TH) ) |
| pixus_mbed | 0:411244c71423 | 194 | { |
| pixus_mbed | 0:411244c71423 | 195 | pre_dsCountX = dsCountX; |
| pixus_mbed | 0:411244c71423 | 196 | OutRotState = OTS_ROT_DOWN; |
| pixus_mbed | 0:411244c71423 | 197 | } |
| pixus_mbed | 0:411244c71423 | 198 | |
| pixus_mbed | 0:411244c71423 | 199 | return OutRotState; |
| pixus_mbed | 0:411244c71423 | 200 | } |
| pixus_mbed | 0:411244c71423 | 201 | |
| pixus_mbed | 0:411244c71423 | 202 | signed long OTS_Resolution_Downscale(signed int delta_count) |
| pixus_mbed | 0:411244c71423 | 203 | { |
| pixus_mbed | 0:411244c71423 | 204 | x_sum += delta_count; |
| pixus_mbed | 0:411244c71423 | 205 | return (x_sum * EXPECTED_COUNT_PER_ROUND / REAL_AVG_COUNT_PER_ROUND); |
| pixus_mbed | 0:411244c71423 | 206 | } |
| pixus_mbed | 0:411244c71423 | 207 | |
| pixus_mbed | 0:411244c71423 | 208 | unsigned char OTS_Detect_Rotation(signed int dx16, signed int dy16) |
| pixus_mbed | 0:411244c71423 | 209 | { |
| pixus_mbed | 0:411244c71423 | 210 | ds_x_sum = OTS_Resolution_Downscale(dx16); |
| pixus_mbed | 0:411244c71423 | 211 | LCM_DisplayDecimal(1,12,ds_x_sum,4);//show downscale value |
| pixus_mbed | 0:411244c71423 | 212 | |
| pixus_mbed | 0:411244c71423 | 213 | return Detect_Rotation(ds_x_sum); |
| pixus_mbed | 0:411244c71423 | 214 | } |
| pixus_mbed | 0:411244c71423 | 215 | |
| pixus_mbed | 0:411244c71423 | 216 | unsigned char OTS_Detect_Pressing(signed int dx16, signed int dy16) |
| pixus_mbed | 0:411244c71423 | 217 | { |
| pixus_mbed | 0:411244c71423 | 218 | #define PRESS 1 |
| pixus_mbed | 0:411244c71423 | 219 | #define RELEASE 0 |
| pixus_mbed | 0:411244c71423 | 220 | |
| pixus_mbed | 0:411244c71423 | 221 | #define DX_ROTATE_TH 2 |
| pixus_mbed | 0:411244c71423 | 222 | #define DY_VALID_TH 1 |
| pixus_mbed | 0:411244c71423 | 223 | #define ACCY_PRESS_TH 5 |
| pixus_mbed | 0:411244c71423 | 224 | #define DY_RELEASE_TH (-2) |
| pixus_mbed | 0:411244c71423 | 225 | |
| pixus_mbed | 0:411244c71423 | 226 | unsigned char OutBtnState = OTS_BTN_NO_CHANGE; |
| pixus_mbed | 0:411244c71423 | 227 | static signed long AccY = 0; |
| pixus_mbed | 0:411244c71423 | 228 | static unsigned char State = RELEASE; //0:release, 1:press |
| pixus_mbed | 0:411244c71423 | 229 | |
| pixus_mbed | 0:411244c71423 | 230 | if((dx16 >= DX_ROTATE_TH)||(dx16 <= (-DX_ROTATE_TH))) |
| pixus_mbed | 0:411244c71423 | 231 | { |
| pixus_mbed | 0:411244c71423 | 232 | AccY = 0; |
| pixus_mbed | 0:411244c71423 | 233 | } |
| pixus_mbed | 0:411244c71423 | 234 | else |
| pixus_mbed | 0:411244c71423 | 235 | { |
| pixus_mbed | 0:411244c71423 | 236 | if(State == PRESS) |
| pixus_mbed | 0:411244c71423 | 237 | { |
| pixus_mbed | 0:411244c71423 | 238 | if(dy16 <= DY_RELEASE_TH) |
| pixus_mbed | 0:411244c71423 | 239 | { |
| pixus_mbed | 0:411244c71423 | 240 | State = RELEASE; |
| pixus_mbed | 0:411244c71423 | 241 | OutBtnState = OTS_BTN_RELEASE; |
| pixus_mbed | 0:411244c71423 | 242 | } |
| pixus_mbed | 0:411244c71423 | 243 | } |
| pixus_mbed | 0:411244c71423 | 244 | else |
| pixus_mbed | 0:411244c71423 | 245 | { |
| pixus_mbed | 0:411244c71423 | 246 | if(dy16 < DY_VALID_TH) |
| pixus_mbed | 0:411244c71423 | 247 | { |
| pixus_mbed | 0:411244c71423 | 248 | AccY = 0; |
| pixus_mbed | 0:411244c71423 | 249 | } |
| pixus_mbed | 0:411244c71423 | 250 | else |
| pixus_mbed | 0:411244c71423 | 251 | { |
| pixus_mbed | 0:411244c71423 | 252 | AccY += dy16; |
| pixus_mbed | 0:411244c71423 | 253 | if(AccY >= ACCY_PRESS_TH) |
| pixus_mbed | 0:411244c71423 | 254 | { |
| pixus_mbed | 0:411244c71423 | 255 | AccY = 0; |
| pixus_mbed | 0:411244c71423 | 256 | State = PRESS; |
| pixus_mbed | 0:411244c71423 | 257 | OutBtnState = OTS_BTN_PRESS; |
| pixus_mbed | 0:411244c71423 | 258 | } |
| pixus_mbed | 0:411244c71423 | 259 | } |
| pixus_mbed | 0:411244c71423 | 260 | } |
| pixus_mbed | 0:411244c71423 | 261 | } |
| pixus_mbed | 0:411244c71423 | 262 | |
| pixus_mbed | 0:411244c71423 | 263 | return OutBtnState; |
| pixus_mbed | 0:411244c71423 | 264 | } |
| pixus_mbed | 0:411244c71423 | 265 | |
| pixus_mbed | 0:411244c71423 | 266 | //----------------------------------------------------------------------- |
| pixus_mbed | 0:411244c71423 | 267 | void OTS_MotionPin_ISR(void) |
| pixus_mbed | 0:411244c71423 | 268 | { |
| pixus_mbed | 0:411244c71423 | 269 | detachInterrupt(digitalPinToInterrupt(PIN_SEN_MOTION)); |
| pixus_mbed | 0:411244c71423 | 270 | MotionPinEventTriggered=1; |
| pixus_mbed | 0:411244c71423 | 271 | } |
| pixus_mbed | 0:411244c71423 | 272 | |
| pixus_mbed | 0:411244c71423 | 273 | //----------------------------------------------------------------------- |
| pixus_mbed | 5:61318505e528 | 274 | |
| pixus_mbed | 5:61318505e528 | 275 | /* |
| pixus_mbed | 5:61318505e528 | 276 | SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. |
| pixus_mbed | 5:61318505e528 | 277 | Multiple bytes of data may be available. |
| pixus_mbed | 5:61318505e528 | 278 | |
| pixus_mbed | 5:61318505e528 | 279 | Host Command Supported: |
| pixus_mbed | 5:61318505e528 | 280 | Read and write sensor register |
| pixus_mbed | 5:61318505e528 | 281 | Sensor read = "r aa", aa must be hex value, example "r 0f" means read register 0x0f. |
| pixus_mbed | 5:61318505e528 | 282 | Sensor write = "w aa bb", aa and bb must be hex value, example "w 02 00" means write register 0x02 with value 0x00. |
| pixus_mbed | 5:61318505e528 | 283 | */ |
| pixus_mbed | 5:61318505e528 | 284 | #include <string> |
| pixus_mbed | 5:61318505e528 | 285 | #include <algorithm> |
| pixus_mbed | 5:61318505e528 | 286 | #include <cctype> |
| pixus_mbed | 5:61318505e528 | 287 | #include <locale> |
| pixus_mbed | 5:61318505e528 | 288 | |
| pixus_mbed | 5:61318505e528 | 289 | #define String string |
| pixus_mbed | 5:61318505e528 | 290 | #define VERSRION "2.40" //PAT9125 FW version |
| pixus_mbed | 5:61318505e528 | 291 | |
| pixus_mbed | 5:61318505e528 | 292 | |
| pixus_mbed | 5:61318505e528 | 293 | void serialProcess(String input); |
| pixus_mbed | 5:61318505e528 | 294 | bool IsParamValid(char *param, String str_pre, bool hex =false); |
| pixus_mbed | 5:61318505e528 | 295 | void PrintHex8(uint8_t data); |
| pixus_mbed | 5:61318505e528 | 296 | bool IsDigit(char *p, bool hex); |
| pixus_mbed | 5:61318505e528 | 297 | typedef unsigned char byte ; |
| pixus_mbed | 5:61318505e528 | 298 | String strRead; |
| pixus_mbed | 5:61318505e528 | 299 | char hexChar[3]; |
| pixus_mbed | 5:61318505e528 | 300 | |
| pixus_mbed | 5:61318505e528 | 301 | void println() |
| pixus_mbed | 5:61318505e528 | 302 | { |
| pixus_mbed | 15:089f1d28d152 | 303 | if(gp_state->p_pc == NULL) return ; |
| pixus_mbed | 5:61318505e528 | 304 | gp_state->p_pc->printf("\n"); |
| pixus_mbed | 5:61318505e528 | 305 | } |
| pixus_mbed | 5:61318505e528 | 306 | |
| pixus_mbed | 5:61318505e528 | 307 | void println(String str) |
| pixus_mbed | 5:61318505e528 | 308 | { |
| pixus_mbed | 15:089f1d28d152 | 309 | if(gp_state->p_pc == NULL) return ; |
| pixus_mbed | 5:61318505e528 | 310 | const char * c = str.c_str(); |
| pixus_mbed | 5:61318505e528 | 311 | gp_state->p_pc->printf(c); |
| pixus_mbed | 5:61318505e528 | 312 | gp_state->p_pc->printf("\n"); |
| pixus_mbed | 5:61318505e528 | 313 | } |
| pixus_mbed | 5:61318505e528 | 314 | void print(String str) |
| pixus_mbed | 5:61318505e528 | 315 | { |
| pixus_mbed | 15:089f1d28d152 | 316 | if(gp_state->p_pc == NULL) return ; |
| pixus_mbed | 5:61318505e528 | 317 | const char * c = str.c_str(); |
| pixus_mbed | 5:61318505e528 | 318 | gp_state->p_pc->printf(c); |
| pixus_mbed | 5:61318505e528 | 319 | } |
| pixus_mbed | 5:61318505e528 | 320 | void print(signed int value) |
| pixus_mbed | 5:61318505e528 | 321 | { |
| pixus_mbed | 15:089f1d28d152 | 322 | if(gp_state->p_pc == NULL) return ; |
| pixus_mbed | 5:61318505e528 | 323 | gp_state->p_pc->printf("%d",value); |
| pixus_mbed | 5:61318505e528 | 324 | } |
| pixus_mbed | 5:61318505e528 | 325 | |
| pixus_mbed | 5:61318505e528 | 326 | |
| pixus_mbed | 5:61318505e528 | 327 | string trim(const string& src, const string& c) |
| pixus_mbed | 5:61318505e528 | 328 | { |
| pixus_mbed | 5:61318505e528 | 329 | int p2 = src.find_last_not_of(c); |
| pixus_mbed | 5:61318505e528 | 330 | if (p2 == string::npos) { |
| pixus_mbed | 5:61318505e528 | 331 | return string(); |
| pixus_mbed | 5:61318505e528 | 332 | } |
| pixus_mbed | 5:61318505e528 | 333 | int p1 = src.find_first_not_of(c); |
| pixus_mbed | 5:61318505e528 | 334 | if (p1 == string::npos) { |
| pixus_mbed | 5:61318505e528 | 335 | p1 = 0; |
| pixus_mbed | 5:61318505e528 | 336 | } |
| pixus_mbed | 5:61318505e528 | 337 | |
| pixus_mbed | 5:61318505e528 | 338 | return src.substr(p1, (p2-p1)+1); |
| pixus_mbed | 5:61318505e528 | 339 | } |
| pixus_mbed | 5:61318505e528 | 340 | |
| pixus_mbed | 5:61318505e528 | 341 | void toCharArray(char *buffer, string s) |
| pixus_mbed | 5:61318505e528 | 342 | { |
| pixus_mbed | 5:61318505e528 | 343 | strcpy(buffer, s.c_str()); |
| pixus_mbed | 5:61318505e528 | 344 | } |
| pixus_mbed | 5:61318505e528 | 345 | |
| pixus_mbed | 5:61318505e528 | 346 | void serialEvent() |
| pixus_mbed | 5:61318505e528 | 347 | { |
| pixus_mbed | 15:089f1d28d152 | 348 | if(gp_state->p_pc == NULL) return ; |
| pixus_mbed | 5:61318505e528 | 349 | while (gp_state->p_pc->readable()) |
| pixus_mbed | 5:61318505e528 | 350 | { |
| pixus_mbed | 5:61318505e528 | 351 | // get the new byte: |
| pixus_mbed | 5:61318505e528 | 352 | char inChar = (char)gp_state->p_pc->getc() ; //Serial.read(); |
| pixus_mbed | 5:61318505e528 | 353 | // if the incoming character is a newline, set a flag |
| pixus_mbed | 5:61318505e528 | 354 | // so the main loop can do something about it: |
| pixus_mbed | 5:61318505e528 | 355 | if ((inChar == '\n') || (inChar == '\r')) |
| pixus_mbed | 5:61318505e528 | 356 | { |
| pixus_mbed | 5:61318505e528 | 357 | //strRead.trim(); |
| pixus_mbed | 5:61318505e528 | 358 | strRead = trim(strRead, " "); |
| pixus_mbed | 5:61318505e528 | 359 | strRead += " "; // for tokenizing |
| pixus_mbed | 5:61318505e528 | 360 | serialProcess(strRead); |
| pixus_mbed | 5:61318505e528 | 361 | strRead = ""; |
| pixus_mbed | 5:61318505e528 | 362 | } |
| pixus_mbed | 5:61318505e528 | 363 | else if (inChar != 0xf0) |
| pixus_mbed | 5:61318505e528 | 364 | { |
| pixus_mbed | 5:61318505e528 | 365 | // add it to the inputString: |
| pixus_mbed | 5:61318505e528 | 366 | strRead += inChar; |
| pixus_mbed | 5:61318505e528 | 367 | } |
| pixus_mbed | 5:61318505e528 | 368 | } |
| pixus_mbed | 5:61318505e528 | 369 | } |
| pixus_mbed | 5:61318505e528 | 370 | |
| pixus_mbed | 5:61318505e528 | 371 | void serialProcess(String input) |
| pixus_mbed | 5:61318505e528 | 372 | { |
| pixus_mbed | 5:61318505e528 | 373 | int val; |
| pixus_mbed | 5:61318505e528 | 374 | int i, j; |
| pixus_mbed | 5:61318505e528 | 375 | String output; |
| pixus_mbed | 5:61318505e528 | 376 | unsigned char Address,Value; |
| pixus_mbed | 5:61318505e528 | 377 | |
| pixus_mbed | 5:61318505e528 | 378 | // ==================================================== |
| pixus_mbed | 5:61318505e528 | 379 | // Single command goes here |
| pixus_mbed | 5:61318505e528 | 380 | // NOTE: All command compare must have " " at the back |
| pixus_mbed | 5:61318505e528 | 381 | // ==================================================== |
| pixus_mbed | 5:61318505e528 | 382 | if (input == "pxi ") |
| pixus_mbed | 5:61318505e528 | 383 | { |
| pixus_mbed | 5:61318505e528 | 384 | println("PixArt-PAT9125"); |
| pixus_mbed | 5:61318505e528 | 385 | } |
| pixus_mbed | 5:61318505e528 | 386 | else if (input == "ver ") |
| pixus_mbed | 5:61318505e528 | 387 | { |
| pixus_mbed | 5:61318505e528 | 388 | println(VERSRION); |
| pixus_mbed | 5:61318505e528 | 389 | } |
| pixus_mbed | 5:61318505e528 | 390 | else if (input == "xy2uart_on ") |
| pixus_mbed | 5:61318505e528 | 391 | { |
| pixus_mbed | 5:61318505e528 | 392 | xy2uart_enh=1; |
| pixus_mbed | 5:61318505e528 | 393 | println("Sensor XY Output Enable"); |
| pixus_mbed | 5:61318505e528 | 394 | } |
| pixus_mbed | 5:61318505e528 | 395 | else if (input == "xy2uart_off ") |
| pixus_mbed | 5:61318505e528 | 396 | { |
| pixus_mbed | 5:61318505e528 | 397 | xy2uart_enh=0; |
| pixus_mbed | 5:61318505e528 | 398 | println("Sensor XY Output Disable"); |
| pixus_mbed | 5:61318505e528 | 399 | } |
| pixus_mbed | 5:61318505e528 | 400 | // ==================================================== |
| pixus_mbed | 5:61318505e528 | 401 | // Command with parameters goes here, like r 00, w 48 00 |
| pixus_mbed | 5:61318505e528 | 402 | // ==================================================== |
| pixus_mbed | 5:61318505e528 | 403 | else if (input != " ") // not empty string, then proceed to decode the command |
| pixus_mbed | 5:61318505e528 | 404 | { |
| pixus_mbed | 5:61318505e528 | 405 | char buff[50]; |
| pixus_mbed | 5:61318505e528 | 406 | //input.toCharArray(buff, input.length()); |
| pixus_mbed | 5:61318505e528 | 407 | toCharArray(buff, input); |
| pixus_mbed | 5:61318505e528 | 408 | |
| pixus_mbed | 5:61318505e528 | 409 | // Get the command |
| pixus_mbed | 5:61318505e528 | 410 | char *tok = strtok(buff, " "); |
| pixus_mbed | 5:61318505e528 | 411 | if (!tok) |
| pixus_mbed | 5:61318505e528 | 412 | { |
| pixus_mbed | 5:61318505e528 | 413 | println("empty command"); |
| pixus_mbed | 5:61318505e528 | 414 | return; |
| pixus_mbed | 5:61318505e528 | 415 | } |
| pixus_mbed | 5:61318505e528 | 416 | |
| pixus_mbed | 5:61318505e528 | 417 | // Get parameters, up to 4 parameters, if more than that, individual command will need to extract it on their own |
| pixus_mbed | 5:61318505e528 | 418 | char *param[4]; |
| pixus_mbed | 5:61318505e528 | 419 | for (val = 0; val < 4; val++) |
| pixus_mbed | 5:61318505e528 | 420 | { |
| pixus_mbed | 5:61318505e528 | 421 | param[val] = strtok(NULL, " "); |
| pixus_mbed | 5:61318505e528 | 422 | if (!param[val]) // break if we reach the end |
| pixus_mbed | 5:61318505e528 | 423 | break; |
| pixus_mbed | 5:61318505e528 | 424 | } |
| pixus_mbed | 5:61318505e528 | 425 | |
| pixus_mbed | 5:61318505e528 | 426 | // switch for command |
| pixus_mbed | 5:61318505e528 | 427 | String str = String(tok); |
| pixus_mbed | 5:61318505e528 | 428 | if ((str == "r") || (str == "w")) |
| pixus_mbed | 5:61318505e528 | 429 | { |
| pixus_mbed | 5:61318505e528 | 430 | // Address is the first byte |
| pixus_mbed | 5:61318505e528 | 431 | if (!IsParamValid(param[0], "Error (r/w): address")) |
| pixus_mbed | 5:61318505e528 | 432 | { |
| pixus_mbed | 5:61318505e528 | 433 | return; |
| pixus_mbed | 5:61318505e528 | 434 | } |
| pixus_mbed | 5:61318505e528 | 435 | // convert to integer |
| pixus_mbed | 5:61318505e528 | 436 | Address = strtol(param[0], NULL, 16); |
| pixus_mbed | 5:61318505e528 | 437 | if (Address > 0x7F) |
| pixus_mbed | 5:61318505e528 | 438 | { |
| pixus_mbed | 5:61318505e528 | 439 | println("Error (r/w): address > 0x7F"); |
| pixus_mbed | 5:61318505e528 | 440 | return; |
| pixus_mbed | 5:61318505e528 | 441 | } |
| pixus_mbed | 5:61318505e528 | 442 | |
| pixus_mbed | 5:61318505e528 | 443 | if (str == "w") |
| pixus_mbed | 5:61318505e528 | 444 | { |
| pixus_mbed | 5:61318505e528 | 445 | // Value is second byte |
| pixus_mbed | 5:61318505e528 | 446 | if (!IsParamValid(param[1], "Error (w): value")) |
| pixus_mbed | 5:61318505e528 | 447 | { |
| pixus_mbed | 5:61318505e528 | 448 | return; |
| pixus_mbed | 5:61318505e528 | 449 | } |
| pixus_mbed | 5:61318505e528 | 450 | // convert to integer |
| pixus_mbed | 5:61318505e528 | 451 | Value = strtol(param[1], NULL, 16); |
| pixus_mbed | 5:61318505e528 | 452 | if (Value > 0xFF) |
| pixus_mbed | 5:61318505e528 | 453 | { |
| pixus_mbed | 5:61318505e528 | 454 | println("Error (w): value > 0xFF"); |
| pixus_mbed | 5:61318505e528 | 455 | return; |
| pixus_mbed | 5:61318505e528 | 456 | } |
| pixus_mbed | 5:61318505e528 | 457 | |
| pixus_mbed | 5:61318505e528 | 458 | OTS_Write_Reg(Address, Value); |
| pixus_mbed | 5:61318505e528 | 459 | } |
| pixus_mbed | 5:61318505e528 | 460 | |
| pixus_mbed | 5:61318505e528 | 461 | print("ADDR "); |
| pixus_mbed | 5:61318505e528 | 462 | PrintHex8(Address); |
| pixus_mbed | 5:61318505e528 | 463 | print(" "); |
| pixus_mbed | 5:61318505e528 | 464 | PrintHex8(OTS_Read_Reg(Address)); |
| pixus_mbed | 5:61318505e528 | 465 | println(); |
| pixus_mbed | 5:61318505e528 | 466 | } |
| pixus_mbed | 5:61318505e528 | 467 | else |
| pixus_mbed | 5:61318505e528 | 468 | { |
| pixus_mbed | 5:61318505e528 | 469 | println("Bad command"); |
| pixus_mbed | 5:61318505e528 | 470 | } |
| pixus_mbed | 5:61318505e528 | 471 | } |
| pixus_mbed | 5:61318505e528 | 472 | } |
| pixus_mbed | 5:61318505e528 | 473 | |
| pixus_mbed | 5:61318505e528 | 474 | // Check if param is not given or not hexa, print the error as well |
| pixus_mbed | 5:61318505e528 | 475 | bool IsParamValid(char *param, String str_pre, bool hex) |
| pixus_mbed | 5:61318505e528 | 476 | { |
| pixus_mbed | 5:61318505e528 | 477 | if (!param) |
| pixus_mbed | 5:61318505e528 | 478 | { |
| pixus_mbed | 5:61318505e528 | 479 | print(str_pre); |
| pixus_mbed | 5:61318505e528 | 480 | println(" not given"); |
| pixus_mbed | 5:61318505e528 | 481 | return false; |
| pixus_mbed | 5:61318505e528 | 482 | } |
| pixus_mbed | 5:61318505e528 | 483 | if (!IsDigit(param, hex)) |
| pixus_mbed | 5:61318505e528 | 484 | { |
| pixus_mbed | 5:61318505e528 | 485 | print(str_pre); |
| pixus_mbed | 5:61318505e528 | 486 | if (hex) |
| pixus_mbed | 5:61318505e528 | 487 | println(" not hex"); |
| pixus_mbed | 5:61318505e528 | 488 | else |
| pixus_mbed | 5:61318505e528 | 489 | println(" not digit"); |
| pixus_mbed | 5:61318505e528 | 490 | return false; |
| pixus_mbed | 5:61318505e528 | 491 | } |
| pixus_mbed | 5:61318505e528 | 492 | return true; |
| pixus_mbed | 5:61318505e528 | 493 | } |
| pixus_mbed | 5:61318505e528 | 494 | |
| pixus_mbed | 5:61318505e528 | 495 | // Check whether is hexa for given char |
| pixus_mbed | 5:61318505e528 | 496 | bool IsDigit(char *p, bool hex) |
| pixus_mbed | 5:61318505e528 | 497 | { |
| pixus_mbed | 5:61318505e528 | 498 | for (int i; i < strlen(p); i++) |
| pixus_mbed | 5:61318505e528 | 499 | { |
| pixus_mbed | 5:61318505e528 | 500 | if (hex) |
| pixus_mbed | 5:61318505e528 | 501 | { |
| pixus_mbed | 5:61318505e528 | 502 | if (!isxdigit(p[i])) |
| pixus_mbed | 5:61318505e528 | 503 | return false; |
| pixus_mbed | 5:61318505e528 | 504 | } |
| pixus_mbed | 5:61318505e528 | 505 | else if (!isdigit(p[i])) |
| pixus_mbed | 5:61318505e528 | 506 | return false; |
| pixus_mbed | 5:61318505e528 | 507 | } |
| pixus_mbed | 5:61318505e528 | 508 | return true; |
| pixus_mbed | 5:61318505e528 | 509 | } |
| pixus_mbed | 5:61318505e528 | 510 | |
| pixus_mbed | 5:61318505e528 | 511 | void Hex8(uint8_t data) |
| pixus_mbed | 5:61318505e528 | 512 | { |
| pixus_mbed | 5:61318505e528 | 513 | byte first ; |
| pixus_mbed | 5:61318505e528 | 514 | first = (data >> 4) | 48; |
| pixus_mbed | 5:61318505e528 | 515 | if (first > 57) hexChar[0] = first + (byte)39; |
| pixus_mbed | 5:61318505e528 | 516 | else hexChar[0] = first ; |
| pixus_mbed | 5:61318505e528 | 517 | |
| pixus_mbed | 5:61318505e528 | 518 | first = (data & 0x0F) | 48; |
| pixus_mbed | 5:61318505e528 | 519 | if (first > 57) hexChar[1] = first + (byte)39; |
| pixus_mbed | 5:61318505e528 | 520 | else hexChar[1] = first; |
| pixus_mbed | 5:61318505e528 | 521 | } |
| pixus_mbed | 5:61318505e528 | 522 | |
| pixus_mbed | 5:61318505e528 | 523 | // Print in hex with leading zero |
| pixus_mbed | 5:61318505e528 | 524 | void PrintHex8(uint8_t data) |
| pixus_mbed | 5:61318505e528 | 525 | { |
| pixus_mbed | 5:61318505e528 | 526 | Hex8(data); |
| pixus_mbed | 5:61318505e528 | 527 | |
| pixus_mbed | 5:61318505e528 | 528 | print(hexChar); |
| pixus_mbed | 5:61318505e528 | 529 | } |
| pixus_mbed | 5:61318505e528 | 530 | |
| pixus_mbed | 5:61318505e528 | 531 | |
| pixus_mbed | 5:61318505e528 | 532 | //----------------------------------------------------------------------- |
| pixus_mbed | 0:411244c71423 | 533 | void loop() |
| pixus_mbed | 0:411244c71423 | 534 | { |
| pixus_mbed | 0:411244c71423 | 535 | |
| pixus_mbed | 0:411244c71423 | 536 | if(digitalRead(PIN_BTN_L) == LOW)//or reset whenever idle_timer timeout |
| pixus_mbed | 0:411244c71423 | 537 | { |
| pixus_mbed | 0:411244c71423 | 538 | OTS_Reset_Variables(); |
| pixus_mbed | 0:411244c71423 | 539 | } |
| pixus_mbed | 0:411244c71423 | 540 | |
| pixus_mbed | 0:411244c71423 | 541 | if(MotionPinEventTriggered==1) |
| pixus_mbed | 0:411244c71423 | 542 | { |
| pixus_mbed | 0:411244c71423 | 543 | MotionPinEventTriggered=0;//clear flag after read 'Motion Status and Data' |
| pixus_mbed | 0:411244c71423 | 544 | OTS_Read_Motion(&deltaX16,&deltaY16); |
| pixus_mbed | 0:411244c71423 | 545 | |
| pixus_mbed | 0:411244c71423 | 546 | if(deltaX16 || deltaY16) |
| pixus_mbed | 0:411244c71423 | 547 | { |
| pixus_mbed | 0:411244c71423 | 548 | OTS_ROT_Status = OTS_Detect_Rotation(deltaX16,deltaY16); |
| pixus_mbed | 0:411244c71423 | 549 | OTS_BTN_Status = OTS_Detect_Pressing(deltaX16,deltaY16); |
| pixus_mbed | 0:411244c71423 | 550 | |
| pixus_mbed | 0:411244c71423 | 551 | if(OTS_ROT_Status == OTS_ROT_UP) |
| pixus_mbed | 0:411244c71423 | 552 | { |
| pixus_mbed | 0:411244c71423 | 553 | LED_RED_ON; |
| pixus_mbed | 0:411244c71423 | 554 | LCM_DisplayString(1,8,"Up "); |
| pixus_mbed | 0:411244c71423 | 555 | } |
| pixus_mbed | 0:411244c71423 | 556 | else if(OTS_ROT_Status == OTS_ROT_DOWN) |
| pixus_mbed | 0:411244c71423 | 557 | { |
| pixus_mbed | 0:411244c71423 | 558 | LED_GREEN_ON; |
| pixus_mbed | 0:411244c71423 | 559 | LCM_DisplayString(1,8,"Dn "); |
| pixus_mbed | 0:411244c71423 | 560 | } |
| pixus_mbed | 0:411244c71423 | 561 | |
| pixus_mbed | 0:411244c71423 | 562 | if(OTS_BTN_Status == OTS_BTN_PRESS) |
| pixus_mbed | 0:411244c71423 | 563 | { |
| pixus_mbed | 0:411244c71423 | 564 | OTS_BTN_Press_Cnt++; |
| pixus_mbed | 0:411244c71423 | 565 | if(OTS_BTN_Press_Cnt > 999) |
| pixus_mbed | 0:411244c71423 | 566 | { |
| pixus_mbed | 0:411244c71423 | 567 | OTS_BTN_Press_Cnt=0; |
| pixus_mbed | 0:411244c71423 | 568 | } |
| pixus_mbed | 0:411244c71423 | 569 | LCM_DisplayString(2,8,"Pre"); |
| pixus_mbed | 0:411244c71423 | 570 | LCM_DisplayDecimal(2,12,OTS_BTN_Press_Cnt,4); |
| pixus_mbed | 0:411244c71423 | 571 | } |
| pixus_mbed | 0:411244c71423 | 572 | else if(OTS_BTN_Status == OTS_BTN_RELEASE) |
| pixus_mbed | 0:411244c71423 | 573 | { |
| pixus_mbed | 0:411244c71423 | 574 | LCM_DisplayString(2,8,"Rel"); |
| pixus_mbed | 0:411244c71423 | 575 | } |
| pixus_mbed | 0:411244c71423 | 576 | } |
| pixus_mbed | 0:411244c71423 | 577 | |
| pixus_mbed | 5:61318505e528 | 578 | if(xy2uart_enh == 1) |
| pixus_mbed | 5:61318505e528 | 579 | { |
| pixus_mbed | 5:61318505e528 | 580 | //output to UART |
| pixus_mbed | 5:61318505e528 | 581 | print("DX:\t");print(deltaX16);print("\t"); |
| pixus_mbed | 5:61318505e528 | 582 | print("DY:\t");print(deltaY16);print("\t"); |
| pixus_mbed | 5:61318505e528 | 583 | print("\n"); |
| pixus_mbed | 5:61318505e528 | 584 | } |
| pixus_mbed | 5:61318505e528 | 585 | |
| pixus_mbed | 0:411244c71423 | 586 | //re-enable interrupt for MOTION pin |
| pixus_mbed | 0:411244c71423 | 587 | attachInterrupt(digitalPinToInterrupt(PIN_SEN_MOTION), OTS_MotionPin_ISR, LOW); |
| pixus_mbed | 0:411244c71423 | 588 | |
| pixus_mbed | 0:411244c71423 | 589 | } |
| pixus_mbed | 0:411244c71423 | 590 | |
| pixus_mbed | 0:411244c71423 | 591 | if(digitalRead(PIN_SEN_MOTION) == HIGH) |
| pixus_mbed | 0:411244c71423 | 592 | { |
| pixus_mbed | 0:411244c71423 | 593 | LED_GREEN_OFF; |
| pixus_mbed | 0:411244c71423 | 594 | LED_RED_OFF; |
| pixus_mbed | 0:411244c71423 | 595 | } |
| pixus_mbed | 0:411244c71423 | 596 | |
| pixus_mbed | 0:411244c71423 | 597 | } |
| pixus_mbed | 0:411244c71423 | 598 | pat9125_mbed::pat9125_mbed(pat9125_mbed_state_s *state) |
| pixus_mbed | 0:411244c71423 | 599 | { |
| pixus_mbed | 15:089f1d28d152 | 600 | gp_state = state ; |
| pixus_mbed | 0:411244c71423 | 601 | gp_state->sen_status = OTS_Sensor_Init(); |
| pixus_mbed | 15:089f1d28d152 | 602 | print("OTS_Sensor_Init\n"); |
| pixus_mbed | 0:411244c71423 | 603 | gp_state->pINT->fall(&OTS_MotionPin_ISR); //interrupt at low state |
| pixus_mbed | 0:411244c71423 | 604 | } |
| pixus_mbed | 0:411244c71423 | 605 | |
| pixus_mbed | 0:411244c71423 | 606 | void pat9125_mbed::task() |
| pixus_mbed | 0:411244c71423 | 607 | { |
| pixus_mbed | 0:411244c71423 | 608 | if(digitalRead(PIN_SEN_MOTION) == LOW) |
| pixus_mbed | 0:411244c71423 | 609 | { |
| pixus_mbed | 0:411244c71423 | 610 | //gp_state->p_pc->printf("Motion Low\n"); |
| pixus_mbed | 0:411244c71423 | 611 | MotionPinEventTriggered = 1 ; |
| pixus_mbed | 0:411244c71423 | 612 | } |
| pixus_mbed | 0:411244c71423 | 613 | loop(); |
| pixus_mbed | 5:61318505e528 | 614 | serialEvent(); |
| pixus_mbed | 0:411244c71423 | 615 | } |
PAT9125EL - Miniature Optical Navigation for Surface Tracking