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

Fork of PMRC4ch by Akifumi Takahashi

Revision:
21:fa067e2a30f2
Parent:
20:26972de3cf90
Child:
42:a827fe9166b5
--- a/PMRC16ch.cpp	Thu Jun 28 08:49:54 2018 +0000
+++ b/PMRC16ch.cpp	Tue Oct 23 15:10:56 2018 +0000
@@ -1,4 +1,9 @@
+
 #include "PMRC16ch.h"
+const uint32_t PMRC16ch::m_statearray[] = {
+    ALLGROUND, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH8, CH9,
+    CH10, CH11, CH12, CH13, CH14, CH15, CH16, ALLHiZ
+};
 //  A constractor whose arguments have all default value
 //  is a default constractor.
 PMRC16ch::PMRC16ch():
@@ -11,6 +16,19 @@
 {
     init();
 }
+PMRC16ch::PMRC16ch(
+    uint8_t arg_num_ch
+):
+    m_num_ch(arg_num_ch),
+    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,
@@ -29,25 +47,23 @@
 {
     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;
+    allHiZ();
+    m_PMRC_mode = TWIN_ELECTRODES;
     m_PMRC_POL = Cathodic;
 }
 
 void PMRC16ch::allGround()
 {
-    if (m_PMRC_state == ALLGROUND) return 1;
+    if (m_PMRC_state == ALLGROUND) return;
     sweep();
     upload();
+    m_PMRC_state = ALLGROUND;
+    m_pos_stim = 0;
 }
 
 void PMRC16ch::allHiZ()
@@ -58,47 +74,51 @@
     //enable insertion data to SR
     m_CLR = 1;
     upload();
+    m_PMRC_state = ALLHiZ;
+    m_pos_stim = 0;
+}
+//------------------------------------------------------------------------------
+void PMRC16ch::setTwin(char arg_stim_ch, char arg_ref_ch)
+{
+    m_PMRC_mode = TWIN_ELECTRODES;
+    m_PMRC_state = m_statearray[arg_stim_ch] + (m_statearray[arg_ref_ch] >> 1);
+    setBits(m_PMRC_state);
+    upload();
+    m_pos_stim = arg_stim_ch;
 }
 
-void PMRC16ch::setCh(char arg_state, Polarity arg_POL)
+void PMRC16ch::setOvsO(char arg_ch)
 {
-    m_PMRC_POL = arg_POL;
-    int8_t num_of_shift = arg_state - static_cast<int8_t>(m_PMRC_state);
+    int8_t num_of_shift;
 
-    if( num_of_shift < 0 )
+    num_of_shift = arg_ch - m_pos_stim;
+    //  m_PMRC_mode == ONE_VS_THEOTHERS && m_pos_stim == 0
+    //  => m_PMRC_state == ALLGROUND
+    if( num_of_shift < 0 || m_pos_stim == 0 || m_PMRC_mode != ONE_VS_THEOTHERS) {
         sweep();
-    
-    if( m_PMRC_state == ALLGROUND ) {
         setStimbits();
-        num_of_shift = static_cast<char>(arg_state - 1);
+        num_of_shift = arg_ch - 1;
     }
     shiftby(num_of_shift);
+    m_PMRC_mode = ONE_VS_THEOTHERS;
+    m_PMRC_state = ALLGROUND + m_statearray[arg_ch];
+
     upload();
-    
-    return static_cast<char>(m_PMRC_state = static_cast<State>(arg_state));
+    m_pos_stim = arg_ch;
 }
 
-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);
+    uint32_t num_shift;
 
-    shiftby(num_of_shift);
-    m_PMRC_state = ALLGROUND;
+    if(m_PMRC_mode != ONE_VS_THEOTHERS || m_PMRC_state == ALLHiZ) {
+        num_shift = m_num_ch;
+    } else if(m_PMRC_mode == ONE_VS_THEOTHERS) {
+        num_shift = m_num_ch - (m_pos_stim - 1);
+    }
+    shiftby(num_shift);
 }
 
 void PMRC16ch::shiftby(int arg_num)
@@ -111,7 +131,6 @@
         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()
@@ -124,6 +143,33 @@
     update();
     m_PMRC_state = CH1;
 }
+
+void PMRC16ch::setBits(const uint32_t arg_bits)
+{
+    uint8_t tmp_bit[2];
+    //reset shiftresister
+    m_CLR = 0;
+    update();
+    //enable insertion data to SR
+    m_CLR = 1;
+    for(int i = 0; i < m_num_ch; i++) {
+        tmp_bit[0] = 0b01 & (arg_bits >> (2 * i));
+        tmp_bit[1] = 0b01 & (arg_bits >> (2 * i + 1));
+        //  if the chan. is not HiZ meaning Hi or Lw
+        if(tmp_bit[0] + tmp_bit[1] == 1) {
+            m_SER = (tmp_bit[0] ^ m_PMRC_POL); //XOR Polarity
+            update();
+            m_SER = (tmp_bit[1] ^ m_PMRC_POL); //XOR Polarity
+            update();
+        } else {
+            m_SER = (tmp_bit[0]);
+            update();
+            m_SER = (tmp_bit[1]);
+            update();
+        }
+    }
+}
+//------------------------------------------------------------------------------
 void PMRC16ch::update()
 {
     //Shift-resister Clock update