MPM controlled 16ch RGB LED dimmer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.9 KiB

  1. /***************************************************************************
  2. * helper functions for demo modes *
  3. * *
  4. * Copyright (C) 2011 - 2012 by Olaf Rempel *
  5. * razzor AT kopf MINUS tisch DOT de *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; version 2 of the License, *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include "rgb16mpm.h"
  24. uint8_t sequence_chase(uint8_t old_pos, uint16_t mask)
  25. {
  26. uint8_t pos = (old_pos & POS_MASK);
  27. uint8_t dir = (old_pos & DIR_MASK);
  28. do {
  29. pos = (dir == DIR_DOWN) ? pos -1 : pos +1;
  30. pos &= 0x0F;
  31. if (pos == 0x00 || pos == 0x0F) {
  32. dir = (pos == 0x00) ? DIR_UP : DIR_DOWN;
  33. }
  34. } while (!((1<<pos) & mask) || pos == (old_pos & ~(DIR_MASK)));
  35. return dir | pos;
  36. }
  37. uint16_t color_ramp(uint16_t value, uint8_t *color)
  38. {
  39. uint8_t col1 = (value & 0xFF);
  40. uint8_t col2 = 0xFF - col1;
  41. switch (value >> 8) {
  42. default:
  43. value = 0x0000;
  44. /* no break */
  45. case 0: /* red: on, green: ramp up, blue: off */
  46. color[0] = 0xFF;
  47. color[1] = col1;
  48. color[2] = 0x00;
  49. break;
  50. case 1: /* red: ramp down, green: on, blue:off */
  51. color[0] = col2;
  52. color[1] = 0xFF;
  53. color[2] = 0x00;
  54. break;
  55. case 2: /* red: off, green: on, blue: ramp up */
  56. color[0] = 0x00;
  57. color[1] = 0xFF;
  58. color[2] = col1;
  59. break;
  60. case 3: /* red: off, green: ramp down: blue: on */
  61. color[0] = 0x00;
  62. color[1] = col2;
  63. color[2] = 0xFF;
  64. break;
  65. case 4: /* red: ramp up, green: off, blue: on */
  66. color[0] = col1;
  67. color[1] = 0x00;
  68. color[2] = 0xFF;
  69. break;
  70. case 5: /* red: on, green: off, blue: ramp down */
  71. color[0] = 0xFF;
  72. color[1] = 0x00;
  73. color[2] = col2;
  74. break;
  75. }
  76. return value;
  77. }
  78. void color_add(uint8_t *color1, uint8_t *color2, uint8_t *output)
  79. {
  80. uint8_t i;
  81. for (i = 0; i < 3; i++) {
  82. uint16_t tmp = color1[i] + color2[i];
  83. output[i] = (tmp & 0xFF00) ? 0xFF : tmp;
  84. }
  85. }
  86. void color_sub(uint8_t *color1, uint8_t *color2, uint8_t *output)
  87. {
  88. uint8_t i;
  89. for (i = 0; i < 3; i++) {
  90. uint16_t tmp = color1[i] - color2[i];
  91. output[i] = (tmp & 0xFF00) ? 0x00 : tmp;
  92. }
  93. }
  94. void color_div(uint8_t *color, uint8_t div, uint8_t *output)
  95. {
  96. uint8_t i;
  97. for (i = 0; i < 3; i++) {
  98. if (color[i] != 0) {
  99. output[i] = color[i] / div;
  100. if (output[i] == 0) {
  101. output[i] = 0x01;
  102. }
  103. } else {
  104. output[i] = 0x00;
  105. }
  106. }
  107. }