wiringpi-ruby 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/ext/wiringpi/WiringPi/devLib/ds1302.c +240 -0
  3. data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
  4. data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
  5. data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
  6. data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
  7. data/ext/wiringpi/WiringPi/devLib/lcd.c +495 -0
  8. data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
  9. data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +673 -0
  10. data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
  11. data/ext/wiringpi/WiringPi/devLib/maxdetect.c +238 -0
  12. data/ext/wiringpi/WiringPi/devLib/maxdetect.h +40 -0
  13. data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
  14. data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
  15. data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +177 -0
  16. data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
  17. data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
  18. data/ext/wiringpi/WiringPi/devLib/piNes.c +113 -0
  19. data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
  20. data/ext/wiringpi/WiringPi/devLib/scrollPhat.c +430 -0
  21. data/ext/wiringpi/WiringPi/devLib/scrollPhat.h +39 -0
  22. data/ext/wiringpi/WiringPi/devLib/scrollPhatFont.h +544 -0
  23. data/ext/wiringpi/WiringPi/examples/Gertboard/7segments.c +221 -0
  24. data/ext/wiringpi/WiringPi/examples/Gertboard/buttons.c +83 -0
  25. data/ext/wiringpi/WiringPi/examples/Gertboard/gertboard.c +96 -0
  26. data/ext/wiringpi/WiringPi/examples/Gertboard/record.c +60 -0
  27. data/ext/wiringpi/WiringPi/examples/Gertboard/temperature.c +78 -0
  28. data/ext/wiringpi/WiringPi/examples/Gertboard/voltmeter.c +73 -0
  29. data/ext/wiringpi/WiringPi/examples/Gertboard/vumeter.c +152 -0
  30. data/ext/wiringpi/WiringPi/examples/PiFace/blink.c +59 -0
  31. data/ext/wiringpi/WiringPi/examples/PiFace/buttons.c +103 -0
  32. data/ext/wiringpi/WiringPi/examples/PiFace/ladder.c +337 -0
  33. data/ext/wiringpi/WiringPi/examples/PiFace/metro.c +111 -0
  34. data/ext/wiringpi/WiringPi/examples/PiFace/motor.c +120 -0
  35. data/ext/wiringpi/WiringPi/examples/PiFace/reaction.c +194 -0
  36. data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow0.c +51 -0
  37. data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow1.c +258 -0
  38. data/ext/wiringpi/WiringPi/examples/PiGlow/piglow.c +176 -0
  39. data/ext/wiringpi/WiringPi/examples/blink-thread.c +61 -0
  40. data/ext/wiringpi/WiringPi/examples/blink.c +48 -0
  41. data/ext/wiringpi/WiringPi/examples/blink12.c +111 -0
  42. data/ext/wiringpi/WiringPi/examples/blink12drcs.c +125 -0
  43. data/ext/wiringpi/WiringPi/examples/blink6drcs.c +115 -0
  44. data/ext/wiringpi/WiringPi/examples/blink8-drcn.c +61 -0
  45. data/ext/wiringpi/WiringPi/examples/blink8.c +57 -0
  46. data/ext/wiringpi/WiringPi/examples/clock.c +201 -0
  47. data/ext/wiringpi/WiringPi/examples/delayTest.c +102 -0
  48. data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
  49. data/ext/wiringpi/WiringPi/examples/header.h +23 -0
  50. data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
  51. data/ext/wiringpi/WiringPi/examples/isr.c +110 -0
  52. data/ext/wiringpi/WiringPi/examples/lcd-adafruit.c +347 -0
  53. data/ext/wiringpi/WiringPi/examples/lcd.c +286 -0
  54. data/ext/wiringpi/WiringPi/examples/lowPower.c +68 -0
  55. data/ext/wiringpi/WiringPi/examples/max31855.c +60 -0
  56. data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
  57. data/ext/wiringpi/WiringPi/examples/okLed.c +82 -0
  58. data/ext/wiringpi/WiringPi/examples/pwm.c +58 -0
  59. data/ext/wiringpi/WiringPi/examples/q2w/binary.c +79 -0
  60. data/ext/wiringpi/WiringPi/examples/q2w/blink-io.c +61 -0
  61. data/ext/wiringpi/WiringPi/examples/q2w/blink.c +50 -0
  62. data/ext/wiringpi/WiringPi/examples/q2w/bright.c +59 -0
  63. data/ext/wiringpi/WiringPi/examples/q2w/button.c +63 -0
  64. data/ext/wiringpi/WiringPi/examples/q2w/volts.c +62 -0
  65. data/ext/wiringpi/WiringPi/examples/rht03.c +86 -0
  66. data/ext/wiringpi/WiringPi/examples/scrollPhat/scphat.c +230 -0
  67. data/ext/wiringpi/WiringPi/examples/scrollPhat/test.c +115 -0
  68. data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
  69. data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
  70. data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
  71. data/ext/wiringpi/WiringPi/examples/softPwm.c +89 -0
  72. data/ext/wiringpi/WiringPi/examples/softTone.c +54 -0
  73. data/ext/wiringpi/WiringPi/examples/speed.c +95 -0
  74. data/ext/wiringpi/WiringPi/examples/spiSpeed.c +118 -0
  75. data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
  76. data/ext/wiringpi/WiringPi/gpio/gpio.c +1538 -0
  77. data/ext/wiringpi/WiringPi/gpio/readall.c +403 -0
  78. data/ext/wiringpi/WiringPi/version.h +3 -0
  79. data/ext/wiringpi/WiringPi/wiringPi/ads1115.c +293 -0
  80. data/ext/wiringpi/WiringPi/wiringPi/ads1115.h +55 -0
  81. data/ext/wiringpi/WiringPi/wiringPi/bmp180.c +237 -0
  82. data/ext/wiringpi/WiringPi/wiringPi/bmp180.h +34 -0
  83. data/ext/wiringpi/WiringPi/wiringPi/drcNet.c +405 -0
  84. data/ext/wiringpi/WiringPi/wiringPi/drcNet.h +42 -0
  85. data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +196 -0
  86. data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
  87. data/ext/wiringpi/WiringPi/wiringPi/ds18b20.c +146 -0
  88. data/ext/wiringpi/WiringPi/wiringPi/ds18b20.h +34 -0
  89. data/ext/wiringpi/WiringPi/wiringPi/htu21d.c +150 -0
  90. data/ext/wiringpi/WiringPi/wiringPi/htu21d.h +34 -0
  91. data/ext/wiringpi/WiringPi/wiringPi/max31855.c +99 -0
  92. data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
  93. data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
  94. data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
  95. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
  96. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
  97. data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
  98. data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
  99. data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
  100. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
  101. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
  102. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +188 -0
  103. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
  104. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +235 -0
  105. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
  106. data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
  107. data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
  108. data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
  109. data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
  110. data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
  111. data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
  112. data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +125 -0
  113. data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
  114. data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
  115. data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
  116. data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
  117. data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
  118. data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +90 -0
  119. data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
  120. data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
  121. data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
  122. data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.c +95 -0
  123. data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.h +26 -0
  124. data/ext/wiringpi/WiringPi/wiringPi/rht03.c +252 -0
  125. data/ext/wiringpi/WiringPi/wiringPi/rht03.h +25 -0
  126. data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
  127. data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
  128. data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +186 -0
  129. data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
  130. data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
  131. data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
  132. data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
  133. data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
  134. data/ext/wiringpi/WiringPi/wiringPi/sr595.c +109 -0
  135. data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
  136. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +2507 -0
  137. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +269 -0
  138. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +233 -0
  139. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
  140. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +144 -0
  141. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +36 -0
  142. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +225 -0
  143. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
  144. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
  145. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
  146. data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +928 -0
  147. data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.h +26 -0
  148. data/ext/wiringpi/WiringPi/wiringPiD/daemonise.c +82 -0
  149. data/ext/wiringpi/WiringPi/wiringPiD/daemonise.h +9 -0
  150. data/ext/wiringpi/WiringPi/wiringPiD/drcNetCmd.h +43 -0
  151. data/ext/wiringpi/WiringPi/wiringPiD/network.c +330 -0
  152. data/ext/wiringpi/WiringPi/wiringPiD/network.h +31 -0
  153. data/ext/wiringpi/WiringPi/wiringPiD/runRemote.c +126 -0
  154. data/ext/wiringpi/WiringPi/wiringPiD/runRemote.h +29 -0
  155. data/ext/wiringpi/WiringPi/wiringPiD/wiringpid.c +382 -0
  156. metadata +155 -1
@@ -0,0 +1,75 @@
1
+ /*
2
+ * sn3218.c:
3
+ * Extend wiringPi with the SN3218 I2C LEd Driver
4
+ * Copyright (c) 2012-2013 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #include <wiringPi.h>
26
+ #include <wiringPiI2C.h>
27
+
28
+ #include "sn3218.h"
29
+
30
+ /*
31
+ * myAnalogWrite:
32
+ * Write analog value on the given pin
33
+ *********************************************************************************
34
+ */
35
+
36
+ static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
37
+ {
38
+ int fd = node->fd ;
39
+ int chan = 0x01 + (pin - node->pinBase) ;
40
+
41
+ wiringPiI2CWriteReg8 (fd, chan, value & 0xFF) ; // Value
42
+ wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
43
+ }
44
+
45
+ /*
46
+ * sn3218Setup:
47
+ * Create a new wiringPi device node for an sn3218 on the Pi's
48
+ * SPI interface.
49
+ *********************************************************************************
50
+ */
51
+
52
+ int sn3218Setup (const int pinBase)
53
+ {
54
+ int fd ;
55
+ struct wiringPiNodeStruct *node ;
56
+
57
+ if ((fd = wiringPiI2CSetup (0x54)) < 0)
58
+ return FALSE ;
59
+
60
+ // Setup the chip - initialise all 18 LEDs to off
61
+
62
+ //wiringPiI2CWriteReg8 (fd, 0x17, 0) ; // Reset
63
+ wiringPiI2CWriteReg8 (fd, 0x00, 1) ; // Not Shutdown
64
+ wiringPiI2CWriteReg8 (fd, 0x13, 0x3F) ; // Enable LEDs 0- 5
65
+ wiringPiI2CWriteReg8 (fd, 0x14, 0x3F) ; // Enable LEDs 6-11
66
+ wiringPiI2CWriteReg8 (fd, 0x15, 0x3F) ; // Enable LEDs 12-17
67
+ wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
68
+
69
+ node = wiringPiNewNode (pinBase, 18) ;
70
+
71
+ node->fd = fd ;
72
+ node->analogWrite = myAnalogWrite ;
73
+
74
+ return TRUE ;
75
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * sn3218.c:
3
+ * Extend wiringPi with the SN3218 I2C LED driver board.
4
+ * Copyright (c) 2012-2013 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ extern int sn3218Setup (int pinBase) ;
30
+
31
+ #ifdef __cplusplus
32
+ }
33
+ #endif
@@ -0,0 +1,186 @@
1
+ /*
2
+ * softPwm.c:
3
+ * Provide many channels of software driven PWM.
4
+ * Copyright (c) 2012-2017 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #include <stdio.h>
26
+ #include <malloc.h>
27
+ #include <pthread.h>
28
+
29
+ #include "wiringPi.h"
30
+ #include "softPwm.h"
31
+
32
+ // MAX_PINS:
33
+ // This is more than the number of Pi pins because we can actually softPwm.
34
+ // Once upon a time I let pins on gpio expanders be softPwm'd, but it's really
35
+ // really not a good thing.
36
+
37
+ #define MAX_PINS 64
38
+
39
+ // The PWM Frequency is derived from the "pulse time" below. Essentially,
40
+ // the frequency is a function of the range and this pulse time.
41
+ // The total period will be range * pulse time in µS, so a pulse time
42
+ // of 100 and a range of 100 gives a period of 100 * 100 = 10,000 µS
43
+ // which is a frequency of 100Hz.
44
+ //
45
+ // It's possible to get a higher frequency by lowering the pulse time,
46
+ // however CPU uage will skyrocket as wiringPi uses a hard-loop to time
47
+ // periods under 100µS - this is because the Linux timer calls are just
48
+ // not accurate at all, and have an overhead.
49
+ //
50
+ // Another way to increase the frequency is to reduce the range - however
51
+ // that reduces the overall output accuracy...
52
+
53
+ #define PULSE_TIME 100
54
+
55
+ static volatile int marks [MAX_PINS] ;
56
+ static volatile int range [MAX_PINS] ;
57
+ static volatile pthread_t threads [MAX_PINS] ;
58
+ static volatile int newPin = -1 ;
59
+
60
+
61
+ /*
62
+ * softPwmThread:
63
+ * Thread to do the actual PWM output
64
+ *********************************************************************************
65
+ */
66
+
67
+ static void *softPwmThread (void *arg)
68
+ {
69
+ int pin, mark, space ;
70
+ struct sched_param param ;
71
+
72
+ param.sched_priority = sched_get_priority_max (SCHED_RR) ;
73
+ pthread_setschedparam (pthread_self (), SCHED_RR, &param) ;
74
+
75
+ pin = *((int *)arg) ;
76
+ free (arg) ;
77
+
78
+ pin = newPin ;
79
+ newPin = -1 ;
80
+
81
+ piHiPri (90) ;
82
+
83
+ for (;;)
84
+ {
85
+ mark = marks [pin] ;
86
+ space = range [pin] - mark ;
87
+
88
+ if (mark != 0)
89
+ digitalWrite (pin, HIGH) ;
90
+ delayMicroseconds (mark * 100) ;
91
+
92
+ if (space != 0)
93
+ digitalWrite (pin, LOW) ;
94
+ delayMicroseconds (space * 100) ;
95
+ }
96
+
97
+ return NULL ;
98
+ }
99
+
100
+
101
+ /*
102
+ * softPwmWrite:
103
+ * Write a PWM value to the given pin
104
+ *********************************************************************************
105
+ */
106
+
107
+ void softPwmWrite (int pin, int value)
108
+ {
109
+ if (pin < MAX_PINS)
110
+ {
111
+ /**/ if (value < 0)
112
+ value = 0 ;
113
+ else if (value > range [pin])
114
+ value = range [pin] ;
115
+
116
+ marks [pin] = value ;
117
+ }
118
+ }
119
+
120
+
121
+ /*
122
+ * softPwmCreate:
123
+ * Create a new softPWM thread.
124
+ *********************************************************************************
125
+ */
126
+
127
+ int softPwmCreate (int pin, int initialValue, int pwmRange)
128
+ {
129
+ int res ;
130
+ pthread_t myThread ;
131
+ int *passPin ;
132
+
133
+ if (pin >= MAX_PINS)
134
+ return -1 ;
135
+
136
+ if (range [pin] != 0) // Already running on this pin
137
+ return -1 ;
138
+
139
+ if (pwmRange <= 0)
140
+ return -1 ;
141
+
142
+ passPin = malloc (sizeof (*passPin)) ;
143
+ if (passPin == NULL)
144
+ return -1 ;
145
+
146
+ digitalWrite (pin, LOW) ;
147
+ pinMode (pin, OUTPUT) ;
148
+
149
+ marks [pin] = initialValue ;
150
+ range [pin] = pwmRange ;
151
+
152
+ *passPin = pin ;
153
+ newPin = pin ;
154
+ res = pthread_create (&myThread, NULL, softPwmThread, (void *)passPin) ;
155
+
156
+ if (res != 0)
157
+ return res ;
158
+
159
+ while (newPin != -1)
160
+ delay (1) ;
161
+
162
+ threads [pin] = myThread ;
163
+
164
+ return res ;
165
+ }
166
+
167
+
168
+ /*
169
+ * softPwmStop:
170
+ * Stop an existing softPWM thread
171
+ *********************************************************************************
172
+ */
173
+
174
+ void softPwmStop (int pin)
175
+ {
176
+ if (pin < MAX_PINS)
177
+ {
178
+ if (range [pin] != 0)
179
+ {
180
+ pthread_cancel (threads [pin]) ;
181
+ pthread_join (threads [pin], NULL) ;
182
+ range [pin] = 0 ;
183
+ digitalWrite (pin, LOW) ;
184
+ }
185
+ }
186
+ }
@@ -0,0 +1,35 @@
1
+ /*
2
+ * softPwm.h:
3
+ * Provide 2 channels of software driven PWM.
4
+ * Copyright (c) 2012 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ extern int softPwmCreate (int pin, int value, int range) ;
30
+ extern void softPwmWrite (int pin, int value) ;
31
+ extern void softPwmStop (int pin) ;
32
+
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
@@ -0,0 +1,211 @@
1
+ /*
2
+ * softServo.c:
3
+ * Provide N channels of software driven PWM suitable for RC
4
+ * servo motors.
5
+ * Copyright (c) 2012 Gordon Henderson
6
+ ***********************************************************************
7
+ * This file is part of wiringPi:
8
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
9
+ *
10
+ * wiringPi is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU Lesser General Public License as
12
+ * published by the Free Software Foundation, either version 3 of the
13
+ * License, or (at your option) any later version.
14
+ *
15
+ * wiringPi is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ * GNU Lesser General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU Lesser General Public
21
+ * License along with wiringPi.
22
+ * If not, see <http://www.gnu.org/licenses/>.
23
+ ***********************************************************************
24
+ */
25
+
26
+ //#include <stdio.h>
27
+ #include <string.h>
28
+ #include <time.h>
29
+ #include <sys/time.h>
30
+ #include <pthread.h>
31
+
32
+ #include "wiringPi.h"
33
+ #include "softServo.h"
34
+
35
+ // RC Servo motors are a bit of an oddity - designed in the days when
36
+ // radio control was experimental and people were tryin to make
37
+ // things as simple as possible as it was all very expensive...
38
+ //
39
+ // So... To drive an RC Servo motor, you need to send it a modified PWM
40
+ // signal - it needs anything from 1ms to 2ms - with 1ms meaning
41
+ // to move the server fully left, and 2ms meaning to move it fully
42
+ // right. Then you need a long gap before sending the next pulse.
43
+ // The reason for this is that you send a multiplexed stream of these
44
+ // pulses up the radio signal into the reciever which de-multiplexes
45
+ // them into the signals for each individual servo. Typically there
46
+ // might be 8 channels, so you need at least 8 "slots" of 2mS pulses
47
+ // meaning the entire frame must fit into a 16mS slot - which would
48
+ // then be repeated...
49
+ //
50
+ // In practice we have a total slot width of about 20mS - so we're sending 50
51
+ // updates per second to each servo.
52
+ //
53
+ // In this code, we don't need to be too fussy about the gap as we're not doing
54
+ // the multipexing, but it does need to be at least 10mS, and preferably 16
55
+ // from what I've been able to determine.
56
+
57
+ // WARNING:
58
+ // This code is really experimental. It was written in response to some people
59
+ // asking for a servo driver, however while it works, there is too much
60
+ // jitter to successfully drive a small servo - I have tried it with a micro
61
+ // servo and it worked, but the servo ran hot due to the jitter in the signal
62
+ // being sent to it.
63
+ //
64
+ // If you want servo control for the Pi, then use the servoblaster kernel
65
+ // module.
66
+
67
+ #define MAX_SERVOS 8
68
+
69
+ static int pinMap [MAX_SERVOS] ; // Keep track of our pins
70
+ static int pulseWidth [MAX_SERVOS] ; // microseconds
71
+
72
+
73
+ /*
74
+ * softServoThread:
75
+ * Thread to do the actual Servo PWM output
76
+ *********************************************************************************
77
+ */
78
+
79
+ static PI_THREAD (softServoThread)
80
+ {
81
+ register int i, j, k, m, tmp ;
82
+ int lastDelay, pin, servo ;
83
+
84
+ int myDelays [MAX_SERVOS] ;
85
+ int myPins [MAX_SERVOS] ;
86
+
87
+ struct timeval tNow, tStart, tPeriod, tGap, tTotal ;
88
+ struct timespec tNs ;
89
+
90
+ tTotal.tv_sec = 0 ;
91
+ tTotal.tv_usec = 8000 ;
92
+
93
+ piHiPri (50) ;
94
+
95
+ for (;;)
96
+ {
97
+ gettimeofday (&tStart, NULL) ;
98
+
99
+ memcpy (myDelays, pulseWidth, sizeof (myDelays)) ;
100
+ memcpy (myPins, pinMap, sizeof (myPins)) ;
101
+
102
+ // Sort the delays (& pins), shortest first
103
+
104
+ for (m = MAX_SERVOS / 2 ; m > 0 ; m /= 2 )
105
+ for (j = m ; j < MAX_SERVOS ; ++j)
106
+ for (i = j - m ; i >= 0 ; i -= m)
107
+ {
108
+ k = i + m ;
109
+ if (myDelays [k] >= myDelays [i])
110
+ break ;
111
+ else // Swap
112
+ {
113
+ tmp = myDelays [i] ; myDelays [i] = myDelays [k] ; myDelays [k] = tmp ;
114
+ tmp = myPins [i] ; myPins [i] = myPins [k] ; myPins [k] = tmp ;
115
+ }
116
+ }
117
+
118
+ // All on
119
+
120
+ lastDelay = 0 ;
121
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
122
+ {
123
+ if ((pin = myPins [servo]) == -1)
124
+ continue ;
125
+
126
+ digitalWrite (pin, HIGH) ;
127
+ myDelays [servo] = myDelays [servo] - lastDelay ;
128
+ lastDelay += myDelays [servo] ;
129
+ }
130
+
131
+ // Now loop, turning them all off as required
132
+
133
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
134
+ {
135
+ if ((pin = myPins [servo]) == -1)
136
+ continue ;
137
+
138
+ delayMicroseconds (myDelays [servo]) ;
139
+ digitalWrite (pin, LOW) ;
140
+ }
141
+
142
+ // Wait until the end of an 8mS time-slot
143
+
144
+ gettimeofday (&tNow, NULL) ;
145
+ timersub (&tNow, &tStart, &tPeriod) ;
146
+ timersub (&tTotal, &tPeriod, &tGap) ;
147
+ tNs.tv_sec = tGap.tv_sec ;
148
+ tNs.tv_nsec = tGap.tv_usec * 1000 ;
149
+ nanosleep (&tNs, NULL) ;
150
+ }
151
+
152
+ return NULL ;
153
+ }
154
+
155
+
156
+ /*
157
+ * softServoWrite:
158
+ * Write a Servo value to the given pin
159
+ *********************************************************************************
160
+ */
161
+
162
+ void softServoWrite (int servoPin, int value)
163
+ {
164
+ int servo ;
165
+
166
+ servoPin &= 63 ;
167
+
168
+ /**/ if (value < -250)
169
+ value = -250 ;
170
+ else if (value > 1250)
171
+ value = 1250 ;
172
+
173
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
174
+ if (pinMap [servo] == servoPin)
175
+ pulseWidth [servo] = value + 1000 ; // uS
176
+ }
177
+
178
+
179
+ /*
180
+ * softServoSetup:
181
+ * Setup the software servo system
182
+ *********************************************************************************
183
+ */
184
+
185
+ int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7)
186
+ {
187
+ int servo ;
188
+
189
+ if (p0 != -1) { pinMode (p0, OUTPUT) ; digitalWrite (p0, LOW) ; }
190
+ if (p1 != -1) { pinMode (p1, OUTPUT) ; digitalWrite (p1, LOW) ; }
191
+ if (p2 != -1) { pinMode (p2, OUTPUT) ; digitalWrite (p2, LOW) ; }
192
+ if (p3 != -1) { pinMode (p3, OUTPUT) ; digitalWrite (p3, LOW) ; }
193
+ if (p4 != -1) { pinMode (p4, OUTPUT) ; digitalWrite (p4, LOW) ; }
194
+ if (p5 != -1) { pinMode (p5, OUTPUT) ; digitalWrite (p5, LOW) ; }
195
+ if (p6 != -1) { pinMode (p6, OUTPUT) ; digitalWrite (p6, LOW) ; }
196
+ if (p7 != -1) { pinMode (p7, OUTPUT) ; digitalWrite (p7, LOW) ; }
197
+
198
+ pinMap [0] = p0 ;
199
+ pinMap [1] = p1 ;
200
+ pinMap [2] = p2 ;
201
+ pinMap [3] = p3 ;
202
+ pinMap [4] = p4 ;
203
+ pinMap [5] = p5 ;
204
+ pinMap [6] = p6 ;
205
+ pinMap [7] = p7 ;
206
+
207
+ for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
208
+ pulseWidth [servo] = 1500 ; // Mid point
209
+
210
+ return piThreadCreate (softServoThread) ;
211
+ }