Library to use my Photo MOS Relays Circuit having 16 or less channels.

Fork of PMRC4ch by Akifumi Takahashi

PMRC16ch.cpp

Committer:
aktk
Date:
2018-06-28
Revision:
20:26972de3cf90
Parent:
18:049283936e3f
Child:
21:fa067e2a30f2

File content as of revision 20:26972de3cf90:

#include "PMRC16ch.h"
//  A constractor whose arguments have all default value
//  is a default constractor.
PMRC16ch::PMRC16ch():
    m_num_ch(16),
    m_SCK(DigitalOut(p11)),
    m_CLR(DigitalOut(p12)),
    m_RCK(DigitalOut(p13)),
    m_SER(DigitalOut(p14)),
    m_OUT(DigitalIn(p10))
{
    init();
}

PMRC16ch::PMRC16ch(
    uint8_t arg_num_ch,
    PinName arg_SCK,
    PinName arg_CLR,
    PinName arg_RCK,
    PinName arg_SER,
    PinName arg_OUT
):
    m_num_ch(arg_num_ch),
    m_SCK(DigitalOut(arg_SCK)),
    m_CLR(DigitalOut(arg_CLR)),
    m_RCK(DigitalOut(arg_RCK)),
    m_SER(DigitalOut(arg_SER)),
    m_OUT(DigitalIn(arg_OUT))
{
    init();
}

void PMRC16ch::init()
{
    m_CLR = 1;
    m_SCK = m_RCK = 0;
    //reset shiftresister
    m_CLR = 0;
    update();
    //enable insertion data to SR
    m_CLR = 1;
    m_PMRC_state = ALLHiZ;
    m_PMRC_POL = Cathodic;
}

void PMRC16ch::allGround()
{
    if (m_PMRC_state == ALLGROUND) return 1;
    sweep();
    upload();
}

void PMRC16ch::allHiZ()
{
    //reset shiftresister
    m_CLR = 0;
    update();
    //enable insertion data to SR
    m_CLR = 1;
    upload();
}

void PMRC16ch::setCh(char arg_state, Polarity arg_POL)
{
    m_PMRC_POL = arg_POL;
    int8_t num_of_shift = arg_state - static_cast<int8_t>(m_PMRC_state);

    if( num_of_shift < 0 )
        sweep();
    
    if( m_PMRC_state == ALLGROUND ) {
        setStimbits();
        num_of_shift = static_cast<char>(arg_state - 1);
    }
    shiftby(num_of_shift);
    upload();
    
    return static_cast<char>(m_PMRC_state = static_cast<State>(arg_state));
}

void PMRC16ch::setBits(const uint32_t bits, int num_of_bits, Polarity arg_POL)
{
    m_PMRC_POL = arg_POL;
    //reset shiftresister
    m_CLR = 0;
    update();
    //enable insertion data to SR
    m_CLR = 1;
    for(int i = 0; i < num_of_bits; i++){
        m_SER = (((bits >> i) & 0b0001) ^ m_PMRC_POL); //XOR Polarity
        update();
    }
    upload();
}

void PMRC16ch::sweep()
{
    int num_of_shift = (16 + 1) - static_cast<int>(m_PMRC_state);

    shiftby(num_of_shift);
    m_PMRC_state = ALLGROUND;
}

void PMRC16ch::shiftby(int arg_num)
{
    for(int i = 0; i < arg_num; i++) {
        //  insert 1 XOR Polarity
        m_SER = 1 ^ m_PMRC_POL;
        update();
        //  insert 0 XOR Polarity
        m_SER = 0 ^ m_PMRC_POL;
        update();
    }
    m_PMRC_state = static_cast<State>((static_cast<int>(m_PMRC_state) + arg_num) % (16 + 1));
}

void PMRC16ch::setStimbits()
{
    //  insert 0 XOR Polarity
    m_SER = 0 ^ m_PMRC_POL;
    update();
    //  insert 1 XOR Polarity
    m_SER = 1 ^ m_PMRC_POL;
    update();
    m_PMRC_state = CH1;
}
void PMRC16ch::update()
{
    //Shift-resister Clock update
    m_SCK = 1;
    m_SCK = 1;
    m_SCK = 0;
    m_SCK = 0;
}
void PMRC16ch::upload()
{
    //FF Clock Update
    m_RCK = 1;
    m_RCK = 1;
    m_RCK = 0;
    m_RCK = 0;
}