Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
RCSocket.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <assert.h>
3 
4 #include "../hubolib.h"
5 #include "CSC5262.h"
6 
7 using namespace HuboLib;
8 using namespace BCM2835;
9 
10 /*
11 Compile and link:
12  g++ RCSocket.cpp CSC5262.cpp -L../ -lhubo -lpthread -lrt -o RCSocket.out
13 Run:
14  sudo ./RCSocket.out <system-code> <socket number> <0|1>
15 Purpose:
16  The demo shows how the GPIO interface can be used to bit bang an output in order to
17  use an 433MHz transmission module for controlling a RC socket.
18 */
19 
20 
21 // Switch Coding (x is not being used and should be set to 0)
22 // System-Code Receiver-Socket
23 // x x x 1 2 3 4 5 x x x A B C D E
24 
25 // Example 1
26 // System-Code Receiver-Socket
27 // x x x 1 2 3 4 5 x x x A B C D E
28 // 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0
29 // 0x05 0x02
30 // SwitchSocket(0x05, 0x02, true... ) switches socket D of system 00101 on.
31 // SwitchSocket(0x05, 0x02, false...) switches socket D of system 00101 off.
32 
33 // Example 2
34 // System-Code Receiver-Socket
35 // x x x 1 2 3 4 5 x x x A B C D E
36 // 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0
37 // 0x13 0x10
38 // SwitchSocket(0x13, 0x10, true... ) switches socket A of system 10011 on.
39 // SwitchSocket(0x13, 0x10, false...) switches socket A of system 10011 off.
40 
41 
42 bool strToUChar(char* str, unsigned char& value);
43 void BoostThreadPriority();
44 
45 
46 int main(int argc, char *argv[])
47 {
48  unsigned char systemCode, socketNumber, value;
49  if (argc!=4 || !strToUChar(argv[1], systemCode) || !strToUChar(argv[2], socketNumber) || !strToUChar(argv[3], value))
50  {
51  printf (
52  "Usage:\n"
53  "RCSocket.out <system-code> <socket number> <0|1>\n"
54  "system-code: 0..31\n"
55  "socket number: 0..31 (whereas A=16, B=8, C=4, D=2 and E=1)\n"
56  "0=off, 1=on\n"
57  );
58  return -1;
59  }
60 
61  // This program requires high priority in order to successfully bit bang over the GPIO.
63 
64  // If this call doesn't return true then we must not use the GPIO part of Hubo library!
65  if (!IsGPIOInitialized ())
66  {
67  printf ("GPIO not properly initialized. Are you running the program as sudoer?\n");
68  return -1;
69  }
70 
71  // Let's use a bit-length of 350µs (that's what the remote control sends) and BCM2835 pin 17 (= GPIO0).
72  // Experiments have shown a minimum of 150µs and a maximum of 580µs for the bit length.
73  CSC5262 mySwitch(350, 17);
74 
75  // While the switches seem to require at least 3 successive transmissions in a series - repeating it
76  // at least 4 times seems to improve transmission stability. After 10 retries in total we stop.
77  mySwitch.SwitchSocket(systemCode, socketNumber, value>0, 4, 10);
78 
79  return 0;
80 }
81 
82 #include <pthread.h>
83 #include <assert.h>
85 {
86  // Scheduling params.
87  sched_param param;
88  int policy;
89  int ret;
90  pthread_t threadHandle = pthread_self();
91 
92  // Get current scheduling parameters of thread.
93  ret = pthread_getschedparam (threadHandle, &policy, &param);
94  assert (ret == 0);
95 
96  // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
97  policy = SCHED_FIFO; // SCHED_RR;
98  param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
99  ret = pthread_setschedparam(threadHandle, policy, &param);
100  assert (ret == 0);
101 }
102 
103 bool strToUChar(char* str, unsigned char& value)
104 {
105  unsigned long v;
106  if (sscanf(str, "%lu", &v) != 1)
107  return false;
108  value = (unsigned char)v;
109  if (value != (unsigned long)v)
110  return false;
111  return true;
112 }
113 
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
Definition: CSC5262.h:5
int main(int argc, char *argv[])
Definition: RCSocket.cpp:46
bool strToUChar(char *str, unsigned char &value)
Definition: RCSocket.cpp:103
bool SwitchSocket(unsigned char SystemCode, unsigned char Receiver, bool bTurnOn, int repeatedSuccess, int totalTransmittion)
Definition: CSC5262.cpp:128
void BoostThreadPriority()
Definition: RCSocket.cpp:84