SX1509 16 Output I/O Expander / LED Driver Library

Dependents:   SX1509_HelloWorld

SX1509.h

Committer:
jjones646
Date:
2014-10-21
Revision:
1:9ab20d13c44e
Parent:
0:893f387bda9f
Child:
2:330355f546b0

File content as of revision 1:9ab20d13c44e:



#ifndef SX1509_H
#define SX1509_H

#include "mbed.h"

// DEFINES - SX1509 Configuration
// ====================================================
#define SX1509_FREQUENCY    400000
#define SX1509_ADDRESS_1    0x3E
#define SX1509_ADDRESS_2    0x3F
#define SX1509_ADDRESS_3    0x70
#define SX1509_ADDRESS_4    0x71

// DEFINES - Register Addresses
// ====================================================
#define REGINPUTDISABLEB    (0x00)
#define REGINPUTDISABLEA    (0x01)
#define REGLONGSLEWB        (0x02)
#define REGLONGSLEWA        (0x03)
#define REGLOWDRIVEB        (0x04)
#define REGLOWDRIVEA        (0x05)
#define REGPULLUPB          (0x06)
#define REGPULLUPA          (0x07)
#define REGPULLDOWNB        (0x08)
#define REGPULLDOWNA        (0x09)
#define REGOPENDRAINB       (0x0A)
#define REGOPENDRAINA       (0x0B)
#define REGPOLARITYB        (0x0C)
#define REGPOLARITYA        (0x0D)
#define REGDIRB             (0x0E)
#define REGDIRA             (0x0F)
#define REGDATAB            (0x10)
#define REGDATAA            (0x11)
#define REGINTERRUPTMASKB   (0x12)
#define REGINTERRUPTMASKA   (0x13)
#define REGSENSEHIGHB       (0x14)
#define REGSENSELOWB        (0x15)
#define REGSENSEHIGHA       (0x16)
#define REGSENSELOWA        (0x17)
#define REGINTERRUPTSOURCEB (0x18)
#define REGINTERRUPTSOURCEA (0x19)
#define REGEVENTSTATUSB     (0x1A)
#define REGEVENTSTATUSA     (0x1B)
#define REGLEVELSHIFTER1    (0x1C)
#define REGLEVELSHIFTER2    (0x1D)
#define REGCLOCK            (0x1E)
#define REGMISC             (0x1F)
#define REGLEDDRIVERENABLEB (0x20)
#define REGLEDDRIVERENABLEA (0x21)
// Debounce
#define REGDEBOUNCECONFIG   (0x22)
#define REGDEBOUNCEENABLEB  (0x23)
#define REGDEBOUNCEENABLEA  (0x24)
#define REGKEYCONFIG1       (0x25)
#define REGKEYCONFIG2       (0x26)
#define REGKEYDATA1         (0x27)
#define REGKEYDATA2         (0x28)
// LED Driver
#define REGTON0             (0x29)
#define REGION0             (0x2A)
#define REGOFF0             (0x2B)
#define REGTON1             (0x2C)
#define REGION1             (0x2D)
#define REGOFF1             (0x2E)
#define REGTON2             (0x2F)
#define REGION2             (0x30)
#define REGOFF2             (0x31)
#define REGTON3             (0x32)
#define REGION3             (0x33)
#define REGOFF3             (0x34)
#define REGTON4             (0x35)
#define REGION4             (0x36)
#define REGOFF4             (0x37)
#define REGTRISE4           (0x38)
#define REGTFALL4           (0x39)
#define REGTON5             (0x3A)
#define REGION5             (0x3B)
#define REGOFF5             (0x3C)
#define REGTRISE5           (0x3D)
#define REGTFALL5           (0x3E)
#define REGTON6             (0x3F)
#define REGION6             (0x40)
#define REGOFF6             (0x41)
#define REGTRISE6           (0x42)
#define REGTFALL6           (0x43)
#define REGTON7             (0x44)
#define REGION7             (0x45)
#define REGOFF7             (0x46)
#define REGTRISE7           (0x47)
#define REGTFALL7           (0x48)
#define REGTON8             (0x49)
#define REGION8             (0x4A)
#define REGOFF8             (0x4B)
#define REGTON9             (0x4C)
#define REGION9             (0x4D)
#define REGOFF9             (0x4E)
#define REGTON10            (0x4F)
#define REGION10            (0x50)
#define REGOFF10            (0x51)
#define REGTON11            (0x52)
#define REGION11            (0x53)
#define REGOFF11            (0x54)
#define REGTON12            (0x55)
#define REGION12            (0x56)
#define REGOFF12            (0x57)
#define REGTRISE12          (0x58)
#define REGTFALL12          (0x59)
#define REGTON13            (0x5A)
#define REGION13            (0x5B)
#define REGOFF13            (0x5C)
#define REGTRISE13          (0x5D)
#define REGTFALL13          (0x5E)
#define REGTON14            (0x5F)
#define REGION14            (0x60)
#define REGOFF14            (0x61)
#define REGTRISE14          (0x62)
#define REGTFALL14          (0x63)
#define REGTON15            (0x64)
#define REGION15            (0x65)
#define REGOFF15            (0x66)
#define REGTRISE15          (0x67)
#define REGTFALL15          (0x68)
// Misc
#define REGHIGHINPUTB       (0x69)
#define REGHIGHINPUTA       (0x6A)
// Software Reset
#define REGRESET            (0x7D)
// Test Registers (do not write)
#define REGTEST1            (0x7E)
#define REGTEST2            (0x7F)


// DEFINES - Class Specific
// ====================================================
#define BUFFER_SIZE         (8)


// ENUMERATIONS
// ====================================================
enum Direction_t {
    OUT = 0,
    IN
};

enum Side_t {
    A = 0,
    B
};

enum Transfer_t {
    WRITE = 0,
    READ
};

enum State_t {
    OFF = 0x00,
    ON  = 0xFF
};

enum Pull_t {
    PULLUP = 0,
    PULLDOWN
};

enum Freq_t {
    SLOW = 0,
    MED = 1,
    FAST = 2
};

enum LED_t {
    IO4 = REGTON4,
    IO5 = REGTON5,
    IO6 = REGTON6,
    IO7 = REGTON7,
    IO12 = REGTON12,
    IO13 = REGTON13,
    IO14 = REGTON14,
    IO15 = REGTON15
};

// CLASS DEFINITION
// ====================================================
class SX1509
{
private:
    Direction_t dirA, dirB;
    char buf[BUFFER_SIZE];

protected:
    DigitalIn   *_intN;
    DigitalOut  *_resetN;
    I2C         *_i2c;
    bool hasInt, hasReset;

    void init(PinName, PinName, PinName, PinName);
    void direction(Direction_t, Side_t);
    int transfer_data(char const, char const, Transfer_t);

public:

    // Constructors
    SX1509();
    SX1509(PinName sda);
    SX1509(PinName sda, PinName scl, PinName intN = NC, PinName resetN = NC);

    void directionA(Direction_t dir);
    void directionB(Direction_t dir);
    void write_register(char const, char const);
    int read_register(char const);
    void setA(char const);
    void setB(char const);
    int readA();
    int readB();
    bool get(int const);
    void set(int const);
    void clear(int const);
    void toggle(int const);
    Direction_t getDirection(Side_t);
    void setBuffer(Side_t, State_t);
    void setPull(Side_t, Pull_t, State_t);
    void setOpenDrain(Side_t, State_t);
    void setClock(State_t);
    void setFreq(Freq_t);
    void setLedDrive(Side_t, State_t);
    void clearAll(Side_t);
    void enableLED(Side_t);
    void setupLED(LED_t);
    
};

#endif