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/pat9125_mbed.cpp@0:411244c71423, 2017-10-03 (annotated)
- Committer:
- pixus_mbed
- Date:
- Tue Oct 03 07:26:38 2017 +0000
- Revision:
- 0:411244c71423
- Child:
- 4:1cd61816c013
- Child:
- 5:61318505e528
1st version : OTS, LCM and Serial output are work.
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 | 0:411244c71423 | 39 | #define LCM_DisplayString_Reset gp_state->pLCM->LCM_DisplayString_Reset |
| pixus_mbed | 0:411244c71423 | 40 | #define LCM_DisplayDecimal(a,b,c,d) gp_state->pLCM->LCM_DisplayDecimal(a,b,c,d) |
| pixus_mbed | 0:411244c71423 | 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 | 0:411244c71423 | 44 | |
| pixus_mbed | 0:411244c71423 | 45 | //for OTS |
| pixus_mbed | 0:411244c71423 | 46 | signed int deltaX16; |
| pixus_mbed | 0:411244c71423 | 47 | signed int deltaY16; |
| pixus_mbed | 0:411244c71423 | 48 | unsigned char OTS_ROT_Status; |
| pixus_mbed | 0:411244c71423 | 49 | unsigned char OTS_BTN_Status; |
| pixus_mbed | 0:411244c71423 | 50 | |
| pixus_mbed | 0:411244c71423 | 51 | signed long x_sum=0; |
| pixus_mbed | 0:411244c71423 | 52 | signed long ds_x_sum=0; |
| pixus_mbed | 0:411244c71423 | 53 | signed long pre_dsCountX=0; |
| pixus_mbed | 0:411244c71423 | 54 | unsigned int OTS_BTN_Press_Cnt=0; |
| pixus_mbed | 0:411244c71423 | 55 | |
| pixus_mbed | 0:411244c71423 | 56 | volatile unsigned char MotionPinEventTriggered=0; |
| pixus_mbed | 0:411244c71423 | 57 | |
| pixus_mbed | 0:411244c71423 | 58 | // Register write function |
| pixus_mbed | 0:411244c71423 | 59 | void OTS_Write_Reg(unsigned char address, unsigned char value) |
| pixus_mbed | 0:411244c71423 | 60 | { |
| pixus_mbed | 0:411244c71423 | 61 | int ret ; |
| pixus_mbed | 0:411244c71423 | 62 | char data_write[2]; |
| pixus_mbed | 0:411244c71423 | 63 | |
| pixus_mbed | 0:411244c71423 | 64 | data_write[0] = address; |
| pixus_mbed | 0:411244c71423 | 65 | data_write[1] = value; |
| pixus_mbed | 0:411244c71423 | 66 | ret = gp_state->p_i2c->write(gp_state->slave_id, data_write, 2, 0); |
| pixus_mbed | 0:411244c71423 | 67 | } |
| pixus_mbed | 0:411244c71423 | 68 | |
| pixus_mbed | 0:411244c71423 | 69 | // Register Read function |
| pixus_mbed | 0:411244c71423 | 70 | unsigned char OTS_Read_Reg(unsigned char address) |
| pixus_mbed | 0:411244c71423 | 71 | { |
| pixus_mbed | 0:411244c71423 | 72 | unsigned char rdata = 0; |
| pixus_mbed | 0:411244c71423 | 73 | gp_state->p_i2c->write(gp_state->slave_id, (char *)&address, 1, 0); |
| pixus_mbed | 0:411244c71423 | 74 | gp_state->p_i2c->read(gp_state->slave_id, (char *)&rdata, 1, 0); |
| pixus_mbed | 0:411244c71423 | 75 | |
| pixus_mbed | 0:411244c71423 | 76 | return(rdata); |
| pixus_mbed | 0:411244c71423 | 77 | } |
| pixus_mbed | 0:411244c71423 | 78 | |
| pixus_mbed | 0:411244c71423 | 79 | // Register write & read back check function |
| pixus_mbed | 0:411244c71423 | 80 | void OTS_WriteRead_Reg(unsigned char address, unsigned char wdata) |
| pixus_mbed | 0:411244c71423 | 81 | { |
| pixus_mbed | 0:411244c71423 | 82 | unsigned char rdata; |
| pixus_mbed | 0:411244c71423 | 83 | do |
| pixus_mbed | 0:411244c71423 | 84 | { |
| pixus_mbed | 0:411244c71423 | 85 | OTS_Write_Reg(address, wdata); // Write data to specified address |
| pixus_mbed | 0:411244c71423 | 86 | rdata = OTS_Read_Reg(address); // Read back previous written data |
| pixus_mbed | 0:411244c71423 | 87 | } while(rdata != wdata); // Check if the data is correctly written |
| pixus_mbed | 0:411244c71423 | 88 | |
| pixus_mbed | 0:411244c71423 | 89 | } |
| pixus_mbed | 0:411244c71423 | 90 | |
| pixus_mbed | 0:411244c71423 | 91 | boolean OTS_Sensor_Init(void) |
| pixus_mbed | 0:411244c71423 | 92 | { |
| pixus_mbed | 0:411244c71423 | 93 | unsigned char sensor_pid=0; |
| pixus_mbed | 0:411244c71423 | 94 | boolean read_id_ok=false; |
| pixus_mbed | 0:411244c71423 | 95 | |
| pixus_mbed | 0:411244c71423 | 96 | // Read sensor_pid in address 0x00 to check if the serial link is valid, PID should be 0x31 |
| pixus_mbed | 0:411244c71423 | 97 | sensor_pid = OTS_Read_Reg(0x00); |
| pixus_mbed | 0:411244c71423 | 98 | if(sensor_pid == 0x31) |
| pixus_mbed | 0:411244c71423 | 99 | { |
| pixus_mbed | 0:411244c71423 | 100 | read_id_ok = true; |
| pixus_mbed | 0:411244c71423 | 101 | |
| pixus_mbed | 0:411244c71423 | 102 | //PAT9125 sensor recommended settings as below: |
| pixus_mbed | 0:411244c71423 | 103 | OTS_Write_Reg(0x7F, 0x00); // switch to bank0, not allowed to perform OTS_WriteRead_Reg |
| pixus_mbed | 0:411244c71423 | 104 | OTS_Write_Reg(0x06, 0x97); // Software Reset (i.e. set bit7 to 1), then it will reset to 0 automatically |
| pixus_mbed | 0:411244c71423 | 105 | |
| pixus_mbed | 0:411244c71423 | 106 | I2C_RESET; |
| pixus_mbed | 0:411244c71423 | 107 | |
| pixus_mbed | 0:411244c71423 | 108 | delay(1); // delay 1ms |
| pixus_mbed | 0:411244c71423 | 109 | OTS_Write_Reg(0x06, 0x17); // ensure the sensor has left the reset state. |
| pixus_mbed | 0:411244c71423 | 110 | |
| pixus_mbed | 0:411244c71423 | 111 | OTS_WriteRead_Reg(0x09, 0x5A); // disable write protect |
| pixus_mbed | 0:411244c71423 | 112 | OTS_WriteRead_Reg(0x0D, 0x65); // set X-axis resolution (depends on application) |
| pixus_mbed | 0:411244c71423 | 113 | OTS_WriteRead_Reg(0x0E, 0xFF); // set Y-axis resolution (depends on application) |
| pixus_mbed | 0:411244c71423 | 114 | OTS_WriteRead_Reg(0x19, 0x04); // set 12-bit X/Y data format (depends on application) |
| pixus_mbed | 0:411244c71423 | 115 | //OTS_WriteRead_Reg(0x4B, 0x04); // ONLY for VDD=VDDA=1.7~1.9V: for power saving |
| pixus_mbed | 0:411244c71423 | 116 | |
| pixus_mbed | 0:411244c71423 | 117 | if(OTS_Read_Reg(0x5E) == 0x04) |
| pixus_mbed | 0:411244c71423 | 118 | { |
| pixus_mbed | 0:411244c71423 | 119 | OTS_WriteRead_Reg(0x5E, 0x08); |
| pixus_mbed | 0:411244c71423 | 120 | if(OTS_Read_Reg(0x5D) == 0x10) |
| pixus_mbed | 0:411244c71423 | 121 | OTS_WriteRead_Reg(0x5D, 0x19); |
| pixus_mbed | 0:411244c71423 | 122 | } |
| pixus_mbed | 0:411244c71423 | 123 | |
| pixus_mbed | 0:411244c71423 | 124 | OTS_WriteRead_Reg(0x09, 0x00); // enable write protect |
| pixus_mbed | 0:411244c71423 | 125 | } |
| pixus_mbed | 0:411244c71423 | 126 | |
| pixus_mbed | 0:411244c71423 | 127 | return read_id_ok; |
| pixus_mbed | 0:411244c71423 | 128 | } |
| pixus_mbed | 0:411244c71423 | 129 | |
| pixus_mbed | 0:411244c71423 | 130 | // Read motion |
| pixus_mbed | 0:411244c71423 | 131 | void OTS_Read_Motion(signed int *dx16, signed int *dy16) |
| pixus_mbed | 0:411244c71423 | 132 | { |
| pixus_mbed | 0:411244c71423 | 133 | int shift = (sizeof(signed int) << 3) - 12 ; |
| pixus_mbed | 0:411244c71423 | 134 | signed int deltaX_l=0, deltaY_l=0, deltaXY_h=0; |
| pixus_mbed | 0:411244c71423 | 135 | signed int deltaX_h=0, deltaY_h=0; |
| pixus_mbed | 0:411244c71423 | 136 | char motion = OTS_Read_Reg(0x02) ; |
| pixus_mbed | 0:411244c71423 | 137 | if(motion & 0x80) //check motion bit in bit7 |
| pixus_mbed | 0:411244c71423 | 138 | { |
| pixus_mbed | 0:411244c71423 | 139 | deltaX_l = OTS_Read_Reg(0x03); |
| pixus_mbed | 0:411244c71423 | 140 | deltaY_l = OTS_Read_Reg(0x04); |
| pixus_mbed | 0:411244c71423 | 141 | deltaXY_h = OTS_Read_Reg(0x12); |
| pixus_mbed | 0:411244c71423 | 142 | |
| pixus_mbed | 0:411244c71423 | 143 | deltaX_h = (deltaXY_h<<4) & 0xF00; |
| pixus_mbed | 0:411244c71423 | 144 | deltaX_h = (deltaX_h << shift) >> shift ; |
| pixus_mbed | 0:411244c71423 | 145 | //if(deltaX_h & 0x800) deltaX_h |= 0xfffff000; // 12-bit data convert to 16-bit |
| pixus_mbed | 0:411244c71423 | 146 | |
| pixus_mbed | 0:411244c71423 | 147 | deltaY_h = (deltaXY_h<<8) & 0xF00; |
| pixus_mbed | 0:411244c71423 | 148 | //if(deltaY_h & 0x800) deltaY_h |= 0xfffff000; // 12-bit data convert to 16-bit |
| pixus_mbed | 0:411244c71423 | 149 | deltaY_h = (deltaY_h << shift) >> shift ; |
| pixus_mbed | 0:411244c71423 | 150 | |
| pixus_mbed | 0:411244c71423 | 151 | } |
| pixus_mbed | 0:411244c71423 | 152 | *dx16 = -(deltaX_h | deltaX_l); //inverse the data (depends on sensor's orientation and application) |
| pixus_mbed | 0:411244c71423 | 153 | *dy16 = -(deltaY_h | deltaY_l); //inverse the data (depends on sensor's orientation and application) |
| pixus_mbed | 0:411244c71423 | 154 | } |
| pixus_mbed | 0:411244c71423 | 155 | |
| pixus_mbed | 0:411244c71423 | 156 | void OTS_Reset_Variables(void) |
| pixus_mbed | 0:411244c71423 | 157 | { |
| pixus_mbed | 0:411244c71423 | 158 | //reset variables |
| pixus_mbed | 0:411244c71423 | 159 | x_sum=0; |
| pixus_mbed | 0:411244c71423 | 160 | ds_x_sum=0; |
| pixus_mbed | 0:411244c71423 | 161 | pre_dsCountX=0; |
| pixus_mbed | 0:411244c71423 | 162 | |
| pixus_mbed | 0:411244c71423 | 163 | OTS_BTN_Press_Cnt=0; |
| pixus_mbed | 0:411244c71423 | 164 | LCM_DisplayString_Reset(); |
| pixus_mbed | 0:411244c71423 | 165 | } |
| pixus_mbed | 0:411244c71423 | 166 | |
| pixus_mbed | 0:411244c71423 | 167 | unsigned char Detect_Rotation(signed long dsCountX) |
| pixus_mbed | 0:411244c71423 | 168 | { |
| pixus_mbed | 0:411244c71423 | 169 | #define EVENT_NUM_PER_ROUND 360//10 |
| pixus_mbed | 0:411244c71423 | 170 | #define EVENT_COUNT_TH (EXPECTED_COUNT_PER_ROUND / EVENT_NUM_PER_ROUND) //360/10=36 //360/360=1 |
| pixus_mbed | 0:411244c71423 | 171 | |
| pixus_mbed | 0:411244c71423 | 172 | signed long diff_count = 0; |
| pixus_mbed | 0:411244c71423 | 173 | unsigned char OutRotState = OTS_ROT_NO_CHANGE; |
| pixus_mbed | 0:411244c71423 | 174 | |
| pixus_mbed | 0:411244c71423 | 175 | diff_count = dsCountX - pre_dsCountX; |
| pixus_mbed | 0:411244c71423 | 176 | if( diff_count >= EVENT_COUNT_TH ) |
| pixus_mbed | 0:411244c71423 | 177 | { |
| pixus_mbed | 0:411244c71423 | 178 | pre_dsCountX = dsCountX; |
| pixus_mbed | 0:411244c71423 | 179 | OutRotState = OTS_ROT_UP; |
| pixus_mbed | 0:411244c71423 | 180 | } |
| pixus_mbed | 0:411244c71423 | 181 | else if( diff_count <= (-EVENT_COUNT_TH) ) |
| pixus_mbed | 0:411244c71423 | 182 | { |
| pixus_mbed | 0:411244c71423 | 183 | pre_dsCountX = dsCountX; |
| pixus_mbed | 0:411244c71423 | 184 | OutRotState = OTS_ROT_DOWN; |
| pixus_mbed | 0:411244c71423 | 185 | } |
| pixus_mbed | 0:411244c71423 | 186 | |
| pixus_mbed | 0:411244c71423 | 187 | return OutRotState; |
| pixus_mbed | 0:411244c71423 | 188 | } |
| pixus_mbed | 0:411244c71423 | 189 | |
| pixus_mbed | 0:411244c71423 | 190 | signed long OTS_Resolution_Downscale(signed int delta_count) |
| pixus_mbed | 0:411244c71423 | 191 | { |
| pixus_mbed | 0:411244c71423 | 192 | x_sum += delta_count; |
| pixus_mbed | 0:411244c71423 | 193 | return (x_sum * EXPECTED_COUNT_PER_ROUND / REAL_AVG_COUNT_PER_ROUND); |
| pixus_mbed | 0:411244c71423 | 194 | } |
| pixus_mbed | 0:411244c71423 | 195 | |
| pixus_mbed | 0:411244c71423 | 196 | unsigned char OTS_Detect_Rotation(signed int dx16, signed int dy16) |
| pixus_mbed | 0:411244c71423 | 197 | { |
| pixus_mbed | 0:411244c71423 | 198 | ds_x_sum = OTS_Resolution_Downscale(dx16); |
| pixus_mbed | 0:411244c71423 | 199 | LCM_DisplayDecimal(1,12,ds_x_sum,4);//show downscale value |
| pixus_mbed | 0:411244c71423 | 200 | |
| pixus_mbed | 0:411244c71423 | 201 | return Detect_Rotation(ds_x_sum); |
| pixus_mbed | 0:411244c71423 | 202 | } |
| pixus_mbed | 0:411244c71423 | 203 | |
| pixus_mbed | 0:411244c71423 | 204 | unsigned char OTS_Detect_Pressing(signed int dx16, signed int dy16) |
| pixus_mbed | 0:411244c71423 | 205 | { |
| pixus_mbed | 0:411244c71423 | 206 | #define PRESS 1 |
| pixus_mbed | 0:411244c71423 | 207 | #define RELEASE 0 |
| pixus_mbed | 0:411244c71423 | 208 | |
| pixus_mbed | 0:411244c71423 | 209 | #define DX_ROTATE_TH 2 |
| pixus_mbed | 0:411244c71423 | 210 | #define DY_VALID_TH 1 |
| pixus_mbed | 0:411244c71423 | 211 | #define ACCY_PRESS_TH 5 |
| pixus_mbed | 0:411244c71423 | 212 | #define DY_RELEASE_TH (-2) |
| pixus_mbed | 0:411244c71423 | 213 | |
| pixus_mbed | 0:411244c71423 | 214 | unsigned char OutBtnState = OTS_BTN_NO_CHANGE; |
| pixus_mbed | 0:411244c71423 | 215 | static signed long AccY = 0; |
| pixus_mbed | 0:411244c71423 | 216 | static unsigned char State = RELEASE; //0:release, 1:press |
| pixus_mbed | 0:411244c71423 | 217 | |
| pixus_mbed | 0:411244c71423 | 218 | if((dx16 >= DX_ROTATE_TH)||(dx16 <= (-DX_ROTATE_TH))) |
| pixus_mbed | 0:411244c71423 | 219 | { |
| pixus_mbed | 0:411244c71423 | 220 | AccY = 0; |
| pixus_mbed | 0:411244c71423 | 221 | } |
| pixus_mbed | 0:411244c71423 | 222 | else |
| pixus_mbed | 0:411244c71423 | 223 | { |
| pixus_mbed | 0:411244c71423 | 224 | if(State == PRESS) |
| pixus_mbed | 0:411244c71423 | 225 | { |
| pixus_mbed | 0:411244c71423 | 226 | if(dy16 <= DY_RELEASE_TH) |
| pixus_mbed | 0:411244c71423 | 227 | { |
| pixus_mbed | 0:411244c71423 | 228 | State = RELEASE; |
| pixus_mbed | 0:411244c71423 | 229 | OutBtnState = OTS_BTN_RELEASE; |
| pixus_mbed | 0:411244c71423 | 230 | } |
| pixus_mbed | 0:411244c71423 | 231 | } |
| pixus_mbed | 0:411244c71423 | 232 | else |
| pixus_mbed | 0:411244c71423 | 233 | { |
| pixus_mbed | 0:411244c71423 | 234 | if(dy16 < DY_VALID_TH) |
| pixus_mbed | 0:411244c71423 | 235 | { |
| pixus_mbed | 0:411244c71423 | 236 | AccY = 0; |
| pixus_mbed | 0:411244c71423 | 237 | } |
| pixus_mbed | 0:411244c71423 | 238 | else |
| pixus_mbed | 0:411244c71423 | 239 | { |
| pixus_mbed | 0:411244c71423 | 240 | AccY += dy16; |
| pixus_mbed | 0:411244c71423 | 241 | if(AccY >= ACCY_PRESS_TH) |
| pixus_mbed | 0:411244c71423 | 242 | { |
| pixus_mbed | 0:411244c71423 | 243 | AccY = 0; |
| pixus_mbed | 0:411244c71423 | 244 | State = PRESS; |
| pixus_mbed | 0:411244c71423 | 245 | OutBtnState = OTS_BTN_PRESS; |
| pixus_mbed | 0:411244c71423 | 246 | } |
| pixus_mbed | 0:411244c71423 | 247 | } |
| pixus_mbed | 0:411244c71423 | 248 | } |
| pixus_mbed | 0:411244c71423 | 249 | } |
| pixus_mbed | 0:411244c71423 | 250 | |
| pixus_mbed | 0:411244c71423 | 251 | return OutBtnState; |
| pixus_mbed | 0:411244c71423 | 252 | } |
| pixus_mbed | 0:411244c71423 | 253 | |
| pixus_mbed | 0:411244c71423 | 254 | //----------------------------------------------------------------------- |
| pixus_mbed | 0:411244c71423 | 255 | void OTS_MotionPin_ISR(void) |
| pixus_mbed | 0:411244c71423 | 256 | { |
| pixus_mbed | 0:411244c71423 | 257 | detachInterrupt(digitalPinToInterrupt(PIN_SEN_MOTION)); |
| pixus_mbed | 0:411244c71423 | 258 | MotionPinEventTriggered=1; |
| pixus_mbed | 0:411244c71423 | 259 | } |
| pixus_mbed | 0:411244c71423 | 260 | |
| pixus_mbed | 0:411244c71423 | 261 | //----------------------------------------------------------------------- |
| pixus_mbed | 0:411244c71423 | 262 | void loop() |
| pixus_mbed | 0:411244c71423 | 263 | { |
| pixus_mbed | 0:411244c71423 | 264 | |
| pixus_mbed | 0:411244c71423 | 265 | if(digitalRead(PIN_BTN_L) == LOW)//or reset whenever idle_timer timeout |
| pixus_mbed | 0:411244c71423 | 266 | { |
| pixus_mbed | 0:411244c71423 | 267 | OTS_Reset_Variables(); |
| pixus_mbed | 0:411244c71423 | 268 | } |
| pixus_mbed | 0:411244c71423 | 269 | |
| pixus_mbed | 0:411244c71423 | 270 | if(MotionPinEventTriggered==1) |
| pixus_mbed | 0:411244c71423 | 271 | { |
| pixus_mbed | 0:411244c71423 | 272 | MotionPinEventTriggered=0;//clear flag after read 'Motion Status and Data' |
| pixus_mbed | 0:411244c71423 | 273 | OTS_Read_Motion(&deltaX16,&deltaY16); |
| pixus_mbed | 0:411244c71423 | 274 | |
| pixus_mbed | 0:411244c71423 | 275 | if(deltaX16 || deltaY16) |
| pixus_mbed | 0:411244c71423 | 276 | { |
| pixus_mbed | 0:411244c71423 | 277 | OTS_ROT_Status = OTS_Detect_Rotation(deltaX16,deltaY16); |
| pixus_mbed | 0:411244c71423 | 278 | OTS_BTN_Status = OTS_Detect_Pressing(deltaX16,deltaY16); |
| pixus_mbed | 0:411244c71423 | 279 | |
| pixus_mbed | 0:411244c71423 | 280 | if(OTS_ROT_Status == OTS_ROT_UP) |
| pixus_mbed | 0:411244c71423 | 281 | { |
| pixus_mbed | 0:411244c71423 | 282 | LED_RED_ON; |
| pixus_mbed | 0:411244c71423 | 283 | LCM_DisplayString(1,8,"Up "); |
| pixus_mbed | 0:411244c71423 | 284 | } |
| pixus_mbed | 0:411244c71423 | 285 | else if(OTS_ROT_Status == OTS_ROT_DOWN) |
| pixus_mbed | 0:411244c71423 | 286 | { |
| pixus_mbed | 0:411244c71423 | 287 | LED_GREEN_ON; |
| pixus_mbed | 0:411244c71423 | 288 | LCM_DisplayString(1,8,"Dn "); |
| pixus_mbed | 0:411244c71423 | 289 | } |
| pixus_mbed | 0:411244c71423 | 290 | |
| pixus_mbed | 0:411244c71423 | 291 | if(OTS_BTN_Status == OTS_BTN_PRESS) |
| pixus_mbed | 0:411244c71423 | 292 | { |
| pixus_mbed | 0:411244c71423 | 293 | OTS_BTN_Press_Cnt++; |
| pixus_mbed | 0:411244c71423 | 294 | if(OTS_BTN_Press_Cnt > 999) |
| pixus_mbed | 0:411244c71423 | 295 | { |
| pixus_mbed | 0:411244c71423 | 296 | OTS_BTN_Press_Cnt=0; |
| pixus_mbed | 0:411244c71423 | 297 | } |
| pixus_mbed | 0:411244c71423 | 298 | LCM_DisplayString(2,8,"Pre"); |
| pixus_mbed | 0:411244c71423 | 299 | LCM_DisplayDecimal(2,12,OTS_BTN_Press_Cnt,4); |
| pixus_mbed | 0:411244c71423 | 300 | } |
| pixus_mbed | 0:411244c71423 | 301 | else if(OTS_BTN_Status == OTS_BTN_RELEASE) |
| pixus_mbed | 0:411244c71423 | 302 | { |
| pixus_mbed | 0:411244c71423 | 303 | LCM_DisplayString(2,8,"Rel"); |
| pixus_mbed | 0:411244c71423 | 304 | } |
| pixus_mbed | 0:411244c71423 | 305 | } |
| pixus_mbed | 0:411244c71423 | 306 | |
| pixus_mbed | 0:411244c71423 | 307 | //re-enable interrupt for MOTION pin |
| pixus_mbed | 0:411244c71423 | 308 | attachInterrupt(digitalPinToInterrupt(PIN_SEN_MOTION), OTS_MotionPin_ISR, LOW); |
| pixus_mbed | 0:411244c71423 | 309 | |
| pixus_mbed | 0:411244c71423 | 310 | } |
| pixus_mbed | 0:411244c71423 | 311 | |
| pixus_mbed | 0:411244c71423 | 312 | if(digitalRead(PIN_SEN_MOTION) == HIGH) |
| pixus_mbed | 0:411244c71423 | 313 | { |
| pixus_mbed | 0:411244c71423 | 314 | LED_GREEN_OFF; |
| pixus_mbed | 0:411244c71423 | 315 | LED_RED_OFF; |
| pixus_mbed | 0:411244c71423 | 316 | } |
| pixus_mbed | 0:411244c71423 | 317 | |
| pixus_mbed | 0:411244c71423 | 318 | } |
| pixus_mbed | 0:411244c71423 | 319 | pat9125_mbed::pat9125_mbed(pat9125_mbed_state_s *state) |
| pixus_mbed | 0:411244c71423 | 320 | { |
| pixus_mbed | 0:411244c71423 | 321 | gp_state = state ; |
| pixus_mbed | 0:411244c71423 | 322 | //gp_state->p_pc->printf("PAT9125 ADDR0 %x\n", OTS_Read_Reg(0)); |
| pixus_mbed | 0:411244c71423 | 323 | gp_state->sen_status = OTS_Sensor_Init(); |
| pixus_mbed | 0:411244c71423 | 324 | gp_state->p_pc->printf("OTS_Sensor_Init\n"); |
| pixus_mbed | 0:411244c71423 | 325 | gp_state->pINT->fall(&OTS_MotionPin_ISR); //interrupt at low state |
| pixus_mbed | 0:411244c71423 | 326 | } |
| pixus_mbed | 0:411244c71423 | 327 | |
| pixus_mbed | 0:411244c71423 | 328 | void pat9125_mbed::task() |
| pixus_mbed | 0:411244c71423 | 329 | { |
| pixus_mbed | 0:411244c71423 | 330 | if(digitalRead(PIN_SEN_MOTION) == LOW) |
| pixus_mbed | 0:411244c71423 | 331 | { |
| pixus_mbed | 0:411244c71423 | 332 | //gp_state->p_pc->printf("Motion Low\n"); |
| pixus_mbed | 0:411244c71423 | 333 | MotionPinEventTriggered = 1 ; |
| pixus_mbed | 0:411244c71423 | 334 | } |
| pixus_mbed | 0:411244c71423 | 335 | loop(); |
| pixus_mbed | 0:411244c71423 | 336 | } |