Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Test Buttton on Hannah
- //
- //
- // IO Expander Class for SX1509
- //
- class IoExpander
- {
- i2cPort = null;
- i2cAddress = null;
- // Port is I2C_12 or I2C_89
- // Address is 0x3E - first address of the i2c Expander.
- constructor(port, address)
- {
- if(port == I2C_12)
- {
- // Configure I2C bus on pins 1 & 2
- hardware.configure(I2C_12);
- i2cPort = hardware.i2c12;
- }
- else if(port == I2C_89)
- {
- // Configure I2C bus on pins 8 & 9
- hardware.configure(I2C_89);
- i2cPort = hardware.i2c89;
- }
- else
- {
- server.log("Invalid I2C port specified.");
- }
- i2cAddress = address << 1;
- }
- // Read a byte
- function read(register)
- {
- local data = i2cPort.read(i2cAddress, format("%c", register), 1);
- if(data == null)
- {
- server.log("I2C Read Failure");
- return -1;
- }
- return data[0];
- }
- // Write a byte
- function write(register, data)
- {
- i2cPort.write(i2cAddress, format("%c%c", register, data));
- }
- // Write a bit to a register
- function writeBit(register, bitn, level)
- {
- local value = read(register);
- value = (level == 0)?(value & ~(1<<bitn)):(value | (1<<bitn));
- write(register, value);
- }
- // Write a masked bit pattern
- function writeMasked(register, data, mask)
- {
- local value = read(register);
- value = (value & ~mask) | (data & mask);
- write(register, value);
- }
- // Set a GPIO level
- function setPin(gpio, level)
- {
- writeBit(gpio>=8?0x10:0x11, gpio&7, level?1:0);
- }
- // Set a GPIO direction
- function setDir(gpio, output)
- {
- writeBit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1);
- }
- // Set a GPIO internal pull up
- function setPullUp(gpio, enable)
- {
- writeBit(gpio>=8?0x06:0x07, gpio&7, enable);
- }
- // Set GPIO interrupt mask
- function setIrqMask(gpio, enable)
- {
- writeBit(gpio>=8?0x12:0x13, gpio&7, enable);
- }
- // Set GPIO interrupt edges
- function setIrqEdges(gpio, rising, falling)
- {
- local addr = 0x17 - (gpio>>2);
- local mask = 0x03 << ((gpio&3)<<1);
- local data = (2*falling + rising) << ((gpio&3)<<1);
- writeMasked(addr, data, mask);
- }
- // Clear an interrupt
- function clearIrq(gpio)
- {
- writeBit(gpio>=8?0x18:0x19, gpio&7, 1);
- }
- // Get a GPIO input pin level
- function getPin(gpio)
- {
- return (read(gpio>=8?0x10:0x11)&(1<<(gpio&7)))?1:0;
- }
- }
- // Buttton Class
- class Button extends IoExpander
- {
- // IO Pin assignments
- pinButton = 0;
- state = 0;
- constructor(port, address, pinButton)
- {
- server.log(format("constructor"));
- base.constructor(port, address);
- // enable pin as active
- setPin(pinButton, 0);
- // set button IRQ
- setPullUp(pinButton, 1);
- //setIrqMask(pinButton, 1);
- //setIrqEdges(pinButton, 1, 1); // Set for rising and falling
- }
- // Read button
- function readState()
- {
- state = getPin(pinButton);
- //server.log(format("debug %d", state));
- return state;
- }
- }
- local button1 = Button(I2C_89, 0x3E, 1);
- local button2 = Button(I2C_89, 0x3E, 2);
- local state1 = 0;
- local state2 = 0;
- // Test
- function change()
- {
- // Schedule the next change
- imp.wakeup(0.5, change);
- // Read the button
- state1 = button1.readState();
- state2 = button2.readState();
- server.log(format("state - button 1 is %d - button 2 is %d", state1, state2));
- if (state1==1 || state2==1){
- server.log(format("state - button 1 is %d - button 2 is %d", state1, state2));
- }
- }
- // Register with the server
- imp.configure("Test Button", [], []);
- change();
- // End of code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement