AnalogOut.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_ANALOGOUT_H
  17. #define MBED_ANALOGOUT_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_ANALOGOUT) || defined(DOXYGEN_ONLY)
  20. #include "hal/analogout_api.h"
  21. #include "platform/PlatformMutex.h"
  22. namespace mbed {
  23. /** \addtogroup drivers */
  24. /** An analog output, used for setting the voltage on a pin
  25. *
  26. * @note Synchronization level: Thread safe
  27. *
  28. * Example:
  29. * @code
  30. * // Make a sawtooth output
  31. *
  32. * #include "mbed.h"
  33. *
  34. * AnalogOut tri(p18);
  35. * int main() {
  36. * while(1) {
  37. * tri = tri + 0.01;
  38. * wait_us(1);
  39. * if(tri == 1) {
  40. * tri = 0;
  41. * }
  42. * }
  43. * }
  44. * @endcode
  45. * @ingroup drivers
  46. */
  47. class AnalogOut {
  48. public:
  49. /** Create an AnalogOut connected to the specified pin
  50. *
  51. * @param pin AnalogOut pin to connect to
  52. */
  53. AnalogOut(PinName pin)
  54. {
  55. analogout_init(&_dac, pin);
  56. }
  57. /** Set the output voltage, specified as a percentage (float)
  58. *
  59. * @param value A floating-point value representing the output voltage,
  60. * specified as a percentage. The value should lie between
  61. * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
  62. * Values outside this range will be saturated to 0.0f or 1.0f.
  63. */
  64. void write(float value)
  65. {
  66. lock();
  67. analogout_write(&_dac, value);
  68. unlock();
  69. }
  70. /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
  71. *
  72. * @param value 16-bit unsigned short representing the output voltage,
  73. * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
  74. */
  75. void write_u16(unsigned short value)
  76. {
  77. lock();
  78. analogout_write_u16(&_dac, value);
  79. unlock();
  80. }
  81. /** Return the current output voltage setting, measured as a percentage (float)
  82. *
  83. * @returns
  84. * A floating-point value representing the current voltage being output on the pin,
  85. * measured as a percentage. The returned value will lie between
  86. * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
  87. *
  88. * @note
  89. * This value may not match exactly the value set by a previous write().
  90. */
  91. float read()
  92. {
  93. lock();
  94. float ret = analogout_read(&_dac);
  95. unlock();
  96. return ret;
  97. }
  98. /** An operator shorthand for write()
  99. * \sa AnalogOut::write()
  100. */
  101. AnalogOut &operator= (float percent)
  102. {
  103. // Underlying write call is thread safe
  104. write(percent);
  105. return *this;
  106. }
  107. /** An operator shorthand for write()
  108. * \sa AnalogOut::write()
  109. */
  110. AnalogOut &operator= (AnalogOut &rhs)
  111. {
  112. // Underlying write call is thread safe
  113. write(rhs.read());
  114. return *this;
  115. }
  116. /** An operator shorthand for read()
  117. * \sa AnalogOut::read()
  118. */
  119. operator float()
  120. {
  121. // Underlying read call is thread safe
  122. return read();
  123. }
  124. virtual ~AnalogOut()
  125. {
  126. // Do nothing
  127. }
  128. protected:
  129. virtual void lock()
  130. {
  131. _mutex.lock();
  132. }
  133. virtual void unlock()
  134. {
  135. _mutex.unlock();
  136. }
  137. dac_t _dac;
  138. PlatformMutex _mutex;
  139. };
  140. } // namespace mbed
  141. #endif
  142. #endif