No Description

rgbringcoder.cxx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Author: Jon Trulson <jtrulson@ics.com>
  3. * Copyright (c) 2015 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <unistd.h>
  25. #include <iostream>
  26. #include "rgbringcoder.h"
  27. #include <signal.h>
  28. using namespace std;
  29. int shouldRun = true;
  30. void sig_handler(int signo)
  31. {
  32. if (signo == SIGINT)
  33. shouldRun = false;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. signal(SIGINT, sig_handler);
  38. //! [Interesting]
  39. // There are a lot of pins to hook up. These pins are valid for the
  40. // Edison board, but may need to be adjusted for other platforms.
  41. // In order:
  42. // enable - 4
  43. // latch - 10
  44. // clear - 11
  45. // clock - 2
  46. // data - 9
  47. // switch - 7
  48. // red pwm - 3
  49. // green pwm - 5
  50. // blue pwm - 6
  51. // encA - 12
  52. // encB - 13
  53. upm::RGBRingCoder *ringCoder =
  54. new upm::RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 5, 6);
  55. uint16_t spin = 0x0001;
  56. bool oldState = false;
  57. int oldPos = 0;
  58. // Lets go green
  59. ringCoder->setRGBLED(0.99, 0.01, 0.99);
  60. while (shouldRun)
  61. {
  62. // you spin me round...
  63. if (spin == 0)
  64. spin = 0x0001;
  65. ringCoder->setRingLEDS(spin);
  66. spin <<= 1;
  67. // check button state
  68. bool bstate = ringCoder->getButtonState();
  69. if (bstate != oldState)
  70. {
  71. cout << "Button state changed from " << oldState << " to "
  72. << bstate << endl;
  73. oldState = bstate;
  74. }
  75. // check encoder position
  76. int epos = ringCoder->getEncoderPosition();
  77. if (epos != oldPos)
  78. {
  79. cout << "Encoder position changed from " << oldPos << " to "
  80. << epos << endl;
  81. oldPos = epos;
  82. }
  83. usleep(100000);
  84. }
  85. //! [Interesting]
  86. delete ringCoder;
  87. return 0;
  88. }