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