cirque_pinnacle.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2018 Cirque Corp. Restrictions apply. See: www.cirque.com/sw-license
  2. #pragma once
  3. #include "cirque_pinnacle_regdefs.h"
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #ifndef CIRQUE_PINNACLE_TIMEOUT
  7. # define CIRQUE_PINNACLE_TIMEOUT 20 // I2C timeout in milliseconds
  8. #endif
  9. #define CIRQUE_PINNACLE_ABSOLUTE_MODE 1
  10. #define CIRQUE_PINNACLE_RELATIVE_MODE 0
  11. #ifndef CIRQUE_PINNACLE_POSITION_MODE
  12. # define CIRQUE_PINNACLE_POSITION_MODE CIRQUE_PINNACLE_ABSOLUTE_MODE
  13. #endif
  14. #define CIRQUE_PINNACLE_DEFAULT_SCALE 1024
  15. #ifndef CIRQUE_PINNACLE_DIAMETER_MM
  16. # define CIRQUE_PINNACLE_DIAMETER_MM 40
  17. #endif
  18. // Coordinate scaling values
  19. #ifndef CIRQUE_PINNACLE_X_LOWER
  20. # define CIRQUE_PINNACLE_X_LOWER 127 // min "reachable" X value
  21. #endif
  22. #ifndef CIRQUE_PINNACLE_X_UPPER
  23. # define CIRQUE_PINNACLE_X_UPPER 1919 // max "reachable" X value
  24. #endif
  25. #ifndef CIRQUE_PINNACLE_Y_LOWER
  26. # define CIRQUE_PINNACLE_Y_LOWER 63 // min "reachable" Y value
  27. #endif
  28. #ifndef CIRQUE_PINNACLE_Y_UPPER
  29. # define CIRQUE_PINNACLE_Y_UPPER 1471 // max "reachable" Y value
  30. #endif
  31. #ifndef CIRQUE_PINNACLE_X_RANGE
  32. # define CIRQUE_PINNACLE_X_RANGE (CIRQUE_PINNACLE_X_UPPER - CIRQUE_PINNACLE_X_LOWER)
  33. #endif
  34. #ifndef CIRQUE_PINNACLE_Y_RANGE
  35. # define CIRQUE_PINNACLE_Y_RANGE (CIRQUE_PINNACLE_Y_UPPER - CIRQUE_PINNACLE_Y_LOWER)
  36. #endif
  37. #if !defined(POINTING_DEVICE_TASK_THROTTLE_MS)
  38. # define POINTING_DEVICE_TASK_THROTTLE_MS 10 // Cirque Pinnacle in normal operation produces data every 10ms. Advanced configuration for pen/stylus usage might require lower values.
  39. #endif
  40. #if defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c)
  41. # include "i2c_master.h"
  42. // Cirque's 7-bit I2C Slave Address
  43. # ifndef CIRQUE_PINNACLE_ADDR
  44. # define CIRQUE_PINNACLE_ADDR I2C_ADDRESS_DEFAULT
  45. # endif
  46. #elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
  47. # include "spi_master.h"
  48. # ifndef CIRQUE_PINNACLE_CLOCK_SPEED
  49. # define CIRQUE_PINNACLE_CLOCK_SPEED 10000000
  50. # endif
  51. # ifndef CIRQUE_PINNACLE_SPI_LSBFIRST
  52. # define CIRQUE_PINNACLE_SPI_LSBFIRST false
  53. # endif
  54. # ifndef CIRQUE_PINNACLE_SPI_MODE
  55. # define CIRQUE_PINNACLE_SPI_MODE 1
  56. # endif
  57. # ifndef CIRQUE_PINNACLE_SPI_DIVISOR
  58. # ifdef __AVR__
  59. # define CIRQUE_PINNACLE_SPI_DIVISOR (F_CPU / CIRQUE_PINNACLE_CLOCK_SPEED)
  60. # else
  61. # define CIRQUE_PINNACLE_SPI_DIVISOR 64
  62. # endif
  63. # ifndef CIRQUE_PINNACLE_SPI_CS_PIN
  64. # error "No Chip Select pin has been defined -- missing CIRQUE_PINNACLE_SPI_CS_PIN define"
  65. # endif
  66. # endif
  67. #endif
  68. #define DIVIDE_UNSIGNED_ROUND(numerator, denominator) (((numerator) + ((denominator) / 2)) / (denominator))
  69. #define CIRQUE_PINNACLE_INCH_TO_PX(inch) (DIVIDE_UNSIGNED_ROUND((inch) * (uint32_t)CIRQUE_PINNACLE_DIAMETER_MM * 10, 254))
  70. #define CIRQUE_PINNACLE_PX_TO_INCH(px) (DIVIDE_UNSIGNED_ROUND((px) * (uint32_t)254, CIRQUE_PINNACLE_DIAMETER_MM * 10))
  71. // Convenient way to store and access measurements
  72. typedef struct {
  73. bool valid; // true if valid data was read, false if no data was ready
  74. #if CIRQUE_PINNACLE_POSITION_MODE
  75. uint16_t xValue;
  76. uint16_t yValue;
  77. uint16_t zValue;
  78. uint8_t buttonFlags;
  79. bool touchDown;
  80. #else
  81. uint8_t xDelta;
  82. uint8_t yDelta;
  83. uint8_t wheelCount;
  84. uint8_t buttons;
  85. #endif
  86. } pinnacle_data_t;
  87. void cirque_pinnacle_init(void);
  88. void cirque_pinnacle_calibrate(void);
  89. void cirque_pinnacle_cursor_smoothing(bool enable);
  90. pinnacle_data_t cirque_pinnacle_read_data(void);
  91. void cirque_pinnacle_scale_data(pinnacle_data_t* coordinates, uint16_t xResolution, uint16_t yResolution);
  92. uint16_t cirque_pinnacle_get_scale(void);
  93. void cirque_pinnacle_set_scale(uint16_t scale);