Hubo Library

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
MD17177.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
7 using namespace HuboLib;
8 using namespace BCM2835;
9
10
11 /*
12 Compile and link:
13 g++ MD17177.cpp -L../ -lhubo -lpthread -lrt -o MD17177.out
14 Run:
15 sudo ./MD17177.out
16 Purpose:
17 The demo rings the Medion MD17177 433MHz door bell.
18 */
19
20
21 const char* g_pCode = "CBCDADABCDABCBCDADADABCDABCBCBCBCBCE";
22
23 class CCode
24 {
25 public :
26 CCode (unsigned short time, unsigned char signal)
27 {
28 m_time = time;
29 m_signal = signal;
30 }
31
32 public :
33 unsigned short m_time;
34 unsigned char m_signal;
35 };
36
37 CCode A( 340, 1);
38 CCode B( 395, 0);
39 CCode C(1060, 1);
40 CCode D(1120, 0);
41 CCode E(5830, 0);
42
43 void SendDatagram (unsigned short pin);
44 void BoostThreadPriority();
45
46
47 int main(int argc, char *argv[])
48 {
49 if (!BCM2835::IsGPIOInitialized ())
50 {
51 printf ("GPIO not properly initialized. Are you running the program as sudoer?\n");
52 return -1;
53 }
54
55 // Define the pin we will use for bit banging as output.
56 unsigned short transmit_pin = 17; // BCM GPIO=17 = GPIO0 = Raspi pinheader 11
57 FunctionSelectPin(transmit_pin, Output);
58
59 // Lift the priority of the thread in order to allow for a precise time keeping during bit banging.
60 BoostThreadPriority();
61
62 // The minimum of datagrams to repeat is around 10.
63 // Let's send it 60 times to be on the save side.
64 // The transmitter of the bell sends it around 67 times.
65 for (int i=0; i<60; i++)
66 {
67 SendDatagram(transmit_pin);
68 }
69
70 return 0;
71 }
72
73
74 void SendDatagram (unsigned short pin)
75 {
76 const char* pBuffer = g_pCode;
77 while (*pBuffer)
78 {
79 switch (*pBuffer)
80 {
81 case 'A' : WritePin (pin, A.m_signal);
82 Delay_MicroSeconds((unsigned long) A.m_time);
83 break;
84 case 'B' : WritePin (pin, B.m_signal);
85 Delay_MicroSeconds((unsigned long) B.m_time);
86 break;
87 case 'C' : WritePin (pin, C.m_signal);
88 Delay_MicroSeconds((unsigned long) C.m_time);
89 break;
90 case 'D' : WritePin (pin, D.m_signal);
91 Delay_MicroSeconds((unsigned long) D.m_time);
92 break;
93 case 'E' : WritePin (pin, E.m_signal);
94 Delay_MicroSeconds((unsigned long) E.m_time);
95 break;
96 default:
97 printf ("Error - unknown code\n");
98 return;
99 }
100 pBuffer++;
101 }
102 }
103
104 #include <pthread.h>
105 #include <assert.h>
106 void BoostThreadPriority()
107 {
108 // Scheduling params.
109 sched_param param;
110 int policy;
111 int ret;
112 pthread_t threadHandle = pthread_self();
113
114 // Get current scheduling parameters of thread.
115 ret = pthread_getschedparam (threadHandle, &policy, &param);
116 assert (ret == 0);
117
118 // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
119 policy = SCHED_FIFO; // SCHED_RR;
120 param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
121 ret = pthread_setschedparam(threadHandle, policy, &param);
122 assert (ret == 0);
123 }
124
125
BCM2835::Output
Definition: hubolib.h:918
A
CCode A(340, 1)
HuboLib::Delay_MicroSeconds
void Delay_MicroSeconds(unsigned long delay_micros)
delays the execution of the calling thread for the given number of micro seconds. ...
SendDatagram
void SendDatagram(unsigned short pin)
Definition: MD17177.cpp:74
BCM2835::IsGPIOInitialized
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
CCode
Definition: MD17177.cpp:23
BCM2835::FunctionSelectPin
void FunctionSelectPin(unsigned char pin, FunctionSelectType mode)
Use FunctionSelect() to configure the mode of a BCM2835 pin.
CCode::m_time
unsigned short m_time
Definition: MD17177.cpp:33
CCode::m_signal
unsigned char m_signal
Definition: MD17177.cpp:34
main
int main(int argc, char *argv[])
Definition: MD17177.cpp:47
BCM2835::WritePin
void WritePin(unsigned char pin, unsigned char value)
Sets or clears a pin configured to be an output.
B
CCode B(395, 0)
E
CCode E(5830, 0)
C
CCode C(1060, 1)
D
CCode D(1120, 0)
BoostThreadPriority
void BoostThreadPriority()
Definition: MD17177.cpp:106
BCM2835
Definition: hubocfg.h:71
g_pCode
const char * g_pCode
Definition: MD17177.cpp:21
CCode::CCode
CCode(unsigned short time, unsigned char signal)
Definition: MD17177.cpp:26
HuboLib
Definition: hubocfg.h:10