Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
CSC5262.cpp
Go to the documentation of this file.
1 #include "CSC5262.h"
2 
3 #include <stdio.h>
4 #include "../hubolib.h"
5 
6 using namespace BCM2835;
7 using namespace HuboLib;
8 
9 
10 CSC5262::CSC5262(int pulseLength, int pin, bool bQuiet)
11 {
12  m_PulseLength = pulseLength;
13  m_Pin = pin;
14  m_bQuiet = bQuiet;
15 
16  if (!IsGPIOInitialized() && !m_bQuiet)
17  printf ("GPIO not properly initialized - following calls will fail!");
18 
19  FunctionSelectPin(m_Pin, Output);
20 }
21 
22 
24 {
25 }
26 
27 
29 {
30  switch (bit)
31  {
32  case Zero:
33  // 1000
34  SetPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
35  ClearPin(m_Pin); Delay_MicroSeconds(3*m_PulseLength);
36  // 1000
37  SetPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
38  ClearPin(m_Pin); Delay_MicroSeconds(3*m_PulseLength);
39  break;
40 
41  case One:
42  // 1110
43  SetPin(m_Pin); Delay_MicroSeconds(3*m_PulseLength);
44  ClearPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
45  // 1110
46  SetPin(m_Pin); Delay_MicroSeconds(3*m_PulseLength);
47  ClearPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
48  break;
49 
50  case Floating:
51  // 1000
52  SetPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
53  ClearPin(m_Pin); Delay_MicroSeconds(3*m_PulseLength);
54  // 1110
55  SetPin(m_Pin); Delay_MicroSeconds(3*m_PulseLength);
56  ClearPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
57  break;
58 
59  case Sync:
60  // 1000 0000 0000 0000 0000 0000 0000 0000
61  SetPin(m_Pin); Delay_MicroSeconds(m_PulseLength);
62  ClearPin(m_Pin); Delay_MicroSeconds(31*m_PulseLength);
63  break;
64 
65  }
66 }
67 
68 
69 void CSC5262::SendLogicalBit (unsigned short bit)
70 {
71  if (bit)
72  SendPysicalBit(Zero);
73  else
74  SendPysicalBit(Floating);
75 }
76 
77 
78 void CSC5262::SendCommand (bool bTurnOn)
79 {
80  if (bTurnOn)
81  {
82  SendPysicalBit (Zero);
83  SendPysicalBit (Floating);
84  }
85  else
86  {
87  SendPysicalBit (Floating);
88  SendPysicalBit (Zero);
89  }
90 }
91 
92 void CSC5262::SendSync ()
93 {
94  SendPysicalBit (Sync);
95 }
96 
97 
98 bool CSC5262::Switch (unsigned char SystemCode, unsigned char Receiver, bool bTurnOn)
99 {
100  unsigned long long start = GetTime_MicroSeconds ();
101 
102  // Clock out system code first...
103  for (int rover = 0x10; rover > 0; rover >>= 1)
104  SendLogicalBit (SystemCode & rover);
105 
106  // ...add socket address...
107  for (int rover = 0x10; rover > 0; rover >>= 1)
108  SendLogicalBit (Receiver & rover);
109 
110  // ... append command...
111  SendCommand (bTurnOn);
112 
113  // ... and finally send sync frame.
114  SendSync ();
115 
116  unsigned long long stop = GetTime_MicroSeconds ();
117 
118  // The datagram has an overall length of 128 bits.
119  // We regard the datagram to be transmitted correct if we see the transmission time to not exceed the time by more than the time of 1 pulse length.
120  bool bSuccess = ( ((long) (stop-start)) <= (128L + 1L)*m_PulseLength );
121 
122  if (!m_bQuiet)
123  printf ("Duration = %llu micro seconds - %s\n", stop-start, bSuccess?"ok":"failed");
124 
125  return bSuccess;
126 }
127 
128 bool CSC5262::SwitchSocket (unsigned char SystemCode, unsigned char Receiver, bool bTurnOn, int repeatedSuccess, int totalTransmittion)
129 {
130  if (repeatedSuccess > totalTransmittion)
131  {
132  if (!m_bQuiet)
133  printf ("User error - The number of total transmissions must not be less than the number of successfully repeated transmissions.\n");
134  return false;
135  }
136  if (repeatedSuccess < 3)
137  {
138  if (!m_bQuiet)
139  printf ("User error - The number of successfully repeated transmissions must at least be 3 in order to switch the socket.\n");
140  return false;
141  }
142 
143  int transmissionCount = 0;
144  int successCount = 0;
145  while (1)
146  {
147  if (Switch (SystemCode, Receiver, bTurnOn))
148  successCount++;
149  else
150  successCount = 0;
151 
152  if (successCount == repeatedSuccess)
153  return true;
154 
155  transmissionCount++;
156 
157  if (transmissionCount == totalTransmittion)
158  return false;
159  }
160 }
void Delay_MicroSeconds(unsigned long delay_micros)
delays the execution of the calling thread for the given number of micro seconds. ...
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
void SetPin(unsigned char pin)
Sets a pin configured as output to 1.
virtual ~CSC5262()
Definition: CSC5262.cpp:23
void SendLogicalBit(unsigned short bit)
Definition: CSC5262.cpp:69
void FunctionSelectPin(unsigned char pin, FunctionSelectType mode)
Use FunctionSelect() to configure the mode of a BCM2835 pin.
void SendSync()
Definition: CSC5262.cpp:92
void SendCommand(bool bTurnOn)
Definition: CSC5262.cpp:78
CSC5262(int pulseLength, int pin, bool bQuiet=false)
Definition: CSC5262.cpp:10
void SendPysicalBit(TriSTateBit bit)
Definition: CSC5262.cpp:28
void ClearPin(unsigned char pin)
Resets a pin configured as output to 0.
unsigned long long GetTime_MicroSeconds()
Returns the number of micro seconds.
bool SwitchSocket(unsigned char SystemCode, unsigned char Receiver, bool bTurnOn, int repeatedSuccess, int totalTransmittion)
Definition: CSC5262.cpp:128
TriSTateBit
Definition: CSC5262.h:15
bool Switch(unsigned char SystemCode, unsigned char Receiver, bool bTurnOn)
Definition: CSC5262.cpp:98