Advertisement
plemire

Test Button For Hannah test board

Sep 17th, 2012
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Test Buttton on Hannah
  2. //
  3.  
  4. //
  5. // IO Expander Class for SX1509
  6. //
  7. class IoExpander
  8. {
  9.     i2cPort = null;
  10.     i2cAddress = null;
  11.    
  12.     // Port is I2C_12 or I2C_89
  13.     // Address is 0x3E - first  address of the i2c Expander.
  14.    
  15.     constructor(port, address)
  16.     {
  17.         if(port == I2C_12)
  18.         {
  19.             // Configure I2C bus on pins 1 & 2
  20.             hardware.configure(I2C_12);
  21.             i2cPort = hardware.i2c12;
  22.         }
  23.         else if(port == I2C_89)
  24.         {
  25.             // Configure I2C bus on pins 8 & 9
  26.             hardware.configure(I2C_89);
  27.             i2cPort = hardware.i2c89;
  28.         }
  29.         else
  30.         {
  31.             server.log("Invalid I2C port specified.");
  32.         }
  33.  
  34.         i2cAddress = address << 1;
  35.     }
  36.  
  37.     // Read a byte
  38.     function read(register)
  39.     {
  40.         local data = i2cPort.read(i2cAddress, format("%c", register), 1);
  41.         if(data == null)
  42.         {
  43.             server.log("I2C Read Failure");
  44.             return -1;
  45.         }
  46.  
  47.         return data[0];
  48.     }
  49.  
  50.     // Write a byte
  51.     function write(register, data)
  52.     {
  53.         i2cPort.write(i2cAddress, format("%c%c", register, data));
  54.     }
  55.  
  56.     // Write a bit to a register
  57.     function writeBit(register, bitn, level)
  58.     {
  59.         local value = read(register);
  60.         value = (level == 0)?(value & ~(1<<bitn)):(value | (1<<bitn));
  61.         write(register, value);
  62.     }
  63.  
  64.     // Write a masked bit pattern
  65.     function writeMasked(register, data, mask)
  66.     {
  67.        local value = read(register);
  68.        value = (value & ~mask) | (data & mask);
  69.        write(register, value);
  70.     }
  71.  
  72.     // Set a GPIO level
  73.     function setPin(gpio, level)
  74.     {
  75.         writeBit(gpio>=8?0x10:0x11, gpio&7, level?1:0);
  76.     }
  77.  
  78.     // Set a GPIO direction
  79.     function setDir(gpio, output)
  80.     {
  81.         writeBit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1);
  82.     }
  83.  
  84.     // Set a GPIO internal pull up
  85.     function setPullUp(gpio, enable)
  86.     {
  87.         writeBit(gpio>=8?0x06:0x07, gpio&7, enable);
  88.     }
  89.  
  90.     // Set GPIO interrupt mask
  91.     function setIrqMask(gpio, enable)
  92.     {
  93.         writeBit(gpio>=8?0x12:0x13, gpio&7, enable);
  94.     }
  95.  
  96.     // Set GPIO interrupt edges
  97.     function setIrqEdges(gpio, rising, falling)
  98.     {
  99.         local addr = 0x17 - (gpio>>2);
  100.         local mask = 0x03 << ((gpio&3)<<1);
  101.         local data = (2*falling + rising) << ((gpio&3)<<1);    
  102.         writeMasked(addr, data, mask);
  103.     }
  104.  
  105.     // Clear an interrupt
  106.     function clearIrq(gpio)
  107.     {
  108.         writeBit(gpio>=8?0x18:0x19, gpio&7, 1);
  109.     }
  110.  
  111.     // Get a GPIO input pin level
  112.     function getPin(gpio)
  113.     {
  114.         return (read(gpio>=8?0x10:0x11)&(1<<(gpio&7)))?1:0;
  115.     }
  116. }
  117.  
  118. // Buttton Class
  119. class Button extends IoExpander
  120. {
  121.     // IO Pin assignments
  122.     pinButton = 0;
  123.     state = 0;
  124.  
  125.     constructor(port, address, pinButton)
  126.     {
  127.         server.log(format("constructor"));
  128.         base.constructor(port, address);
  129.        
  130.         // enable pin as active
  131.         setPin(pinButton, 0);
  132.        
  133.        
  134.         // set button IRQ
  135.         setPullUp(pinButton, 1);
  136.         //setIrqMask(pinButton, 1);
  137.         //setIrqEdges(pinButton, 1, 1);  // Set for rising and falling        
  138.     }
  139.    
  140.     // Read button
  141.     function readState()
  142.     {
  143.         state = getPin(pinButton);
  144.  
  145.         //server.log(format("debug %d", state));
  146.         return state;
  147.     }
  148.    
  149. }
  150.  
  151.  
  152. local button1 = Button(I2C_89, 0x3E, 1);
  153. local button2 = Button(I2C_89, 0x3E, 2);
  154. local state1 = 0;
  155. local state2 = 0;
  156.  
  157. // Test
  158. function change()
  159. {
  160.     // Schedule the next change
  161.     imp.wakeup(0.5, change);
  162.    
  163.     // Read the button
  164.     state1 = button1.readState();
  165.     state2 = button2.readState();
  166.    
  167.     server.log(format("state - button 1 is %d - button 2 is %d", state1, state2));
  168.     if (state1==1 || state2==1){
  169.         server.log(format("state - button 1 is %d - button 2 is %d", state1, state2));
  170.     }
  171. }
  172.  
  173. // Register with the server
  174. imp.configure("Test Button", [], []);
  175.  
  176. change();
  177.  
  178. // End of code.
Advertisement
RAW Paste Data Copied
Advertisement