analog.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*********************************************************************
  2. *
  3. * Analog library for Fraise pic18f device
  4. *
  5. *
  6. *********************************************************************
  7. * Author Date Comment
  8. *********************************************************************
  9. * Antoine Rousseau march 2013 Original.
  10. ********************************************************************/
  11. /*
  12. # This program is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU General Public License
  14. # as published by the Free Software Foundation; either version 2
  15. # of the License, or (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24. # MA 02110-1301, USA.
  25. */
  26. #ifndef _ANALOG__H_
  27. #define _ANALOG__H_
  28. /** @file */
  29. /** @defgroup analog Analog module
  30. * Automates the use of analog input pins.
  31. * Example :
  32. * \include analog/examples/example1/main.c
  33. * @{
  34. */
  35. #include <fruit.h>
  36. /** \name Settings to put in config.h
  37. These parameters can be overloaded in the config.h of your firmware.
  38. */
  39. //@{
  40. #ifndef ANALOG_MAX_CHANNELS
  41. #define ANALOG_MAX_CHANNELS 16 /**< @brief default 16.*/
  42. #endif
  43. #ifndef ANALOG_FILTER
  44. #define ANALOG_FILTER 3 /**< @brief default 3 ; analog values are filtered and multiplied by 1<<ANALOG_FILTER. */
  45. #endif
  46. #ifndef ANALOG_THRESHOLD
  47. /** @brief default 7 : don't send an analog channel until it differs from last sent value by more than ANALOG_THRESHOLD. */
  48. #define ANALOG_THRESHOLD 7
  49. #endif
  50. #ifndef ANALOG_MINMAX_MARGIN
  51. /** @brief default 100 : increase the minimum measured value by this amount (and decrease max value) for scaling output. */
  52. #define ANALOG_MINMAX_MARGIN 100
  53. #endif
  54. #ifndef ANALOG_SCALED_MAX
  55. /** @brief default 16383 : maximum scaled output value. */
  56. #define ANALOG_SCALED_MAX 16383
  57. #endif
  58. //@}
  59. /** \name Output mode switchs
  60. The different mode switchs can be OR-ed together and passed to analogSetMode().
  61. Default is AMODE_NUM.
  62. */
  63. //@{
  64. /** @brief Map each channel to a normalized scale (see analogScaling() ). */
  65. #define AMODE_SCALE 1
  66. /** @brief Send values in a raw (numerical) message, parsed by analog/parse.pd patch. Otherwise send text messages : "A channel value".*/
  67. #define AMODE_NUM 2
  68. /** @brief Crossing mode.
  69. If channel has been set (see analogSet() ), wait the measurement value has crossed the set value before to continue to send value updates.*/
  70. #define AMODE_CROSS 4
  71. //@}
  72. void analogSelectAdc(unsigned char chan,unsigned char hwchan); // attach a hardware channel to an analog channel
  73. void analogSelectAdcTouch(unsigned char chan,unsigned char hwchan, unsigned char *port, unsigned char bit); // attach a hardware channel to an touch channel
  74. #define analogSelectTouch_(num,adchan, port, bit) do { analogSelectAdcTouch(num, adchan, &PORT##port, bit); } while(0)
  75. /** \name Initialization
  76. */
  77. //@{
  78. /** @brief Init the module in setup() */
  79. void analogInit();
  80. /** @brief Enable capacitive touch function in setup() */
  81. void analogInitTouch();
  82. /// @brief Select a pin for an analog channel.
  83. /** @param num Number of the channel (first channel = 0)
  84. @param conn Symbol of the pin (example : K1 for connector 1)
  85. */
  86. #define analogSelect(num,conn) do { pinModeAnalogIn(conn); CALL_FUN2(analogSelectAdc,num,KAN(conn)); } while(0)
  87. /// @brief Select a pin for a capacitive touch channel and prepare it for capacitive measurement.
  88. /** @param num Number of the channel (first channel = 0)
  89. @param conn Symbol of the pin (example : K1 for connector 1)
  90. */
  91. #define analogSelectTouch(num,conn) do { CALL_FUN4(analogSelectTouch_,num, KAN(conn), KPORT(conn), KBIT(conn)); } while(0)
  92. /// @brief Configure the way analog values are sent (use Output mode switchs).
  93. void analogSetMode(unsigned char mode);
  94. //@}
  95. /** \name Main loop functions
  96. */
  97. //@{
  98. /// @brief Module service routine, to be called by the main loop().
  99. /// @return channel currently sampled.
  100. unsigned char analogService(void);
  101. /** @brief Send analog values that changed. *//**
  102. Call at the maximum rate you want to report analog.
  103. The way values are sent depends on the Output mode switchs. See analogSetMode().
  104. @return number of channels sent (max 4) */
  105. char analogSend(void);
  106. //@}
  107. /** \name Utilities
  108. */
  109. //@{
  110. void analogDeselect(unsigned char chan); ///< @brief Deselect a channel.
  111. /// @brief Set the value of a channel (to be used in conjunction with AMODE_CROSS).
  112. void analogSet(unsigned char chan, int val);
  113. /// @brief Get the last measured value of a channel.
  114. int analogGet(unsigned char chan);
  115. /// @brief Get the distance between the last measured value of a channel and its internal value (set by analogSet() )
  116. int analogGetDistance(unsigned char chan);
  117. /// @brief Start or stop the scaling calibration
  118. /// @param scaling 1:start 0:stop
  119. /// First use analogScaling(1) to start calibration, which will measure the minimum and maximum values for each channel ; then stop calibration with analogScaling(0).
  120. void analogScaling(unsigned char scaling); // when scaling, min and max are updated each sample
  121. /// @brief EEPROM declaration for this module
  122. /// Call this function in your EEdeclareMain() if you want to save analog scaling calibration.
  123. void analogDeclareEE();
  124. //@}
  125. /** @}
  126. */
  127. #endif