1 #include "pirrx51hardware.h"
5 // Needed system includes:
13 #include <linux/types.h>
14 #include <linux/ioctl.h>
18 // Includes I'm using for error handling stuff:
19 #include "pirexception.h"
23 // The N900's IR transmitter is controlled by a device driver created
24 // specifically for the LIRC daemon:
25 #define PATH_TO_LIRC_DEVICE "/dev/lirc0"
27 // It appears that the frequency on this device can range between
28 // 20000 on the low end and 500000 on the high end...
29 // 38000 is the default for LIRC:
30 //#define DEFAULT_FREQUENCY 38000
32 // The duty cycle is a percentage (0-100), 50 is LIRC's default:
33 //#define DEFAULT_DUTY_CYCLE 50
36 PIRRX51Hardware::PIRRX51Hardware()
44 PIRRX51Hardware::PIRRX51Hardware(
45 unsigned int frequency,
46 unsigned int dutyCycle)
51 setCarrierFrequency(frequency);
52 setDutyCycle(dutyCycle);
56 PIRRX51Hardware::~PIRRX51Hardware()
58 if (fileDescriptor >= 0) close(fileDescriptor);
62 void PIRRX51Hardware::openLircDevice()
65 // check the current time:
66 gettimeofday(&previousTime, NULL);
69 fileDescriptor = open(PATH_TO_LIRC_DEVICE, O_WRONLY);
71 if (fileDescriptor == -1)
74 ss << "Failed to connect to " << PATH_TO_LIRC_DEVICE << "\n";
75 ss << "Error is " << strerror(errno) << "\n";
76 throw PIRException(ss.str());
82 void PIRRX51Hardware::addPair(
86 if (index >= (BUFFER_SIZE - 1))
88 // Needed room for 2 ints, didn't have it.
89 throw PIRException("Buffer overflow in PIRCommandBuffer object.\n");
92 buffer[index] = pulse;
94 buffer[index] = space;
99 void PIRRX51Hardware::addSingle(
102 if (index >= BUFFER_SIZE)
104 throw PIRException("Buffer overflow in PIRCommandBuffer object.\n");
107 buffer[index] = single;
112 void PIRRX51Hardware::sendCommandToDevice()
114 // Sanity check first:
118 // We should probably complain here, but for now, just return.
122 // Note: if the generated command string ends on a "space", we'll just
123 // go ahead and ignore that last value. Want the light switched off
124 // permanently at the end of the command string, not temporarily.
125 // So, only odd-numbered strings of commands are allowed:
126 if ((index % 2) == 0)
133 gettimeofday(&newTime, NULL);
134 long microseconds = newTime.tv_usec - previousTime.tv_usec;
135 microseconds += (newTime.tv_sec - previousTime.tv_sec) * 1000000;
136 std::cout << "Time since last call to device: " << microseconds << std::endl;
137 previousTime = newTime;
139 std::cout << "Sending array of ints to device:\n";
143 std::cout << "buffer[" << blah << "]: " << buffer[blah] << "\n";
146 std::cout << std::endl;
148 if (write(fileDescriptor, buffer, index * sizeof(int)) == -1)
150 std::stringstream ss;
151 ss << "Failed to send command.\n";
152 ss << "IR device returned error: " << strerror(errno) << "\n";
153 throw PIRException(ss.str());
162 void PIRRX51Hardware::setCarrierFrequency(
163 unsigned int frequency)
165 // if (!frequency) frequency = DEFAULT_FREQUENCY;
168 std::cout << "Setting frequency to " << frequency << "\n";
170 if (ioctl(fileDescriptor, _IOW('i', 0x13, __u32), &frequency) == -1)
172 std::stringstream ss;
173 ss << "Failed to set carrier frequency.\n";
174 ss << "IR device returned error: " << strerror(errno) << "\n";
175 throw PIRException(ss.str());
181 void PIRRX51Hardware::setDutyCycle(
182 unsigned int dutyCycle)
184 // if (dutyCycle > 100) dutyCycle = DEFAULT_DUTY_CYCLE;
187 std::cout << "Setting duty cycle to " << dutyCycle << "\n";
189 if (ioctl(fileDescriptor, _IOW('i', 0x15, __u32), &dutyCycle) == -1)
191 std::stringstream ss;
192 ss << "Failed to set duty cycle percentage.\n";
193 ss << "IR device returned error: " << strerror(errno) << "\n";
194 throw PIRException(ss.str());