Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Transceiver.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string>
3 #include <unistd.h>
4 
5 #include "../hubolib.h"
6 #include "CStringDump.h"
7 
8 using namespace HuboLib;
9 using namespace BCM2835;
10 
11 using namespace std;
12 
13 /*
14 Compile and link:
15  g++ Transceiver.cpp CStringDump.cpp -L../ -lhubo -lpthread -lrt -o Transceiver.out
16 Run:
17  sudo ./Transceiver.out <r|w> <filename>
18 Purpose:
19  The demo shows how the GPIO interface can be used to read in and bit bang out a stream of bits
20  from and to two 433MHz transmission modules.
21 */
22 
23 
24 void ReceiveToFile (unsigned short pin, string sFilename, unsigned long long breakTime);
25 void TransmitFromFile (unsigned short pin, string sFilename, int repeatCount);
26 
27 
28 int main(int argc, char *argv[])
29 {
30  if (argc!=3 || !(argv[1][0]=='r'||argv[1][0]=='w'))
31  {
32  printf (
33  "Usage:\n"
34  "receive <r|w> <filename>\n"
35  "r: read radio signals into filename\n"
36  "w: write content of filename to transmitter\n"
37  );
38  return -1;
39  }
40 
41  unsigned char command = argv[1][0];
42  string sFilename = argv[2];
43 
45  {
46  printf ("GPIO not properly initialized. Are you running the program as sudoer?\n");
47  return -1;
48  }
49 
50  // The two pins we will use for bit banging.
51  unsigned short transmit_pin = 17; // BCM GPIO=17 = GPIO0 = Raspi pinheader 11
52  unsigned short receive_pin = 22; // BCM GPIO=22 = GPIO3 = Raspi pinheader 15
53 
54  switch (command)
55  {
56  case 'r':
57  ReceiveToFile(receive_pin, sFilename, 1000000);
58  break;
59  case 'w':
60  TransmitFromFile(transmit_pin, sFilename, 1);
61  break;
62  }
63 
64  return 0;
65 }
66 
67 void ReceiveToFile(unsigned short pin, string sFilename, unsigned long long breakTime)
68 {
70  SetPullUpDown(pin, PUD_OFF);
71  // SetPullUpDown(pin, PUD_UP);
72 
73  unsigned long long now, last_time, delay;
74  now = last_time = GetTime_MicroSeconds();
75  unsigned short last_value, value;
76  last_value = value = 0;
77 
78  CStringDump log(sFilename);
79  log.Reset();
80 
81  while (1)
82  {
83  value = ReadPin(pin);
84  if (value != last_value)
85  {
86  now = GetTime_MicroSeconds();
87  delay = now - last_time;
88 
89  if (delay >= breakTime)
90  {
91  if (log.GetCount() > 0)
92  {
93  log.Dump();
94  printf ("Writing done...\n");
95  sleep(1);
96  log.Reset();
97  }
98  }
99  else
100  {
101  char buffer[128];
102  sprintf (buffer, "%ld %d\n", (long) delay, value);
103  log.Add(buffer);
104  // printf ("%s", buffer); // Uncomment for testing purposes.
105  }
106 
107  last_value = value;
108  last_time = now;
109  }
110  }
111 }
112 
113 void TransmitFromFile (unsigned short pin, string sFilename, int repeatCount)
114 {
115  std::list<unsigned long long> m_DelayList;
116  std::list<int> m_ValueList;
117 
118  // Read file into the above lists.
119  FILE* pFile = fopen (sFilename.c_str(), "r");
120  if (!pFile)
121  {
122  printf ("Error opening file!\n");
123  return;
124  }
125  char buffer[128];
126  while (fgets(buffer, 128, pFile))
127  {
128  unsigned long long delay;
129  int value;
130  if (sscanf(buffer, "%llu %d", &delay, &value) != 2)
131  printf ("Error in format reading\n");
132  else
133  {
134  m_DelayList.push_back(delay);
135  m_ValueList.push_back(value);
136  }
137  }
138  fclose(pFile);
139 
140  // Check content.
141  if (m_DelayList.empty() || m_ValueList.empty())
142  {
143  printf ("Files seem to be empty.\n");
144  return;
145  }
146 
147  // Define output pin.
149 
150  // Replay data from the two lists.
151  for (int i=0; i<repeatCount; i++)
152  {
153  std::list<unsigned long long>::iterator iterator_delay = m_DelayList.begin();
154  std::list<int>::iterator iterator_value = m_ValueList.begin();
155 
156  while (iterator_delay != m_DelayList.end() && iterator_value != m_ValueList.end())
157  {
158  Delay_MicroSeconds((unsigned long)*iterator_delay);
159  WritePin (pin, (unsigned char) *iterator_value);
160  // printf ("%llu %d\n", *iterator_delay, *iterator_value);
161  iterator_delay++;
162  iterator_value++;
163  }
164  }
165 }
void ReceiveToFile(unsigned short pin, string sFilename, unsigned long long breakTime)
Definition: Transceiver.cpp:67
unsigned short ReadPin(unsigned short pin)
Reads the value of an (input) pin.
void Delay_MicroSeconds(unsigned long delay_micros)
delays the execution of the calling thread for the given number of micro seconds. ...
int main(int argc, char *argv[])
Definition: Transceiver.cpp:28
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
void Add(std::string sLine)
Definition: CStringDump.cpp:32
void Reset()
Definition: CStringDump.cpp:57
void FunctionSelectPin(unsigned char pin, FunctionSelectType mode)
Use FunctionSelect() to configure the mode of a BCM2835 pin.
void TransmitFromFile(unsigned short pin, string sFilename, int repeatCount)
int GetCount()
Definition: CStringDump.h:25
void WritePin(unsigned char pin, unsigned char value)
Sets or clears a pin configured to be an output.
unsigned long long GetTime_MicroSeconds()
Returns the number of micro seconds.
void SetPullUpDown(unsigned char pin, PullUpDownType pud)
Enables or disables the BCM2835 internal pull -up or -down resistors for a pin configured as an input...