added init function from NXP Rapid ioT SDK, read_proximity_sensors function now works well.

Dependents:   rIoTwear-touch rIoTwear-snake

Revision:
2:f88a77463a32
Parent:
1:aa30dc96dc77
Child:
3:b0a2ba594005
--- a/sx9500.cpp	Fri May 08 01:32:54 2015 +0000
+++ b/sx9500.cpp	Thu Dec 19 17:06:08 2019 +0000
@@ -28,10 +28,100 @@
     printf("%d useful:0x%02x%02x\r\n", CSn, buf[0], buf[1]);
     
     read(SX9500_REG_AVGMSB, buf, 2);
-    printf("%d   avg:0x%02x%02x\r\n", CSn, buf[0], buf[1]);
+    //printf("%d   avg:0x%02x%02x\r\n", CSn, buf[0], buf[1]);
     
     read(SX9500_REG_DIFFMSB, buf, 2);
-    printf("%d  diff:0x%02x%02x\r\n", CSn, buf[0], buf[1]);    
+    // printf("%d  diff:0x%02x%02x\r\n", CSn, buf[0], buf[1]);    
+}
+
+
+/*
+ * Copyright (c) 2018 NXP
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice, this list
+ *   of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice, this
+ *   list of conditions and the following disclaimer in the documentation and/or
+ *   other materials provided with the distribution.
+ *
+ * o Neither the name of the copyright holder nor the names of its
+ *   contributors may be used to endorse or promote products derived from this
+ *   software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+uint8_t SX9500::init(void)
+{
+    uint8_t lenRegTable = sizeof(sx9500_i2c_reg_setup) / sizeof(smtc_reg_data_t);
+    uint8_t val;
+    uint8_t i = 0;    
+    
+    // read IRQSRC to release NIRQ pin
+    read(SX9500_REG_IRQSRC, &val,1);
+    
+    while (i < lenRegTable)
+    {
+        /* Write all registers/values contained in i2c_reg */
+        write(sx9500_i2c_reg_setup[i].reg, sx9500_i2c_reg_setup[i].val);
+        
+        /* Read back value from register and verify write */
+        val = read_single(sx9500_i2c_reg_setup[i].reg);
+        
+        if (val != sx9500_i2c_reg_setup[i].val)
+        {
+            return SX9500_INTERNAL_ERROR;
+        }
+
+        i++;
+    }
+
+return SX9500_SUCCESS;
+
+}
+
+/*
+ * END Copyright (c) 2018 NXP
+ *
+*/
+
+SX9500_TouchState_t SX9500::read_proximity_sensors()
+{
+    SX9500_TouchState_t touchState;       
+    RegStat_t prox;
+        
+    read(SX9500_REG_STAT, &prox.octet, 2);
+
+    memset((void *)&touchState, 0, sizeof(SX9500_TouchState_t));
+    if(prox.octet > 0)
+    {
+            if(prox.bits.proxstat0)
+              touchState.downPressed = true;
+                        
+            if(prox.bits.proxstat1)
+              touchState.rightPressed = true;
+            
+            if(prox.bits.proxstat2)            
+              touchState.upPressed = true;
+            
+            if(prox.bits.proxstat3)            
+              touchState.leftPressed = true;                    
+    }
+       
+    return touchState;
 }
 
 void SX9500::write(uint8_t addr, uint8_t data)