├── .gitattributes ├── .gitignore ├── LICENSE.md ├── README.md ├── examples ├── I2C_Gesture_Demo │ └── I2C_Gesture_Demo.ino ├── I2C_Gesture_Interrupt │ └── I2C_Gesture_Interrupt.ino ├── I2C_ZX_Demo │ └── I2C_ZX_Demo.ino ├── I2C_ZX_Interrupt │ └── I2C_ZX_Interrupt.ino ├── UART_Gesture_Demo │ └── UART_Gesture_Demo.ino └── UART_ZX_Demo │ └── UART_ZX_Demo.ino ├── keywords.txt ├── library.properties └── src ├── ZX_Sensor.cpp └── ZX_Sensor.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## SparkFun Useful stuff 3 | ################# 4 | 5 | ## AVR Development 6 | *.eep 7 | *.elf 8 | *.lst 9 | *.lss 10 | *.sym 11 | *.d 12 | *.o 13 | *.srec 14 | *.map 15 | 16 | ## Notepad++ backup files 17 | *.bak 18 | 19 | ## BOM files 20 | *bom* 21 | 22 | ################# 23 | ## Eclipse 24 | ################# 25 | 26 | *.pydevproject 27 | .project 28 | .metadata 29 | bin/ 30 | tmp/ 31 | *.tmp 32 | *.bak 33 | *.swp 34 | *~.nib 35 | local.properties 36 | .classpath 37 | .settings/ 38 | .loadpath 39 | 40 | # External tool builders 41 | .externalToolBuilders/ 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | 53 | ############# 54 | ## Eagle 55 | ############# 56 | 57 | # Ignore the board and schematic backup files 58 | *.b#? 59 | *.s#? 60 | 61 | 62 | ################# 63 | ## Visual Studio 64 | ################# 65 | 66 | ## Ignore Visual Studio temporary files, build results, and 67 | ## files generated by popular Visual Studio add-ons. 68 | 69 | # User-specific files 70 | *.suo 71 | *.user 72 | *.sln.docstates 73 | 74 | # Build results 75 | [Dd]ebug/ 76 | [Rr]elease/ 77 | *_i.c 78 | *_p.c 79 | *.ilk 80 | *.meta 81 | *.obj 82 | *.pch 83 | *.pdb 84 | *.pgc 85 | *.pgd 86 | *.rsp 87 | *.sbr 88 | *.tlb 89 | *.tli 90 | *.tlh 91 | *.tmp 92 | *.vspscc 93 | .builds 94 | *.dotCover 95 | 96 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 97 | #packages/ 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opensdf 104 | *.sdf 105 | 106 | # Visual Studio profiler 107 | *.psess 108 | *.vsp 109 | 110 | # ReSharper is a .NET coding add-in 111 | _ReSharper* 112 | 113 | # Installshield output folder 114 | [Ee]xpress 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish 128 | 129 | # Others 130 | [Bb]in 131 | [Oo]bj 132 | sql 133 | TestResults 134 | *.Cache 135 | ClientBin 136 | stylecop.* 137 | ~$* 138 | *.dbmdl 139 | Generated_Code #added for RIA/Silverlight projects 140 | 141 | # Backup & report files from converting an old project file to a newer 142 | # Visual Studio version. Backup files are not needed, because we have git ;-) 143 | _UpgradeReport_Files/ 144 | Backup*/ 145 | UpgradeLog*.XML 146 | 147 | 148 | ############ 149 | ## Windows 150 | ############ 151 | 152 | # Windows image file caches 153 | Thumbs.db 154 | 155 | # Folder config file 156 | Desktop.ini 157 | 158 | 159 | ############# 160 | ## Python 161 | ############# 162 | 163 | *.py[co] 164 | 165 | # Packages 166 | *.egg 167 | *.egg-info 168 | dist 169 | build 170 | eggs 171 | parts 172 | bin 173 | var 174 | sdist 175 | develop-eggs 176 | .installed.cfg 177 | 178 | # Installer logs 179 | pip-log.txt 180 | 181 | # Unit test / coverage reports 182 | .coverage 183 | .tox 184 | 185 | #Translations 186 | *.mo 187 | 188 | #Mr Developer 189 | .mr.developer.cfg 190 | 191 | # Mac crap 192 | .DS_Store 193 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | SparkFun License Information 2 | ============================ 3 | 4 | SparkFun uses two different licenses for our files - one for hardware and one for code. 5 | 6 | Hardware 7 | --------- 8 | 9 | **SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** 10 | 11 | Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). 12 | 13 | You are free to: 14 | 15 | Share — copy and redistribute the material in any medium or format 16 | Adapt — remix, transform, and build upon the material 17 | for any purpose, even commercially. 18 | The licensor cannot revoke these freedoms as long as you follow the license terms. 19 | Under the following terms: 20 | 21 | Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. 22 | ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. 23 | No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. 24 | Notices: 25 | 26 | You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. 27 | No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 28 | 29 | 30 | Code 31 | -------- 32 | 33 | **SparkFun code, firmware, and software is released under the [MIT License](http://opensource.org/licenses/MIT).** 34 | 35 | The MIT License (MIT) 36 | 37 | Copyright (c) 2015 SparkFun Electronics 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy 40 | of this software and associated documentation files (the "Software"), to deal 41 | in the Software without restriction, including without limitation the rights 42 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 43 | copies of the Software, and to permit persons to whom the Software is 44 | furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in all 47 | copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 51 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 52 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 55 | SOFTWARE. 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SparkFun ZX Distance and Gesture Sensor Arduino Library 2 | ======================================================= 3 | 4 | ![ZX Distance and Gesture Sensor](https://cdn.sparkfun.com//assets/parts/1/0/2/7/9/13162-01a.jpg) 5 | 6 | [*SparkFun ZX Distance and Gesture Sensor (SEN-13162)*](https://www.sparkfun.com/products/13162) 7 | 8 | An Arduino library for the ZX Distance and Gesture Sensor - an infrared sensor capable of determining an object's distance from the sensor and its relative position between the IR LEDs 9 | 10 | Repository Contents 11 | ------------------- 12 | 13 | * **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE. 14 | * **/src** - Source files for the library (.cpp, .h). 15 | * **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE. 16 | * **library.properties** - General library properties for the Arduino package manager. 17 | 18 | Documentation 19 | -------------- 20 | 21 | * **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. 22 | * **[Product Repository](https://github.com/sparkfun/ZX_Gesture_Sensor_SMD)** - Main repository (including hardware files) 23 | * **[ZX Distance and Gesture Sensor SMD Hookup Guide](https://learn.sparkfun.com/tutorials/zx-distance-and-gesture-sensor-smd-hookup-guide)** - Tutorial for connecting and testing the ZX Sensor 24 | 25 | Products that use this Library 26 | --------------------------------- 27 | 28 | * [ZX Distance and Gesture Sensor](https://www.sparkfun.com/products/12780)- an infrared sensor capable of determining an object's distance from the sensor and its relative position between the IR LEDs 29 | 30 | Known Bugs 31 | ---------- 32 | 33 | * The UART gesture example does not work. A future update of the ZX's firmware is required before this feature will work. The example is kept to show how UART gestures should work in the future. 34 | 35 | Product Versions 36 | ---------------- 37 | * [SEN-13162](https://www.sparkfun.com/products/13162) - ZX Distance and Gesture Sensor 38 | SMD 39 | * _[SEN-12780 (RETIRED)](https://www.sparkfun.com/products/retired/12780) - ZX Distance and Gesture Sensor_ 40 | 41 | Version History 42 | --------------- 43 | 44 | * [V_1.0.2](https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library/tree/V_1.0.2) - Fixed library name 45 | * [V_1.0.1](https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library/tree/V_1.0.1) - Fixed typo in library properties: "Sensor" to "Sensors" 46 | * [V_1.0.0](https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library/tree/V_1.0.0) - Initial release 47 | 48 | License Information 49 | ------------------- 50 | 51 | This product is _**open source**_! 52 | 53 | Please review the LICENSE.md file for license information. 54 | 55 | If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com. 56 | 57 | Distributed as-is; no warranty is given. 58 | 59 | - Your friends at SparkFun. 60 | 61 | _This is a collaboration with [XYZ Interactive](http://www.gesturesense.com/xyz)_ 62 | -------------------------------------------------------------------------------- /examples/I2C_Gesture_Demo/I2C_Gesture_Demo.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | I2C_Gesture_Demo.ino 3 | XYZ Interactive ZX Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 6, 2015 6 | https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 7 | 8 | Tests the ZX sensor's ability to read ZX data over I2C. This demo 9 | configures the ZX sensor and periodically polls for gestures. The 10 | gesture is displayed along with its "speed" (how long it takes to 11 | complete the gesture). Note that higher numbers of "speed" 12 | indicate a slower speed. 13 | 14 | Hardware Connections: 15 | 16 | Arduino Pin ZX Sensor Board Function 17 | --------------------------------------- 18 | 5V VCC Power 19 | GND GND Ground 20 | A4 DA I2C Data 21 | A5 CL I2C Clock 22 | 23 | Resources: 24 | Include Wire.h and ZX_Sensor.h 25 | 26 | Development environment specifics: 27 | Written in Arduino 1.6.3 28 | Tested with a SparkFun RedBoard 29 | 30 | This code is beerware; if you see me (or any other SparkFun 31 | employee) at the local, and you've found our code helpful, please 32 | buy us a round! 33 | 34 | Distributed as-is; no warranty is given. 35 | ****************************************************************/ 36 | 37 | #include 38 | #include 39 | 40 | // Constants 41 | const int ZX_ADDR = 0x10; // ZX Sensor I2C address 42 | 43 | // Global Variables 44 | ZX_Sensor zx_sensor = ZX_Sensor(ZX_ADDR); 45 | GestureType gesture; 46 | uint8_t gesture_speed; 47 | 48 | void setup() { 49 | 50 | uint8_t ver; 51 | 52 | // Initialize Serial port 53 | Serial.begin(9600); 54 | Serial.println(); 55 | Serial.println("----------------------------------------"); 56 | Serial.println("SparkFun/GestureSense - I2C Gesture Demo"); 57 | Serial.println("Note: higher 'speed' numbers mean slower"); 58 | Serial.println("----------------------------------------"); 59 | 60 | // Initialize ZX Sensor (configure I2C and read model ID) 61 | if ( zx_sensor.init() ) { 62 | Serial.println("ZX Sensor initialization complete"); 63 | } else { 64 | Serial.println("Something went wrong during ZX Sensor init!"); 65 | } 66 | 67 | // Read the model version number and ensure the library will work 68 | ver = zx_sensor.getModelVersion(); 69 | if ( ver == ZX_ERROR ) { 70 | Serial.println("Error reading model version number"); 71 | } else { 72 | Serial.print("Model version: "); 73 | Serial.println(ver); 74 | } 75 | if ( ver != ZX_MODEL_VER ) { 76 | Serial.print("Model version needs to be "); 77 | Serial.print(ZX_MODEL_VER); 78 | Serial.print(" to work with this library. Stopping."); 79 | while(1); 80 | } 81 | 82 | // Read the register map version and ensure the library will work 83 | ver = zx_sensor.getRegMapVersion(); 84 | if ( ver == ZX_ERROR ) { 85 | Serial.println("Error reading register map version number"); 86 | } else { 87 | Serial.print("Register Map Version: "); 88 | Serial.println(ver); 89 | } 90 | if ( ver != ZX_REG_MAP_VER ) { 91 | Serial.print("Register map version needs to be "); 92 | Serial.print(ZX_REG_MAP_VER); 93 | Serial.print(" to work with this library. Stopping."); 94 | while(1); 95 | } 96 | 97 | } 98 | 99 | void loop() { 100 | 101 | // If there is gesture data available, read and print it 102 | if ( zx_sensor.gestureAvailable() ) { 103 | gesture = zx_sensor.readGesture(); 104 | gesture_speed = zx_sensor.readGestureSpeed(); 105 | switch ( gesture ) { 106 | case NO_GESTURE: 107 | Serial.println("No Gesture"); 108 | break; 109 | case RIGHT_SWIPE: 110 | Serial.print("Right Swipe. Speed: "); 111 | Serial.println(gesture_speed, DEC); 112 | break; 113 | case LEFT_SWIPE: 114 | Serial.print("Left Swipe. Speed: "); 115 | Serial.println(gesture_speed, DEC); 116 | break; 117 | case UP_SWIPE: 118 | Serial.print("Up Swipe. Speed: "); 119 | Serial.println(gesture_speed, DEC); 120 | break; 121 | default: 122 | break; 123 | } 124 | } 125 | } -------------------------------------------------------------------------------- /examples/I2C_Gesture_Interrupt/I2C_Gesture_Interrupt.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | I2C_Gesture_Interrupt.ino 3 | XYZ Interactive ZX Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 6, 2015 6 | https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 7 | 8 | Tests the ZX sensor's ability to read gesture data over I2C using 9 | an interrupt pin. This program configures I2C and sets up an 10 | interrupt to occur whenever the ZX Sensor throws its DR pin high. 11 | The gesture is displayed along with its "speed" (how long it takes 12 | to complete the gesture). Note that higher numbers of "speed" 13 | indicate a slower speed. 14 | 15 | Hardware Connections: 16 | 17 | Arduino Pin ZX Sensor Board Function 18 | --------------------------------------- 19 | 5V VCC Power 20 | GND GND Ground 21 | A4 DA I2C Data 22 | A5 CL I2C Clock 23 | 2 DR Data Ready 24 | 25 | Resources: 26 | Include Wire.h and ZX_Sensor.h 27 | 28 | Development environment specifics: 29 | Written in Arduino 1.6.3 30 | Tested with a SparkFun RedBoard 31 | 32 | This code is beerware; if you see me (or any other SparkFun 33 | employee) at the local, and you've found our code helpful, please 34 | buy us a round! 35 | 36 | Distributed as-is; no warranty is given. 37 | ****************************************************************/ 38 | 39 | #include 40 | #include 41 | 42 | // Constants 43 | const int ZX_ADDR = 0x10; // ZX Sensor I2C address 44 | const int INTERRUPT_NUM = 0; // Pin 2 on the UNO 45 | 46 | // Global Variables 47 | ZX_Sensor zx_sensor = ZX_Sensor(ZX_ADDR); 48 | volatile GestureType gesture; 49 | volatile bool interrupt_flag; 50 | uint8_t gesture_speed; 51 | 52 | void setup() { 53 | 54 | uint8_t ver; 55 | 56 | // Initialize gesture to no gesture 57 | gesture = NO_GESTURE; 58 | 59 | // Initialize Serial port 60 | Serial.begin(9600); 61 | Serial.println(); 62 | Serial.println("---------------------------------------------"); 63 | Serial.println("SparkFun/GestureSense - I2C Gesture Interrupt"); 64 | Serial.println("Note: higher 'speed' numbers mean slower"); 65 | Serial.println("---------------------------------------------"); 66 | 67 | // Initialize ZX Sensor (configure I2C and read model ID) 68 | if ( zx_sensor.init(GESTURE_INTERRUPTS) ) { 69 | Serial.println("ZX Sensor initialization complete"); 70 | } else { 71 | Serial.println("Something went wrong during ZX Sensor init!"); 72 | } 73 | 74 | // Read the model version number and ensure the library will work 75 | ver = zx_sensor.getModelVersion(); 76 | if ( ver == ZX_ERROR ) { 77 | Serial.println("Error reading model version number"); 78 | } else { 79 | Serial.print("Model version: "); 80 | Serial.println(ver); 81 | } 82 | if ( ver != ZX_MODEL_VER ) { 83 | Serial.print("Model version needs to be "); 84 | Serial.print(ZX_MODEL_VER); 85 | Serial.print(" to work with this library. Stopping."); 86 | while(1); 87 | } 88 | 89 | // Read the register map version and ensure the library will work 90 | ver = zx_sensor.getRegMapVersion(); 91 | if ( ver == ZX_ERROR ) { 92 | Serial.println("Error reading register map version number"); 93 | } else { 94 | Serial.print("Register Map Version: "); 95 | Serial.println(ver); 96 | } 97 | if ( ver != ZX_REG_MAP_VER ) { 98 | Serial.print("Register map version needs to be "); 99 | Serial.print(ZX_REG_MAP_VER); 100 | Serial.print(" to work with this library. Stopping."); 101 | while(1); 102 | } 103 | 104 | // Initialize interrupt service routine 105 | interrupt_flag = false; 106 | zx_sensor.clearInterrupt(); 107 | attachInterrupt(INTERRUPT_NUM, interruptRoutine, RISING); 108 | Serial.println("Interrupts now configured. Gesture away!"); 109 | } 110 | 111 | void loop() { 112 | 113 | // If we have an interrupt, read and print the gesture 114 | if ( interrupt_flag ) { 115 | 116 | // Clear the interrupt flag 117 | interrupt_flag = false; 118 | 119 | // You MUST read the STATUS register to clear interrupt! 120 | zx_sensor.clearInterrupt(); 121 | 122 | // Read last gesture 123 | gesture = zx_sensor.readGesture(); 124 | gesture_speed = zx_sensor.readGestureSpeed(); 125 | switch ( gesture ) { 126 | case NO_GESTURE: 127 | Serial.println("No Gesture"); 128 | break; 129 | case RIGHT_SWIPE: 130 | Serial.print("Right Swipe. Speed: "); 131 | Serial.println(gesture_speed, DEC); 132 | break; 133 | case LEFT_SWIPE: 134 | Serial.print("Left Swipe. Speed: "); 135 | Serial.println(gesture_speed, DEC); 136 | break; 137 | case UP_SWIPE: 138 | Serial.print("Up Swipe. Speed: "); 139 | Serial.println(gesture_speed, DEC); 140 | break; 141 | default: 142 | break; 143 | } 144 | } 145 | } 146 | 147 | void interruptRoutine() { 148 | interrupt_flag = true; 149 | } -------------------------------------------------------------------------------- /examples/I2C_ZX_Demo/I2C_ZX_Demo.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | I2C_ZX_Demo.ino 3 | XYZ Interactive ZX Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 6, 2015 6 | https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 7 | 8 | Tests the ZX sensor's ability to read ZX data over I2C. This demo 9 | configures the ZX sensor and periodically polls for Z-axis and X-axis data. 10 | 11 | Hardware Connections: 12 | 13 | Arduino Pin ZX Sensor Board Function 14 | --------------------------------------- 15 | 5V VCC Power 16 | GND GND Ground 17 | A4 DA I2C Data 18 | A5 CL I2C Clock 19 | 20 | Resources: 21 | Include Wire.h and ZX_Sensor.h 22 | 23 | Development environment specifics: 24 | Written in Arduino 1.6.3 25 | Tested with a SparkFun RedBoard 26 | 27 | This code is beerware; if you see me (or any other SparkFun 28 | employee) at the local, and you've found our code helpful, please 29 | buy us a round! 30 | 31 | Distributed as-is; no warranty is given. 32 | ****************************************************************/ 33 | 34 | #include 35 | #include 36 | 37 | // Constants 38 | const int ZX_ADDR = 0x10; // ZX Sensor I2C address 39 | 40 | // Global Variables 41 | ZX_Sensor zx_sensor = ZX_Sensor(ZX_ADDR); 42 | uint8_t x_pos; 43 | uint8_t z_pos; 44 | 45 | void setup() { 46 | 47 | uint8_t ver; 48 | 49 | // Initialize Serial port 50 | Serial.begin(9600); 51 | Serial.println(); 52 | Serial.println("-----------------------------------"); 53 | Serial.println("SparkFun/GestureSense - I2C ZX Demo"); 54 | Serial.println("-----------------------------------"); 55 | 56 | // Initialize ZX Sensor (configure I2C and read model ID) 57 | if ( zx_sensor.init() ) { 58 | Serial.println("ZX Sensor initialization complete"); 59 | } else { 60 | Serial.println("Something went wrong during ZX Sensor init!"); 61 | } 62 | 63 | // Read the model version number and ensure the library will work 64 | ver = zx_sensor.getModelVersion(); 65 | if ( ver == ZX_ERROR ) { 66 | Serial.println("Error reading model version number"); 67 | } else { 68 | Serial.print("Model version: "); 69 | Serial.println(ver); 70 | } 71 | if ( ver != ZX_MODEL_VER ) { 72 | Serial.print("Model version needs to be "); 73 | Serial.print(ZX_MODEL_VER); 74 | Serial.print(" to work with this library. Stopping."); 75 | while(1); 76 | } 77 | 78 | // Read the register map version and ensure the library will work 79 | ver = zx_sensor.getRegMapVersion(); 80 | if ( ver == ZX_ERROR ) { 81 | Serial.println("Error reading register map version number"); 82 | } else { 83 | Serial.print("Register Map Version: "); 84 | Serial.println(ver); 85 | } 86 | if ( ver != ZX_REG_MAP_VER ) { 87 | Serial.print("Register map version needs to be "); 88 | Serial.print(ZX_REG_MAP_VER); 89 | Serial.print(" to work with this library. Stopping."); 90 | while(1); 91 | } 92 | } 93 | 94 | void loop() { 95 | 96 | // If there is position data available, read and print it 97 | if ( zx_sensor.positionAvailable() ) { 98 | x_pos = zx_sensor.readX(); 99 | if ( x_pos != ZX_ERROR ) { 100 | Serial.print("X: "); 101 | Serial.print(x_pos); 102 | } 103 | z_pos = zx_sensor.readZ(); 104 | if ( z_pos != ZX_ERROR ) { 105 | Serial.print(" Z: "); 106 | Serial.println(z_pos); 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /examples/I2C_ZX_Interrupt/I2C_ZX_Interrupt.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | I2C_ZX_Interrupt.ino 3 | XYZ Interactive ZX Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 6, 2015 6 | https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 7 | 8 | Tests the ZX sensor's ability to read ZX data over I2C using an 9 | interrupt pin. This program configures I2C and sets up an 10 | interrupt to occur whenever the ZX sensor throws its DR pin high. 11 | 12 | Hardware Connections: 13 | 14 | Arduino Pin ZX Sensor Board Function 15 | --------------------------------------- 16 | 5V VCC Power 17 | GND GND Ground 18 | A4 DA I2C Data 19 | A5 CL I2C Clock 20 | 2 DR Data Ready 21 | 22 | Resources: 23 | Include Wire.h and ZX_Sensor.h 24 | 25 | Development environment specifics: 26 | Written in Arduino 1.6.3 27 | Tested with a SparkFun RedBoard 28 | 29 | This code is beerware; if you see me (or any other SparkFun 30 | employee) at the local, and you've found our code helpful, please 31 | buy us a round! 32 | 33 | Distributed as-is; no warranty is given. 34 | ****************************************************************/ 35 | 36 | #include 37 | #include 38 | 39 | // Constants 40 | const int ZX_ADDR = 0x10; // ZX Sensor I2C address 41 | const int INT_PIN = 2; // Pin to look for interrupt 42 | 43 | // Global Variables 44 | ZX_Sensor zx_sensor = ZX_Sensor(ZX_ADDR); 45 | uint8_t x_pos; 46 | uint8_t z_pos; 47 | 48 | void setup() { 49 | 50 | uint8_t ver; 51 | 52 | // Initialize Serial port 53 | Serial.begin(9600); 54 | Serial.println(); 55 | Serial.println("----------------------------------------"); 56 | Serial.println("SparkFun/GestureSense - I2C ZX Interrupt"); 57 | Serial.println("----------------------------------------"); 58 | 59 | // Initialize ZX Sensor - configure I2C and set interrupts 60 | if ( zx_sensor.init(POSITION_INTERRUPTS) ) { 61 | Serial.println("ZX Sensor initialization complete"); 62 | } else { 63 | Serial.println("Something went wrong during ZX Sensor init!"); 64 | } 65 | 66 | // Read the model version number and ensure the library will work 67 | ver = zx_sensor.getModelVersion(); 68 | if ( ver == ZX_ERROR ) { 69 | Serial.println("Error reading model version number"); 70 | } else { 71 | Serial.print("Model version: "); 72 | Serial.println(ver); 73 | } 74 | if ( ver != ZX_MODEL_VER ) { 75 | Serial.print("Model version needs to be "); 76 | Serial.print(ZX_MODEL_VER); 77 | Serial.print(" to work with this library. Stopping."); 78 | while(1); 79 | } 80 | 81 | // Read the register map version and ensure the library will work 82 | ver = zx_sensor.getRegMapVersion(); 83 | if ( ver == ZX_ERROR ) { 84 | Serial.println("Error reading register map version number"); 85 | } else { 86 | Serial.print("Register Map Version: "); 87 | Serial.println(ver); 88 | } 89 | if ( ver != ZX_REG_MAP_VER ) { 90 | Serial.print("Register map version needs to be "); 91 | Serial.print(ZX_REG_MAP_VER); 92 | Serial.print(" to work with this library. Stopping."); 93 | while(1); 94 | } 95 | 96 | // Clear Sensor interrupts and wait for pin to go low 97 | zx_sensor.clearInterrupt(); 98 | delay(10); 99 | } 100 | 101 | void loop() { 102 | 103 | // If we have an interrupt, read and print ZX data 104 | if ( digitalRead(INT_PIN) ) { 105 | 106 | // Read last X and Z coordinates 107 | x_pos = zx_sensor.readX(); 108 | if ( x_pos != ZX_ERROR ) { 109 | Serial.print("X: "); 110 | Serial.print(x_pos); 111 | } 112 | z_pos = zx_sensor.readZ(); 113 | if ( z_pos != ZX_ERROR ) { 114 | Serial.print(" Z: "); 115 | Serial.println(z_pos); 116 | } 117 | 118 | // You MUST read the STATUS register to clear interrupt! 119 | zx_sensor.clearInterrupt(); 120 | } 121 | } -------------------------------------------------------------------------------- /examples/UART_Gesture_Demo/UART_Gesture_Demo.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | UART_Gesture_Demo.ino 3 | XYZ Interactive ZX Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 6, 2015 6 | https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 7 | 8 | IMPORTANT: This feature is unsupported in the ZX sensor! This example only 9 | kind of works. You will see gestures appear, but they will generally be about 10 | 1 gesture behind. A future update of the ZX's firmware is required to enable 11 | this feature. 12 | 13 | Tests the ZX sensor's ability to read gesture data over UART using 14 | SoftwareSerial. The ZX_Sensor library is needed for the 15 | UART constants. As a result, we have to link the Wire library, 16 | even though it is not used. 17 | 18 | Hardware Connections: 19 | 20 | Arduino Pin ZX Sensor Board Function 21 | --------------------------------------- 22 | 5V VCC Power 23 | GND GND Ground 24 | 10 TX Data 25 | 26 | Resources: 27 | Include SoftwareSerial.h, Wire.h, and ZX_Constants.h 28 | 29 | Development environment specifics: 30 | Written in Arduino 1.6.3 31 | Tested with a SparkFun RedBoard 32 | 33 | This code is beerware; if you see me (or any other SparkFun 34 | employee) at the local, and you've found our code helpful, please 35 | buy us a round! 36 | 37 | Distributed as-is; no warranty is given. 38 | ****************************************************************/ 39 | 40 | #include 41 | #include 42 | #include 43 | 44 | // Global Variables 45 | SoftwareSerial soft_serial(10, 11); // RX, TX 46 | uint8_t c; 47 | 48 | void setup() { 49 | 50 | // Initialize hardware serial port. Note the baud rate. 51 | Serial.begin(115200); 52 | Serial.println(); 53 | Serial.println("-----------------------------------------"); 54 | Serial.println("SparkFun/GestureSense - UART Gesture Demo"); 55 | Serial.println("Note: higher 'speed' numbers mean slower"); 56 | Serial.println("-----------------------------------------"); 57 | 58 | // Initialize software serial port 59 | soft_serial.begin(115200); 60 | } 61 | 62 | void loop() { 63 | 64 | // Read in a character from the UART 65 | if ( soft_serial.available() ) { 66 | c = soft_serial.read(); 67 | 68 | // Determine type of message and print it 69 | switch ( c ) { 70 | case ZX_UART_GESTURE: 71 | 72 | // Determine type of gesture 73 | c = soft_serial.read(); 74 | switch ( c ) { 75 | case RIGHT_SWIPE: 76 | c = soft_serial.read(); 77 | Serial.print("Right Swipe. Speed: "); 78 | Serial.println(c, DEC); 79 | break; 80 | case LEFT_SWIPE: 81 | c = soft_serial.read(); 82 | Serial.print("Left Swipe. Speed: "); 83 | Serial.println(c, DEC); 84 | break; 85 | case UP_SWIPE: 86 | c = soft_serial.read(); 87 | Serial.print("Up Swipe. Speed: "); 88 | Serial.println(c, DEC); 89 | default: 90 | break; 91 | } 92 | 93 | break; 94 | case ZX_UART_ID: 95 | Serial.print("Sensor type: "); 96 | c = soft_serial.read(); 97 | Serial.print(c, DEC); 98 | Serial.print(" HW version: "); 99 | c = soft_serial.read(); 100 | Serial.print(c, DEC); 101 | Serial.print(" FW version: "); 102 | c = soft_serial.read(); 103 | Serial.println(c, DEC); 104 | break; 105 | default: 106 | break; 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /examples/UART_ZX_Demo/UART_ZX_Demo.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | UART_ZX_Demo.ino 3 | XYZ Interactive ZX Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 6, 2015 6 | https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 7 | 8 | Tests the ZX sensor's ability to read ZX data over UART using 9 | SoftwareSerial. The ZX_Sensor library is needed for the 10 | UART constants. As a result, we have to link the Wire library, 11 | even though it is not used. 12 | 13 | Hardware Connections: 14 | 15 | Arduino Pin ZX Sensor Board Function 16 | --------------------------------------- 17 | 5V VCC Power 18 | GND GND Ground 19 | 10 TX Data 20 | 21 | Resources: 22 | Include SoftwareSerial.h, Wire.h, and ZX_Constants.h 23 | 24 | Development environment specifics: 25 | Written in Arduino 1.6.3 26 | Tested with a SparkFun RedBoard 27 | 28 | This code is beerware; if you see me (or any other SparkFun 29 | employee) at the local, and you've found our code helpful, please 30 | buy us a round! 31 | 32 | Distributed as-is; no warranty is given. 33 | ****************************************************************/ 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | // Global Variables 40 | SoftwareSerial soft_serial(10, 11); // RX, TX 41 | uint8_t c; 42 | 43 | void setup() { 44 | 45 | // Initialize hardware serial port. Note the baud rate. 46 | Serial.begin(115200); 47 | Serial.println(); 48 | Serial.println("------------------------------------"); 49 | Serial.println("SparkFun/GestureSense - UART ZX Demo"); 50 | Serial.println("------------------------------------"); 51 | 52 | // Initialize software serial port 53 | soft_serial.begin(115200); 54 | } 55 | 56 | void loop() { 57 | 58 | // Read in a character from the UART 59 | if ( soft_serial.available() ) { 60 | c = soft_serial.read(); 61 | 62 | // Determine type of message and print it 63 | switch ( c ) { 64 | case ZX_UART_END: 65 | Serial.println("End of stream"); 66 | break; 67 | case ZX_UART_RANGES: 68 | Serial.print("Ranges: "); 69 | c = soft_serial.read(); 70 | Serial.print(c, DEC); 71 | Serial.print(", "); 72 | c = soft_serial.read(); 73 | Serial.println(c, DEC); 74 | break; 75 | case ZX_UART_X: 76 | Serial.print("X: "); 77 | c = soft_serial.read(); 78 | Serial.println(c, DEC); 79 | break; 80 | case ZX_UART_Z: 81 | Serial.print("Z: "); 82 | c = soft_serial.read(); 83 | Serial.println(c, DEC); 84 | break; 85 | case ZX_UART_ID: 86 | Serial.print("Sensor type: "); 87 | c = soft_serial.read(); 88 | Serial.print(c, DEC); 89 | Serial.print(" HW version: "); 90 | c = soft_serial.read(); 91 | Serial.print(c, DEC); 92 | Serial.print(" FW version: "); 93 | c = soft_serial.read(); 94 | Serial.println(c, DEC); 95 | break; 96 | default: 97 | break; 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ######################################################## 2 | # Syntax Coloring Map For ZX Distance and Gesture Sensor 3 | ######################################################## 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | 16 | 17 | ####################################### 18 | # Constants (LITERAL1) 19 | ####################################### 20 | 21 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=SparkFun ZX Distance and Gesture Sensor 2 | version=1.0.1 3 | author=SparkFun Electronics 4 | maintainer=SparkFun Electronics 5 | sentence=The ZX Sensor uses infrared light to determine the distance from an object and where the object is located on the X axis (between IR LEDs), available from SparkFun Electronics 6 | paragraph=The ZX Distance and Gesture Sensor is sensor capable of sensing an object's distance from the sensor and its location between the IR LEDs. 7 | category=Sensors 8 | url=https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library 9 | architectures=* -------------------------------------------------------------------------------- /src/ZX_Sensor.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ZX_Sensor.cpp 3 | * @brief Library for the SparkFun/GestureSense ZX Sensor 4 | * @author Shawn Hymel (SparkFun Electronics) 5 | * 6 | * @copyright This code is public domain but you buy me a beer if you use 7 | * this and we meet someday (Beerware license). 8 | * 9 | * This library interfaces the XYZ Interactive ZX Sensor to Arduino over I2C. 10 | * The library relies on the Arduino Wire (I2C) library. To use the library, 11 | * instantiate a ZX_Sensor object, call init(), and call the desired 12 | * functions. 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | #include "ZX_Sensor.h" 19 | 20 | /******************************************************************************* 21 | * Initialization 22 | ******************************************************************************/ 23 | 24 | /** 25 | * @brief Constructor - Instantiates ZX_Sensor object 26 | */ 27 | ZX_Sensor::ZX_Sensor(int address) 28 | { 29 | addr_ = address; 30 | } 31 | 32 | /** 33 | * @brief Destructor 34 | */ 35 | ZX_Sensor::~ZX_Sensor() 36 | { 37 | 38 | } 39 | 40 | /** 41 | * @brief Configures I2C communications and checks ZX sensor model number. 42 | * 43 | * @param enable_interrupts enables DR pin to assert on events 44 | * @return True if initialized successfully. False otherwise. 45 | */ 46 | bool ZX_Sensor::init( InterruptType interrupts /* = NO_INTERRUPTS */, 47 | bool active_high /* = true */) 48 | { 49 | 50 | /* Initialize I2C */ 51 | Wire.begin(); 52 | 53 | /* Enable DR interrupts based on desired interrupts */ 54 | setInterruptTrigger(interrupts); 55 | configureInterrupts(active_high, false); 56 | if ( interrupts == NO_INTERRUPTS ) { 57 | disableInterrupts(); 58 | } else { 59 | enableInterrupts(); 60 | } 61 | 62 | return true; 63 | } 64 | 65 | /** 66 | * @brief Reads the sensor model version 67 | * 68 | * @return sensor model version number 69 | */ 70 | uint8_t ZX_Sensor::getModelVersion() 71 | { 72 | uint8_t ver; 73 | 74 | if ( !wireReadDataByte(ZX_MODEL, ver) ) { 75 | return ZX_ERROR; 76 | } 77 | 78 | return ver; 79 | } 80 | 81 | /** 82 | * @brief Reads the register map version 83 | * 84 | * @return register map version number 85 | */ 86 | uint8_t ZX_Sensor::getRegMapVersion() 87 | { 88 | uint8_t ver; 89 | 90 | if ( !wireReadDataByte(ZX_REGVER, ver) ) { 91 | return ZX_ERROR; 92 | } 93 | 94 | return ver; 95 | } 96 | 97 | /******************************************************************************* 98 | * Interrupt Configuration 99 | ******************************************************************************/ 100 | 101 | /** 102 | * @brief Sets the triggers that cause the DR pin to change 103 | * 104 | * @param[in] interrupts which types of interrupts to enable 105 | * @return True if operation successful. False otherwise. 106 | */ 107 | bool ZX_Sensor::setInterruptTrigger(InterruptType interrupts) 108 | { 109 | 110 | #if DEBUG 111 | Serial.print(F("Setting interrupts: ")); 112 | Serial.println(interrupts); 113 | #endif 114 | 115 | switch ( interrupts ) { 116 | case POSITION_INTERRUPTS: 117 | if ( !setRegisterBit(ZX_DRE, DRE_CRD) ) { 118 | return false; 119 | } 120 | break; 121 | case GESTURE_INTERRUPTS: 122 | if ( !setRegisterBit(ZX_DRE, DRE_SWP) ) { 123 | return false; 124 | } 125 | if ( !setRegisterBit(ZX_DRE, DRE_HOVER) ) { 126 | return false; 127 | } 128 | if ( !setRegisterBit(ZX_DRE, DRE_HVG) ) { 129 | return false; 130 | } 131 | break; 132 | case ALL_INTERRUPTS: 133 | if ( !wireWriteDataByte(ZX_DRE, SET_ALL_DRE) ) { 134 | return false; 135 | } 136 | break; 137 | default: 138 | if ( !wireWriteDataByte(ZX_DRE, 0x00) ) { 139 | return false; 140 | } 141 | break; 142 | } 143 | 144 | #if DEBUG 145 | uint8_t val; 146 | wireReadDataByte(ZX_DRE, val); 147 | Serial.print(F("ZX_DRE: b")); 148 | Serial.println(val, BIN); 149 | #endif 150 | 151 | return true; 152 | } 153 | 154 | /** 155 | * @brief Configured the behavior of the DR pin on an interrupt 156 | * 157 | * @param[in] active_high true for DR active-high. False for active-low. 158 | * @param[in] pin_pulse true: DR pulse. False: DR pin asserts until STATUS read 159 | * @return True if operation successful. False otherwise. 160 | */ 161 | bool ZX_Sensor::configureInterrupts( bool active_high, 162 | bool pin_pulse /* = false */) 163 | { 164 | /* Set or clear polarity bit to make DR active-high or active-low */ 165 | if ( active_high ) { 166 | if ( !setRegisterBit(ZX_DRCFG, DRCFG_POLARITY) ) { 167 | return false; 168 | } 169 | } else { 170 | if ( !clearRegisterBit(ZX_DRCFG, DRCFG_POLARITY) ) { 171 | return false; 172 | } 173 | } 174 | 175 | /* Set or clear edge bit to make DR pulse or remain set until STATUS read */ 176 | if ( pin_pulse ) { 177 | if ( !setRegisterBit(ZX_DRCFG, DRCFG_EDGE) ) { 178 | return false; 179 | } 180 | } else { 181 | if ( !clearRegisterBit(ZX_DRCFG, DRCFG_EDGE) ) { 182 | return false; 183 | } 184 | } 185 | 186 | #if DEBUG 187 | uint8_t val; 188 | wireReadDataByte(ZX_DRCFG, val); 189 | Serial.print(F("ZX_DRCFG: b")); 190 | Serial.println(val, BIN); 191 | #endif 192 | 193 | return true; 194 | } 195 | 196 | /** 197 | * @brief Turns on interrupts so that DR asserts on desired events. 198 | * 199 | * @return True if operation successful. False otherwise. 200 | */ 201 | bool ZX_Sensor::enableInterrupts() 202 | { 203 | if ( !setRegisterBit(ZX_DRCFG, DRCFG_EN) ) { 204 | return false; 205 | } 206 | 207 | return true; 208 | } 209 | 210 | /** 211 | * @brief Turns off interrupts so that DR will never assert. 212 | * 213 | * @return True if operation successful. False otherwise. 214 | */ 215 | bool ZX_Sensor::disableInterrupts() 216 | { 217 | if ( !clearRegisterBit(ZX_DRCFG, DRCFG_EN) ) { 218 | return false; 219 | } 220 | 221 | return true; 222 | } 223 | 224 | /** 225 | * @brief Reads the STATUS register to clear an interrupt (de-assert DR) 226 | * 227 | * @return True if operation successful. False otherwise. 228 | */ 229 | bool ZX_Sensor::clearInterrupt() 230 | { 231 | uint8_t val; 232 | 233 | if ( !wireReadDataByte(ZX_STATUS, val) ) { 234 | return false; 235 | } 236 | 237 | return true; 238 | } 239 | 240 | /******************************************************************************* 241 | * Data available 242 | ******************************************************************************/ 243 | 244 | /** 245 | * @brief Indicates that new position (X or Z) data is available 246 | * 247 | * @return True if data is ready to be read. False otherwise. 248 | */ 249 | bool ZX_Sensor::positionAvailable() 250 | { 251 | uint8_t status; 252 | 253 | /* Read STATUS register and extract DAV bit */ 254 | if ( !wireReadDataByte(ZX_STATUS, status) ) { 255 | return false; 256 | } 257 | status &= 0b00000001; 258 | if ( status ) { 259 | return true; 260 | } 261 | 262 | return false; 263 | } 264 | 265 | /** 266 | * @brief Indicates that a gesture is available to be read 267 | * 268 | * @return True if gesture is ready to be read. False otherwise. 269 | */ 270 | bool ZX_Sensor::gestureAvailable() 271 | { 272 | uint8_t status; 273 | 274 | /* Read STATUS register and extract SWP bit */ 275 | if ( !wireReadDataByte(ZX_STATUS, status) ) { 276 | return false; 277 | } 278 | status &= 0b00011100; 279 | if ( status ) { 280 | return true; 281 | } 282 | 283 | return false; 284 | } 285 | 286 | /******************************************************************************* 287 | * Sensor data read 288 | ******************************************************************************/ 289 | 290 | /** 291 | * @brief Reads the X position data from the sensor 292 | * 293 | * @return 0-240 for X position. 0xFF on read error. 294 | */ 295 | uint8_t ZX_Sensor::readX() 296 | { 297 | uint8_t x_pos; 298 | 299 | /* Read X Position register and return it */ 300 | if ( !wireReadDataByte(ZX_XPOS, x_pos) ) { 301 | return ZX_ERROR; 302 | } 303 | if ( x_pos > MAX_X ) { 304 | return ZX_ERROR; 305 | } 306 | return x_pos; 307 | } 308 | 309 | /** 310 | * @brief Reads the Z position data from the sensor 311 | * 312 | * @return 0-240 for Z position. 0xFF on read error. 313 | */ 314 | uint8_t ZX_Sensor::readZ() 315 | { 316 | uint8_t z_pos; 317 | 318 | /* Read X Position register and return it */ 319 | if ( !wireReadDataByte(ZX_ZPOS, z_pos) ) { 320 | return ZX_ERROR; 321 | } 322 | if ( z_pos > MAX_Z ) { 323 | return ZX_ERROR; 324 | } 325 | return z_pos; 326 | } 327 | 328 | /** 329 | * @brief Reads the last detected gesture from the sensor 330 | * 331 | * 0x01 Right Swipe 332 | * 0x02 Left Swipe 333 | * 0x03 Up Swipe 334 | * 335 | * @return a number corresponding to a gesture. 0xFF on error. 336 | */ 337 | GestureType ZX_Sensor::readGesture() 338 | { 339 | uint8_t gesture; 340 | 341 | /* Read GESTURE register and return the value */ 342 | if ( !wireReadDataByte(ZX_GESTURE, gesture) ) { 343 | return NO_GESTURE; 344 | } 345 | #if DEBUG 346 | Serial.print(F("Gesture read: ")); 347 | Serial.println(gesture); 348 | #endif 349 | switch ( gesture ) { 350 | case RIGHT_SWIPE: 351 | return RIGHT_SWIPE; 352 | case LEFT_SWIPE: 353 | return LEFT_SWIPE; 354 | case UP_SWIPE: 355 | return UP_SWIPE; 356 | default: 357 | return NO_GESTURE; 358 | } 359 | } 360 | 361 | /** 362 | * @brief Reads the speed of the last gesture from the sensor 363 | * 364 | * @return a number corresponding to the speed of the gesture. 0xFF on error. 365 | */ 366 | uint8_t ZX_Sensor::readGestureSpeed() 367 | { 368 | uint8_t speed; 369 | 370 | /* Read GESTURE register and return the value */ 371 | if ( !wireReadDataByte(ZX_GSPEED, speed) ) { 372 | return ZX_ERROR; 373 | } 374 | 375 | return speed; 376 | } 377 | 378 | /******************************************************************************* 379 | * Bit Manipulation 380 | ******************************************************************************/ 381 | 382 | /** 383 | * @brief sets a bit in a register over I2C 384 | * 385 | * @param[in] bit the number of the bit (0-7) to set 386 | * @return True if successful write operation. False otherwise. 387 | */ 388 | bool ZX_Sensor::setRegisterBit(uint8_t reg, uint8_t bit) 389 | { 390 | uint8_t val; 391 | 392 | /* Read value from register */ 393 | if ( !wireReadDataByte(reg, val) ) { 394 | return false; 395 | } 396 | 397 | /* Set bits in register and write back to the register */ 398 | val |= (1 << bit); 399 | if ( !wireWriteDataByte(reg, val) ) { 400 | return false; 401 | } 402 | 403 | return true; 404 | } 405 | 406 | /** 407 | * @brief clears a bit in a register over I2C 408 | * 409 | * @param[in] bit the number of the bit (0-7) to clear 410 | * @return True if successful write operation. False otherwise. 411 | */ 412 | bool ZX_Sensor::clearRegisterBit(uint8_t reg, uint8_t bit) 413 | { 414 | uint8_t val; 415 | 416 | /* Read value from register */ 417 | if ( !wireReadDataByte(reg, val) ) { 418 | return false; 419 | } 420 | 421 | /* Clear bit in register and write back to the register */ 422 | val &= ~(1 << bit); 423 | if ( !wireWriteDataByte(reg, val) ) { 424 | return false; 425 | } 426 | 427 | return true; 428 | } 429 | 430 | /******************************************************************************* 431 | * Raw I2C Reads and Writes 432 | ******************************************************************************/ 433 | 434 | /** 435 | * @brief Writes a single byte to the I2C device (no register) 436 | * 437 | * @param[in] val the 1-byte value to write to the I2C device 438 | * @return True if successful write operation. False otherwise. 439 | */ 440 | bool ZX_Sensor::wireWriteByte(uint8_t val) 441 | { 442 | Wire.beginTransmission(addr_); 443 | Wire.write(val); 444 | if( Wire.endTransmission() != 0 ) { 445 | return false; 446 | } 447 | 448 | return true; 449 | } 450 | 451 | /** 452 | * @brief Writes a single byte to the I2C device and specified register 453 | * 454 | * @param[in] reg the register in the I2C device to write to 455 | * @param[in] val the 1-byte value to write to the I2C device 456 | * @return True if successful write operation. False otherwise. 457 | */ 458 | bool ZX_Sensor::wireWriteDataByte(uint8_t reg, uint8_t val) 459 | { 460 | Wire.beginTransmission(addr_); 461 | Wire.write(reg); 462 | Wire.write(val); 463 | if( Wire.endTransmission() != 0 ) { 464 | return false; 465 | } 466 | 467 | return true; 468 | } 469 | 470 | /** 471 | * @brief Reads a single byte from the I2C device and specified register 472 | * 473 | * @param[in] reg the register to read from 474 | * @param[out] the value returned from the register 475 | * @return True if successful read operation. False otherwise. 476 | */ 477 | bool ZX_Sensor::wireReadDataByte(uint8_t reg, uint8_t &val) 478 | { 479 | 480 | /* Indicate which register we want to read from */ 481 | if (!wireWriteByte(reg)) { 482 | return false; 483 | } 484 | 485 | /* Read from register */ 486 | Wire.requestFrom(addr_, 1); 487 | while (Wire.available()) { 488 | val = Wire.read(); 489 | } 490 | 491 | return true; 492 | } -------------------------------------------------------------------------------- /src/ZX_Sensor.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ZX_Sensor.h 3 | * @brief Library for the SparkFun/GestureSense ZX Sensor 4 | * @author Shawn Hymel (SparkFun Electronics) 5 | * 6 | * @copyright This code is public domain but you buy me a beer if you use 7 | * this and we meet someday (Beerware license). 8 | * 9 | * This library interfaces the XYZ Interactive ZX Sensor to Arduino over I2C. 10 | * The library relies on the Arduino Wire (I2C) library. To use the library, 11 | * instantiate a ZX_Sensor object, call init(), and call the desired 12 | * functions. 13 | */ 14 | 15 | #ifndef ZX_SENSOR_H 16 | #define ZX_SENSOR_H 17 | 18 | #include 19 | 20 | /* Debug */ 21 | #define DEBUG 0 22 | 23 | /* Acceptable ZX Sensor version */ 24 | #define ZX_MODEL_VER 0x01 25 | 26 | /* Acceptable ZX Sensor register map */ 27 | #define ZX_REG_MAP_VER 0x01 28 | 29 | /* ZX Sensor register addresses */ 30 | #define ZX_STATUS 0x00 31 | #define ZX_DRE 0x01 32 | #define ZX_DRCFG 0x02 33 | #define ZX_GESTURE 0x04 34 | #define ZX_GSPEED 0x05 35 | #define ZX_DCM 0x06 36 | #define ZX_XPOS 0x08 37 | #define ZX_ZPOS 0x0A 38 | #define ZX_LRNG 0x0C 39 | #define ZX_RRNG 0x0E 40 | #define ZX_REGVER 0xFE 41 | #define ZX_MODEL 0xFF 42 | 43 | /* ZX Sensor bit names */ 44 | #define DRE_RNG 0 45 | #define DRE_CRD 1 46 | #define DRE_SWP 2 47 | #define DRE_HOVER 3 48 | #define DRE_HVG 4 49 | #define DRE_EDGE 5 50 | #define DRCFG_POLARITY 0 51 | #define DRCFG_EDGE 1 52 | #define DRCFG_FORCE 6 53 | #define DRCFG_EN 7 54 | 55 | /* ZX Sensor UART message headers */ 56 | #define ZX_UART_END 0xFF 57 | #define ZX_UART_RANGES 0xFE 58 | #define ZX_UART_X 0xFA 59 | #define ZX_UART_Z 0xFB 60 | #define ZX_UART_GESTURE 0xFC 61 | #define ZX_UART_ID 0xF1 62 | 63 | /* Constants */ 64 | #define ZX_ERROR 0xFF 65 | #define MAX_X 240 66 | #define MAX_Z 240 67 | #define SET_ALL_DRE 0b00111111 68 | 69 | /* Enumeration for possible gestures */ 70 | typedef enum GestureType { 71 | RIGHT_SWIPE = 0x01, 72 | LEFT_SWIPE = 0x02, 73 | UP_SWIPE = 0x03, 74 | NO_GESTURE = 0xFF 75 | } GestureType; 76 | 77 | /* Enumeration for possible interrupt enables */ 78 | typedef enum InterruptType { 79 | NO_INTERRUPTS = 0x00, 80 | POSITION_INTERRUPTS = 0x01, 81 | GESTURE_INTERRUPTS = 0x02, 82 | ALL_INTERRUPTS = 0x03 83 | } InterruptType; 84 | 85 | /* ZX Sensor Class */ 86 | class ZX_Sensor { 87 | public: 88 | 89 | /* Initialization */ 90 | ZX_Sensor(int address); 91 | ~ZX_Sensor(); 92 | bool init(InterruptType interrupts = NO_INTERRUPTS, 93 | bool active_high = true); 94 | uint8_t getModelVersion(); 95 | uint8_t getRegMapVersion(); 96 | 97 | /* Interrupt configuration */ 98 | bool setInterruptTrigger(InterruptType interrupts); 99 | bool configureInterrupts(bool active_high, bool pin_pulse = false); 100 | bool enableInterrupts(); 101 | bool disableInterrupts(); 102 | bool clearInterrupt(); 103 | 104 | /* Data available */ 105 | bool positionAvailable(); 106 | bool gestureAvailable(); 107 | 108 | /* Sensor data read */ 109 | uint8_t readX(); 110 | uint8_t readZ(); 111 | GestureType readGesture(); 112 | uint8_t readGestureSpeed(); 113 | 114 | private: 115 | 116 | /* Bit manipulation */ 117 | bool setRegisterBit(uint8_t reg, uint8_t bit); 118 | bool clearRegisterBit(uint8_t reg, uint8_t bit); 119 | 120 | /* Raw I2C reads and writes */ 121 | bool wireWriteByte(uint8_t val); 122 | bool wireWriteDataByte(uint8_t reg, uint8_t val); 123 | bool wireReadDataByte(uint8_t reg, uint8_t &val); 124 | 125 | /* Members */ 126 | int addr_; 127 | }; 128 | 129 | #endif /* ZX_SENSOR_H */ --------------------------------------------------------------------------------