needed for 1620 test code

Fork of ShiftOut by Ollie Milton

ShiftOut.h

Committer:
el11es
Date:
2016-09-27
Revision:
5:f7749e47a29b
Parent:
4:7333dc6fca5c

File content as of revision 5:f7749e47a29b:

#ifndef SHIFTOUT_H
#define SHIFTOUT_H

#include <mbed.h>

/** A simple serial driver for a shift register that uses only three digital out pins leaving SPI and i2c free. 
 * ShiftOut can be configured for any size shift register but defaults to eight bits.
 */
class ShiftOut {

    public :
    
        /** Constructs a new ShiftOut with the given three pins.
         * @param clk - the pin to use for the shift register clock.
         * @param data - the pin to use for the shift register data line.
         * @param latch - the pin to use for the shift register latch.
         * @param registerCount - the number of registers in the shift register, defaults to eight.
         */
        ShiftOut(PinName clk, PinName data, PinName latch, int8_t registerCount = 0x08) {
            clkout = new DigitalOut(clk);
            dataout = new DigitalOut(data);
            latchout = new DigitalOut(latch);
            this->registerCount = registerCount;
        }
        
        ~ShiftOut() {
            delete clkout;
            delete dataout;
            delete latchout;
        }
        
        /** Writes the given integer to the shift register
         */
        void write(int data) {
            *latchout = 0;
            for (int i = registerCount - 1; i >=  0; i--) {
                *clkout = 0;
                *dataout = (data & (1 << i)) != 0;
                *clkout = 1;
            }
            *latchout = 1;
        }
        
        void set_latch (int val){
            *latchout = val;
         }
         
    private :
        DigitalOut *clkout;
        DigitalOut *dataout;
        DigitalOut *latchout;
        int8_t registerCount;     
};

#endif