PixArt / Mbed 2 deprecated PAT9125_OTS_L476RG_Program

Dependencies:   mbed

Fork of PAT9125_OTS_L476RG by PixArt

This section highlights the setup and operating guide to integrating PAT9125EL with the Nucleo L476RG.

Setup

Hardware:

  • 1. PC with Windows Operating System
  • 2. Nucleo L476RG Dev Kit Board from ST
  • 3. PAT9125EL Arduino Enabled Board
  • 4. USB Cable (one end with miniUSB)

Connection:

Please connect the board as show below.

/media/uploads/pixus_mbed/connection_with_labeling.jpg

Supplying Power

Connect the one end of the USB to the PC USB and the other to the Nucleo L476RG miniUSB as shown in the figure below.

/media/uploads/pixus_mbed/connection_with_power_led_red.jpg

I am assuming that you already know and are able to download the code at this point. Once programmed with the latest compiled .bin file, the LED should turn Green, as illustrated below.

/media/uploads/pixus_mbed/connection_with_power_led_green.jpg

Next, we will test to see if the sensor (PAT9125EL) is indeed functional, and able to display the rotational displacement, on the on-board LCD Display.

While you rotate the Rotational Shaft clockwise, you should see an increment in the digital count for "SHAFT" on the LCD Display, also showing "Up" as you rotate the shaft in the clockwise direction.

While you rotate the Rotational Shaft counter-clockwise, you should see decrease in the digital counts under "SHAFT"on the LCD Display, also showing "Down" as you rotate the shaft in the counter-clockwise direction.

While you push the shaft inward, you should see an increment in digital counts under SPRING. While pushed inward, it will show as "Pre" for Press on the LCD Display.

Conversely, once you release the shaft, the LCD Display should show "Rel" for "Release".

To RESET the digital count, press on the button on the bottom left side of the board. This should reset the digital count on "SHAFT" and "SPRING" to "+0000".

Have fun with this and should you have any questions, please do not hesitate to contact us.

Committer:
pixus_mbed
Date:
Wed Oct 18 10:01:44 2017 +0000
Branch:
Branch_nRF51
Revision:
4:1cd61816c013
Parent:
0:411244c71423
Child:
13:1efc192be0f5
Support UART command mode.

Who changed what in which revision?

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