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.

97 lines
3.1 KiB

  1. #!/usr/bin/env python3
  2. #
  3. # This script was used to work out how to map the RGB/brightness input
  4. # data to GPIO pinout voltages.
  5. maxV = 3.27
  6. requirements = [
  7. # Input # Expected output
  8. (0, 0, 0, 1.00, False, None, None, None),
  9. (255, 255, 255, 0.00, False, None, None, None),
  10. (255, 0, 0, 0.01, True, 2.95, maxV, maxV),
  11. (255, 255, 0, 0.01, True, 3.04, 2.86, 3.17),
  12. (255, 255, 255, 0.01, True, 3.04, 2.86, 3.07),
  13. (0, 255, 0, 0.01, True, 3.27, 2.95, 3.27),
  14. (0, 255, 255, 0.01, True, 3.13, 2.86, 3.07),
  15. (0, 0, 255, 0.01, True, 3.27, 3.27, 2.95),
  16. (255, 0, 255, 0.01, True, 2.88, 3.12, 2.86),
  17. ]
  18. def get_red(r, g, b, brightness):
  19. if r == 0:
  20. if g == 1 and b == 1:
  21. v = round(3.1317 - brightness * 0.16, 2)
  22. return v
  23. else:
  24. return maxV;
  25. if g == 1:
  26. v = round(r * (3.0417 - brightness * 0.48), 2)
  27. elif b == 1:
  28. v = round(r * (2.88 - (brightness - 0.01) * (2.88 - 1.85)), 2)
  29. else:
  30. v = round(r * (2.95 - (brightness - 0.01) * (2.95 - 1.82)), 2)
  31. v = round(r * (2.9617 - brightness * 1.13), 2)
  32. return v
  33. def get_green(r, g, b, brightness):
  34. if g == 0:
  35. if r == 1 and b == 1:
  36. return round(3.12 - (brightness - 0.01) * (3.12 - 2.92), 2)
  37. else:
  38. return maxV;
  39. if r == 1:
  40. v = round(g * (2.86 - (brightness - 0.01) * (2.86 - 1.77)), 2)
  41. elif r == 0 and b == 1:
  42. v = round(g * (2.86 - (brightness - 0.01) * (2.86 - 1.77)), 2)
  43. else:
  44. v = round(g * (2.95 - (brightness - 0.01) * (2.95 - 1.82)), 2)
  45. return v
  46. def get_blue(r, g, b, brightness):
  47. if b == 0:
  48. if r > 0 and g > 0:
  49. return 3.17
  50. else:
  51. return maxV;
  52. if r == 1 and g == 1:
  53. v = round(r * (3.0717 - brightness * 0.48), 2)
  54. elif r == 1 and g == 0:
  55. v = round(r * (2.86 - (brightness - 0.01) * (2.86 - 1.77)), 2)
  56. elif r == 0 and g == 1:
  57. v = round(g * (3.0717 - brightness * 0.48), 2)
  58. else:
  59. v = round(b * (2.95 - (brightness - 0.01) * (2.95 - 1.82)), 2)
  60. return v
  61. def make_rgb_fractions(r, g, b):
  62. """ These ought to be 0 to 1 on input already, but it won't
  63. hurt to make sure they are."""
  64. m = max(r, g, b)
  65. r = r/m
  66. g = g/m
  67. b = b/m
  68. return (r, g, b)
  69. for r,g,b,brightness,stateExpected, rVexpected, gVexpected, bVexpected in requirements:
  70. print(f"IN: {r}, {g}, {b} at {brightness} / ", end="")
  71. print(f"EXP: ({stateExpected}) {rVexpected},{gVexpected},{bVexpected} ", end="")
  72. state = True
  73. if r + g + b == 0 or brightness == 0:
  74. state = False
  75. rV = None
  76. gV = None
  77. bV = None
  78. else:
  79. r, g, b = make_rgb_fractions(r, g, b)
  80. rV = get_red(r, g, b, brightness)
  81. gV = get_green(r, g, b, brightness)
  82. bV = get_blue(r, g, b, brightness)
  83. print(f"OUT: ({state}) {rV},{gV},{bV} ", end="")
  84. if rV == rVexpected and gV == gVexpected and bV == bVexpected and state == stateExpected:
  85. print(" [PASS]");
  86. else:
  87. print(" [FAIL]");