Advertisement
Guest User

Untitled

a guest
Aug 15th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. // AMX Color Controller
  2.  
  3. //I2C Addresses
  4. const i2c_ioexp = 0x7C;
  5. const i2c_temp = 0x98;
  6. const i2c_als = 0xE8;
  7. const i2c_accel = 0x38;
  8.  
  9. //----------------------------------------
  10. //-- Configure I2C
  11. //----------------------------------------
  12. hardware.configure(I2C_89);
  13. local i2c = hardware.i2c89;
  14.  
  15. local led_r = 0;
  16. local led_g = 0;
  17. local led_b = 0;
  18. local potLevel = 0.0;
  19. //----------------------------------------
  20. //-- IO Expander Functions
  21. //----------------------------------------
  22. local function ioexp_read(addr) {
  23. local result = i2c.read(i2c_ioexp, format("%c", addr), 1);
  24. if (result == null) {
  25. server.log("i2c read fail");
  26. return -1;
  27. } else return result[0];
  28. }
  29.  
  30. local function ioexp_write(addr, data) {
  31. i2c.write(i2c_ioexp, format("%c%c",addr, data));
  32. }
  33.  
  34. local function ioexp_writebit(addr, bitn, level) {
  35. // read modify write
  36. local reg = ioexp_read(addr);
  37. reg = (level==0)?(reg&~(1<<bitn)) : (reg | (1<<bitn));
  38. ioexp_write(addr, reg)
  39. }
  40.  
  41. local function ioexp_modify_write(addr, data, mask) {
  42. local reg = ioexp_read(addr);
  43. reg = (reg & ~mask) | (data & mask);
  44. ioexp_write(addr, reg);
  45. }
  46.  
  47. local function ioexp_setpin(gpio, level) {
  48. ioexp_writebit(gpio>=8?0x10:0x11, gpio&7, level?1:0);
  49. }
  50.  
  51. local function ioexp_setdir(gpio, output) {
  52. ioexp_writebit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1);
  53. }
  54.  
  55. local function ioexp_setpullup(gpio, enable) {
  56. ioexp_writebit(gpio>=8?0x06:0x07, gpio&7, enable);
  57. }
  58.  
  59. local function ioexp_setirqmask(gpio, enable) {
  60. ioexp_writebit(gpio>=8?0x12:0x13, gpio&7, enable);
  61. }
  62.  
  63. local function ioexp_setirqedge(gpio, rising, falling) {
  64. local addr = 0x17 - (gpio>>2);
  65. local mask = 0x03 << ((gpio&3)<<1);
  66. local data = (2*falling + rising) << ((gpio&3)<<1);
  67. ioexp_modify_write(addr, data, mask);
  68. }
  69.  
  70. local function ioexp_clearirq(gpio) {
  71. ioexp_writebit(gpio>=8?0x18:0x19, gpio&7, 1);
  72. }
  73.  
  74. local function ioexp_readpin(gpio) {
  75. return (ioexp_read(gpio>=8?0x10:0x11)&(1<<(gpio&7)))?1:0;
  76. }
  77.  
  78. local function ioexp_setled(gpio, led) {
  79. ioexp_writebit(gpio>=8?0x20:0x21, gpio&7, led);
  80. }
  81.  
  82. local function ioexp_update_leds(r,g,b) {
  83. if(r != null)
  84. led_r = r;
  85. if(g != null)
  86. led_g = g;
  87. if(b != null)
  88. led_b = b;
  89. ioexp_write(0x3b, led_g);
  90. ioexp_write(0x40, led_b);
  91. ioexp_write(0x45, led_r);
  92. }
  93.  
  94. //----------------------------------------
  95. //-- Custom Classes
  96. //----------------------------------------
  97.  
  98. // Custom color class
  99. class color extends InputPort
  100. {
  101. function set(v)
  102. {
  103. // Update the color at 33%
  104. if(v.tostring().toupper().find("HEX-") > -1)
  105. {
  106. local rgb = Hex2RGB(v.slice(4));
  107. ioexp_update_leds(rgb[0] * potLevel, rgb[1] * potLevel, rgb[2] * potLevel);
  108. }
  109.  
  110. else if(v.tostring().toupper().find("RGB-") > -1)
  111. {
  112.  
  113. local vString = v.tostring().toupper();
  114. local rgb = split(vString.slice(4),",");
  115. ioexp_update_leds(rgb[0].tointeger() * potLevel, rgb[1].tointeger() * potLevel, rgb[2].tointeger() * potLevel);
  116. }
  117. }
  118. }
  119.  
  120. //----------------------------------------
  121. //-- Custom Functions
  122. //----------------------------------------
  123.  
  124. function Hex2RGB(v)
  125. {
  126. local vString = v.tostring().toupper();
  127. local hexChars = "0123456789ABCDEF";
  128.  
  129. // Make sure it's 6 digits long
  130. if(vString.len() == 6)
  131. {
  132. // Return the new colors
  133. return [ (hexChars.find(vString[0].tochar())*16) + hexChars.find(vString[1].tochar()),
  134. (hexChars.find(vString[2].tochar())*16) + hexChars.find(vString[3].tochar()),
  135. (hexChars.find(vString[4].tochar())*16) + hexChars.find(vString[5].tochar()) ];
  136. }
  137.  
  138. // Just return the existing LED colors
  139. return [led_r, led_g, led_b];
  140. }
  141.  
  142. function RGB2Hex(r,g,b)
  143. {
  144. return format(r,"%02X") + format(g,"%02X") + format(b,"%02X");
  145. }
  146.  
  147. function ReadPot()
  148. {
  149. imp.wakeup(0.2,ReadPot);
  150.  
  151. // Read the pot, scale to 0.0 - 1.0
  152. local pot = hardware.pin2.read() / 65520.0;
  153. if(math.fabs(pot - potLevel) > 0.005)
  154. {
  155. // Update soft copy
  156. potLevel = pot;
  157. ioexp_update_leds(led_r * potLevel, led_g * potLevel, led_b * potLevel);
  158. }
  159.  
  160. }
  161.  
  162. //LED Driver Enable
  163. ioexp_modify_write(0x01, 0xE0, 0xFF);
  164. ioexp_modify_write(0x0f, 0xE0, 0x00);
  165. ioexp_modify_write(0x0b, 0xE0, 0xFF);
  166. ioexp_modify_write(0x21, 0xE0, 0xFF);
  167. ioexp_write(0x1e, 0x50);
  168. ioexp_write(0x1f, 0x10);
  169. ioexp_update_leds(0,0,0);
  170. ioexp_setpin(5, 0);
  171. ioexp_setpin(6, 0);
  172. ioexp_setpin(7, 0);
  173.  
  174. // Enable Rotary
  175. ioexp_setpin(8, 0);
  176. ioexp_setdir(8, 1);
  177. hardware.pin2.configure(ANALOG_IN);
  178.  
  179. // Turn off the LED
  180. ioexp_update_leds(0,0,0);
  181.  
  182. // Initiate
  183. imp.configure("Hannah / AMX Color", [color("Color","string")], []);
  184. potLevel = 1;
  185. server.show("");
  186.  
  187. //ReadPot();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement