1 #include "pirrx51hardware.h"
5 // Needed system includes:
12 #include <linux/types.h>
13 #include <linux/ioctl.h>
16 // Includes I'm using for error handling stuff:
17 #include "pirexception.h"
21 // The N900's IR transmitter is controlled by a device driver created
22 // specifically for the LIRC daemon:
23 #define PATH_TO_LIRC_DEVICE "/dev/lirc0"
25 // It appears that the frequency on this device can range between
26 // 20000 on the low end and 500000 on the high end...
27 // 38000 is the default for LIRC:
28 //#define DEFAULT_FREQUENCY 38000
30 // The duty cycle is a percentage (0-100), 50 is LIRC's default:
31 //#define DEFAULT_DUTY_CYCLE 50
34 PIRRX51Hardware::PIRRX51Hardware()
42 PIRRX51Hardware::PIRRX51Hardware(
43 unsigned int frequency,
44 unsigned int dutyCycle)
49 setCarrierFrequency(frequency);
50 setDutyCycle(dutyCycle);
54 PIRRX51Hardware::~PIRRX51Hardware()
56 if (fileDescriptor >= 0) close(fileDescriptor);
60 void PIRRX51Hardware::openLircDevice()
63 // check the current time:
64 gettimeofday(&previousTime, NULL);
66 fileDescriptor = open(PATH_TO_LIRC_DEVICE, O_WRONLY);
68 if (fileDescriptor == -1)
71 ss << "Failed to connect to " << PATH_TO_LIRC_DEVICE << "\n";
72 ss << "Error is " << strerror(errno) << "\n";
73 throw PIRException(ss.str());
78 void PIRRX51Hardware::addPair(
82 if (index >= (BUFFER_SIZE - 1))
84 // Needed room for 2 ints, didn't have it.
85 throw PIRException("Buffer overflow in PIRCommandBuffer object.\n");
88 buffer[index] = pulse;
90 buffer[index] = space;
95 void PIRRX51Hardware::addSingle(
98 if (index >= BUFFER_SIZE)
100 throw PIRException("Buffer overflow in PIRCommandBuffer object.\n");
103 buffer[index] = single;
108 void PIRRX51Hardware::sendCommandToDevice()
110 // Sanity check first:
114 // We should probably complain here, but for now, just return.
118 // Note: if the generated command string ends on a "space", we'll just
119 // go ahead and ignore that last value. Want the light switched off
120 // permanently at the end of the command string, not temporarily.
121 // So, only odd-numbered strings of commands are allowed:
122 if ((index % 2) == 0)
129 gettimeofday(&newTime, NULL);
130 long microseconds = newTime.tv_usec - previousTime.tv_usec;
131 microseconds += (newTime.tv_sec - previousTime.tv_sec) * 1000000;
132 std::cout << "Time since last call to device: " << microseconds << std::endl;
133 previousTime = newTime;
135 std::cout << "Sending array of ints to device:\n";
139 std::cout << "buffer[" << blah << "]: " << buffer[blah] << "\n";
142 std::cout << std::endl;
144 if (write(fileDescriptor, buffer, index * sizeof(int)) == -1)
146 std::stringstream ss;
147 ss << "Failed to send command.\n";
148 ss << "IR device returned error: " << strerror(errno) << "\n";
149 throw PIRException(ss.str());
157 void PIRRX51Hardware::setCarrierFrequency(
158 unsigned int frequency)
160 // if (!frequency) frequency = DEFAULT_FREQUENCY;
163 std::cout << "Setting frequency to " << frequency << std::endl;
165 if (ioctl(fileDescriptor, _IOW('i', 0x13, __u32), &frequency) == -1)
167 std::stringstream ss;
168 ss << "Failed to set carrier frequency.\n";
169 ss << "IR device returned error: " << strerror(errno) << "\n";
170 throw PIRException(ss.str());
175 void PIRRX51Hardware::setDutyCycle(
176 unsigned int dutyCycle)
178 // if (dutyCycle > 100) dutyCycle = DEFAULT_DUTY_CYCLE;
181 std::cout << "Setting duty cycle to " << dutyCycle << std::endl;
183 if (ioctl(fileDescriptor, _IOW('i', 0x15, __u32), &dutyCycle) == -1)
185 std::stringstream ss;
186 ss << "Failed to set duty cycle percentage.\n";
187 ss << "IR device returned error: " << strerror(errno) << "\n";
188 throw PIRException(ss.str());