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,33 @@
1
+ /*
2
+ * 23017.h:
3
+ * Extend wiringPi with the MCP 23017 I2C GPIO expander chip
4
+ * Copyright (c) 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 mcp23017Setup (const int pinBase, const int i2cAddress) ;
30
+
31
+ #ifdef __cplusplus
32
+ }
33
+ #endif
@@ -0,0 +1,188 @@
1
+ /*
2
+ * mcp23s08.c:
3
+ * Extend wiringPi with the MCP 23s08 SPI GPIO expander chip
4
+ * Copyright (c) 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 <stdio.h>
26
+ #include <stdint.h>
27
+
28
+ #include "wiringPi.h"
29
+ #include "wiringPiSPI.h"
30
+ #include "mcp23x0817.h"
31
+
32
+ #include "mcp23s08.h"
33
+
34
+ #define MCP_SPEED 4000000
35
+
36
+
37
+
38
+ /*
39
+ * writeByte:
40
+ * Write a byte to a register on the MCP23s08 on the SPI bus.
41
+ *********************************************************************************
42
+ */
43
+
44
+ static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
45
+ {
46
+ uint8_t spiData [4] ;
47
+
48
+ spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
49
+ spiData [1] = reg ;
50
+ spiData [2] = data ;
51
+
52
+ wiringPiSPIDataRW (spiPort, spiData, 3) ;
53
+ }
54
+
55
+ /*
56
+ * readByte:
57
+ * Read a byte from a register on the MCP23s08 on the SPI bus.
58
+ *********************************************************************************
59
+ */
60
+
61
+ static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
62
+ {
63
+ uint8_t spiData [4] ;
64
+
65
+ spiData [0] = CMD_READ | ((devId & 7) << 1) ;
66
+ spiData [1] = reg ;
67
+
68
+ wiringPiSPIDataRW (spiPort, spiData, 3) ;
69
+
70
+ return spiData [2] ;
71
+ }
72
+
73
+
74
+ /*
75
+ * myPinMode:
76
+ *********************************************************************************
77
+ */
78
+
79
+ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
80
+ {
81
+ int mask, old, reg ;
82
+
83
+ reg = MCP23x08_IODIR ;
84
+ mask = 1 << (pin - node->pinBase) ;
85
+ old = readByte (node->data0, node->data1, reg) ;
86
+
87
+ if (mode == OUTPUT)
88
+ old &= (~mask) ;
89
+ else
90
+ old |= mask ;
91
+
92
+ writeByte (node->data0, node->data1, reg, old) ;
93
+ }
94
+
95
+
96
+ /*
97
+ * myPullUpDnControl:
98
+ *********************************************************************************
99
+ */
100
+
101
+ static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
102
+ {
103
+ int mask, old, reg ;
104
+
105
+ reg = MCP23x08_GPPU ;
106
+ mask = 1 << (pin - node->pinBase) ;
107
+
108
+ old = readByte (node->data0, node->data1, reg) ;
109
+
110
+ if (mode == PUD_UP)
111
+ old |= mask ;
112
+ else
113
+ old &= (~mask) ;
114
+
115
+ writeByte (node->data0, node->data1, reg, old) ;
116
+ }
117
+
118
+
119
+ /*
120
+ * myDigitalWrite:
121
+ *********************************************************************************
122
+ */
123
+
124
+ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
125
+ {
126
+ int bit, old ;
127
+
128
+ bit = 1 << ((pin - node->pinBase) & 7) ;
129
+
130
+ old = node->data2 ;
131
+ if (value == LOW)
132
+ old &= (~bit) ;
133
+ else
134
+ old |= bit ;
135
+
136
+ writeByte (node->data0, node->data1, MCP23x08_GPIO, old) ;
137
+ node->data2 = old ;
138
+ }
139
+
140
+
141
+ /*
142
+ * myDigitalRead:
143
+ *********************************************************************************
144
+ */
145
+
146
+ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
147
+ {
148
+ int mask, value ;
149
+
150
+ mask = 1 << ((pin - node->pinBase) & 7) ;
151
+ value = readByte (node->data0, node->data1, MCP23x08_GPIO) ;
152
+
153
+ if ((value & mask) == 0)
154
+ return LOW ;
155
+ else
156
+ return HIGH ;
157
+ }
158
+
159
+
160
+ /*
161
+ * mcp23s08Setup:
162
+ * Create a new instance of an MCP23s08 SPI GPIO interface. We know it
163
+ * has 8 pins, so all we need to know here is the SPI address and the
164
+ * user-defined pin base.
165
+ *********************************************************************************
166
+ */
167
+
168
+ int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
169
+ {
170
+ struct wiringPiNodeStruct *node ;
171
+
172
+ if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0)
173
+ return FALSE ;
174
+
175
+ writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
176
+
177
+ node = wiringPiNewNode (pinBase, 8) ;
178
+
179
+ node->data0 = spiPort ;
180
+ node->data1 = devId ;
181
+ node->pinMode = myPinMode ;
182
+ node->pullUpDnControl = myPullUpDnControl ;
183
+ node->digitalRead = myDigitalRead ;
184
+ node->digitalWrite = myDigitalWrite ;
185
+ node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
186
+
187
+ return TRUE ;
188
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * 23s08.h:
3
+ * Extend wiringPi with the MCP 23s08 SPI GPIO expander chip
4
+ * Copyright (c) 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 mcp23s08Setup (const int pinBase, const int spiPort, const int devId) ;
30
+
31
+ #ifdef __cplusplus
32
+ }
33
+ #endif
@@ -0,0 +1,235 @@
1
+ /*
2
+ * mcp23s17.c:
3
+ * Extend wiringPi with the MCP 23s17 SPI GPIO expander chip
4
+ * Copyright (c) 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 <stdio.h>
26
+ #include <stdint.h>
27
+
28
+ #include "wiringPi.h"
29
+ #include "wiringPiSPI.h"
30
+ #include "mcp23x0817.h"
31
+
32
+ #include "mcp23s17.h"
33
+
34
+ #define MCP_SPEED 4000000
35
+
36
+
37
+
38
+ /*
39
+ * writeByte:
40
+ * Write a byte to a register on the MCP23s17 on the SPI bus.
41
+ *********************************************************************************
42
+ */
43
+
44
+ static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
45
+ {
46
+ uint8_t spiData [4] ;
47
+
48
+ spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
49
+ spiData [1] = reg ;
50
+ spiData [2] = data ;
51
+
52
+ wiringPiSPIDataRW (spiPort, spiData, 3) ;
53
+ }
54
+
55
+ /*
56
+ * readByte:
57
+ * Read a byte from a register on the MCP23s17 on the SPI bus.
58
+ *********************************************************************************
59
+ */
60
+
61
+ static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
62
+ {
63
+ uint8_t spiData [4] ;
64
+
65
+ spiData [0] = CMD_READ | ((devId & 7) << 1) ;
66
+ spiData [1] = reg ;
67
+
68
+ wiringPiSPIDataRW (spiPort, spiData, 3) ;
69
+
70
+ return spiData [2] ;
71
+ }
72
+
73
+
74
+ /*
75
+ * myPinMode:
76
+ *********************************************************************************
77
+ */
78
+
79
+ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
80
+ {
81
+ int mask, old, reg ;
82
+
83
+ pin -= node->pinBase ;
84
+
85
+ if (pin < 8) // Bank A
86
+ reg = MCP23x17_IODIRA ;
87
+ else
88
+ {
89
+ reg = MCP23x17_IODIRB ;
90
+ pin &= 0x07 ;
91
+ }
92
+
93
+ mask = 1 << pin ;
94
+ old = readByte (node->data0, node->data1, reg) ;
95
+
96
+ if (mode == OUTPUT)
97
+ old &= (~mask) ;
98
+ else
99
+ old |= mask ;
100
+
101
+ writeByte (node->data0, node->data1, reg, old) ;
102
+ }
103
+
104
+
105
+ /*
106
+ * myPullUpDnControl:
107
+ *********************************************************************************
108
+ */
109
+
110
+ static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
111
+ {
112
+ int mask, old, reg ;
113
+
114
+ pin -= node->pinBase ;
115
+
116
+ if (pin < 8) // Bank A
117
+ reg = MCP23x17_GPPUA ;
118
+ else
119
+ {
120
+ reg = MCP23x17_GPPUB ;
121
+ pin &= 0x07 ;
122
+ }
123
+
124
+ mask = 1 << pin ;
125
+ old = readByte (node->data0, node->data1, reg) ;
126
+
127
+ if (mode == PUD_UP)
128
+ old |= mask ;
129
+ else
130
+ old &= (~mask) ;
131
+
132
+ writeByte (node->data0, node->data1, reg, old) ;
133
+ }
134
+
135
+
136
+ /*
137
+ * myDigitalWrite:
138
+ *********************************************************************************
139
+ */
140
+
141
+ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
142
+ {
143
+ int bit, old ;
144
+
145
+ pin -= node->pinBase ; // Pin now 0-15
146
+
147
+ bit = 1 << (pin & 7) ;
148
+
149
+ if (pin < 8) // Bank A
150
+ {
151
+ old = node->data2 ;
152
+
153
+ if (value == LOW)
154
+ old &= (~bit) ;
155
+ else
156
+ old |= bit ;
157
+
158
+ writeByte (node->data0, node->data1, MCP23x17_GPIOA, old) ;
159
+ node->data2 = old ;
160
+ }
161
+ else // Bank B
162
+ {
163
+ old = node->data3 ;
164
+
165
+ if (value == LOW)
166
+ old &= (~bit) ;
167
+ else
168
+ old |= bit ;
169
+
170
+ writeByte (node->data0, node->data1, MCP23x17_GPIOB, old) ;
171
+ node->data3 = old ;
172
+ }
173
+ }
174
+
175
+
176
+ /*
177
+ * myDigitalRead:
178
+ *********************************************************************************
179
+ */
180
+
181
+ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
182
+ {
183
+ int mask, value, gpio ;
184
+
185
+ pin -= node->pinBase ;
186
+
187
+ if (pin < 8) // Bank A
188
+ gpio = MCP23x17_GPIOA ;
189
+ else
190
+ {
191
+ gpio = MCP23x17_GPIOB ;
192
+ pin &= 0x07 ;
193
+ }
194
+
195
+ mask = 1 << pin ;
196
+ value = readByte (node->data0, node->data1, gpio) ;
197
+
198
+ if ((value & mask) == 0)
199
+ return LOW ;
200
+ else
201
+ return HIGH ;
202
+ }
203
+
204
+
205
+ /*
206
+ * mcp23s17Setup:
207
+ * Create a new instance of an MCP23s17 SPI GPIO interface. We know it
208
+ * has 16 pins, so all we need to know here is the SPI address and the
209
+ * user-defined pin base.
210
+ *********************************************************************************
211
+ */
212
+
213
+ int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
214
+ {
215
+ struct wiringPiNodeStruct *node ;
216
+
217
+ if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0)
218
+ return FALSE ;
219
+
220
+ writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ;
221
+ writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ;
222
+
223
+ node = wiringPiNewNode (pinBase, 16) ;
224
+
225
+ node->data0 = spiPort ;
226
+ node->data1 = devId ;
227
+ node->pinMode = myPinMode ;
228
+ node->pullUpDnControl = myPullUpDnControl ;
229
+ node->digitalRead = myDigitalRead ;
230
+ node->digitalWrite = myDigitalWrite ;
231
+ node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ;
232
+ node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ;
233
+
234
+ return TRUE ;
235
+ }