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

Fork of PMRC4ch by Akifumi Takahashi

Revision:
16:e81a30a098dd
Parent:
15:56703876e914
Child:
17:08a68860396b
diff -r 56703876e914 -r e81a30a098dd PMRC16ch.cpp
--- a/PMRC16ch.cpp	Tue Jun 26 12:19:09 2018 +0000
+++ b/PMRC16ch.cpp	Tue Jun 26 12:42:26 2018 +0000
@@ -58,6 +58,7 @@
 
 char PMRC16ch::setCh(State arg_state, Polarity arg_POL)
 {
+    m_PMRC_POL = arg_POL;
     int num_of_shift = static_cast<int>(arg_state) - static_cast<int>(m_PMRC_state);
 
     if( num_of_shift < 0 )
@@ -70,11 +71,11 @@
     shiftby(num_of_shift);
     upload();
     
-    m_PMRC_POL = arg_POL;
     return static_cast<char>(m_PMRC_state = arg_state);
 }
 char 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 )
@@ -87,7 +88,6 @@
     shiftby(num_of_shift);
     upload();
     
-    m_PMRC_POL = arg_POL;
     return static_cast<char>(m_PMRC_state = static_cast<State>(arg_state));
 }
 
@@ -103,18 +103,17 @@
 
 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_SER = (((bits >> i) & 0b0001) ^ m_PMRC_POL); //XOR Polarity
         update();
     }
     upload();
-    
-    m_PMRC_POL = arg_POL;
 }
 
 void PMRC16ch::sweep()
@@ -128,11 +127,11 @@
 void PMRC16ch::shiftby(int arg_num)
 {
     for(int i = 0; i < arg_num; i++) {
-        //  insert 1
-        m_SER = 1;
+        //  insert 1 XOR Polarity
+        m_SER = 1 ^ m_PMRC_POL;
         update();
-        //  insert 0
-        m_SER = 0;
+        //  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));
@@ -140,11 +139,11 @@
 
 void PMRC16ch::setStimbits()
 {
-    //  insert 0
-    m_SER = 0;
+    //  insert 0 XOR Polarity
+    m_SER = 0 ^ m_PMRC_POL;
     update();
-    //  insert 1
-    m_SER = 1;
+    //  insert 1 XOR Polarity
+    m_SER = 1 ^ m_PMRC_POL;
     update();
     m_PMRC_state = CH1;
 }