wiringpi-ruby 2.0.0 → 2.1.3

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 (168) 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. data/ext/wiringpi/wiringpi_wrap.c +636 -219
  157. data/lib/wiringpi.rb +25 -12
  158. data/lib/wiringpi/gpio.rb +90 -83
  159. data/lib/wiringpi/i2c.rb +29 -22
  160. data/lib/wiringpi/modules/mcp23017.rb +15 -0
  161. data/lib/wiringpi/modules/mcp23s17.rb +16 -0
  162. data/lib/wiringpi/scrollphat.rb +14 -14
  163. data/lib/wiringpi/serial.rb +8 -18
  164. data/lib/wiringpi/spi.rb +14 -11
  165. data/lib/wiringpi/watch.rb +24 -0
  166. metadata +160 -4
  167. data/lib/wiringpi/event.rb +0 -21
  168. data/lib/wiringpi/mcp23x17.rb +0 -31
@@ -0,0 +1,42 @@
1
+ /*
2
+ * wiringPiI2C.h:
3
+ * Simplified I2C access routines
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 wiringPiI2CRead (int fd) ;
30
+ extern int wiringPiI2CReadReg8 (int fd, int reg) ;
31
+ extern int wiringPiI2CReadReg16 (int fd, int reg) ;
32
+
33
+ extern int wiringPiI2CWrite (int fd, int data) ;
34
+ extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ;
35
+ extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ;
36
+
37
+ extern int wiringPiI2CSetupInterface (const char *device, int devId) ;
38
+ extern int wiringPiI2CSetup (const int devId) ;
39
+
40
+ #ifdef __cplusplus
41
+ }
42
+ #endif
@@ -0,0 +1,144 @@
1
+ /*
2
+ * wiringPiSPI.c:
3
+ * Simplified SPI access routines
4
+ * Copyright (c) 2012-2015 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
+
26
+ #include <stdio.h>
27
+ #include <stdint.h>
28
+ #include <stdlib.h>
29
+ #include <fcntl.h>
30
+ #include <errno.h>
31
+ #include <string.h>
32
+ #include <sys/ioctl.h>
33
+ #include <asm/ioctl.h>
34
+ #include <linux/spi/spidev.h>
35
+
36
+ #include "wiringPi.h"
37
+
38
+ #include "wiringPiSPI.h"
39
+
40
+
41
+ // The SPI bus parameters
42
+ // Variables as they need to be passed as pointers later on
43
+
44
+ //static const char *spiDev0 = "/dev/spidev0.0" ;
45
+ //static const char *spiDev1 = "/dev/spidev0.1" ;
46
+ static const uint8_t spiBPW = 8 ;
47
+ static const uint16_t spiDelay = 0 ;
48
+
49
+ static uint32_t spiSpeeds [2] ;
50
+ static int spiFds [2] ;
51
+
52
+
53
+ /*
54
+ * wiringPiSPIGetFd:
55
+ * Return the file-descriptor for the given channel
56
+ *********************************************************************************
57
+ */
58
+
59
+ int wiringPiSPIGetFd (int channel)
60
+ {
61
+ return spiFds [channel & 1] ;
62
+ }
63
+
64
+
65
+ /*
66
+ * wiringPiSPIDataRW:
67
+ * Write and Read a block of data over the SPI bus.
68
+ * Note the data ia being read into the transmit buffer, so will
69
+ * overwrite it!
70
+ * This is also a full-duplex operation.
71
+ *********************************************************************************
72
+ */
73
+
74
+ int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
75
+ {
76
+ struct spi_ioc_transfer spi ;
77
+
78
+ channel &= 1 ;
79
+
80
+ // Mentioned in spidev.h but not used in the original kernel documentation
81
+ // test program )-:
82
+
83
+ memset (&spi, 0, sizeof (spi)) ;
84
+
85
+ spi.tx_buf = (unsigned long)data ;
86
+ spi.rx_buf = (unsigned long)data ;
87
+ spi.len = len ;
88
+ spi.delay_usecs = spiDelay ;
89
+ spi.speed_hz = spiSpeeds [channel] ;
90
+ spi.bits_per_word = spiBPW ;
91
+
92
+ return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
93
+ }
94
+
95
+
96
+ /*
97
+ * wiringPiSPISetupMode:
98
+ * Open the SPI device, and set it up, with the mode, etc.
99
+ *********************************************************************************
100
+ */
101
+
102
+ int wiringPiSPISetupMode (int channel, int speed, int mode)
103
+ {
104
+ int fd ;
105
+ char spiDev [32] ;
106
+
107
+ mode &= 3 ; // Mode is 0, 1, 2 or 3
108
+
109
+ // Channel can be anything - lets hope for the best
110
+ // channel &= 1 ; // Channel is 0 or 1
111
+
112
+ snprintf (spiDev, 31, "/dev/spidev0.%d", channel) ;
113
+
114
+ if ((fd = open (spiDev, O_RDWR)) < 0)
115
+ return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
116
+
117
+ spiSpeeds [channel] = speed ;
118
+ spiFds [channel] = fd ;
119
+
120
+ // Set SPI parameters.
121
+
122
+ if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)
123
+ return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
124
+
125
+ if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
126
+ return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
127
+
128
+ if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
129
+ return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
130
+
131
+ return fd ;
132
+ }
133
+
134
+
135
+ /*
136
+ * wiringPiSPISetup:
137
+ * Open the SPI device, and set it up, etc. in the default MODE 0
138
+ *********************************************************************************
139
+ */
140
+
141
+ int wiringPiSPISetup (int channel, int speed)
142
+ {
143
+ return wiringPiSPISetupMode (channel, speed, 0) ;
144
+ }
@@ -0,0 +1,36 @@
1
+ /*
2
+ * wiringPiSPI.h:
3
+ * Simplified SPI access routines
4
+ * Copyright (c) 2012-2015 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
+ int wiringPiSPIGetFd (int channel) ;
30
+ int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
31
+ int wiringPiSPISetupMode (int channel, int speed, int mode) ;
32
+ int wiringPiSPISetup (int channel, int speed) ;
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
@@ -0,0 +1,225 @@
1
+ /*
2
+ * wiringSerial.c:
3
+ * Handle a serial port
4
+ ***********************************************************************
5
+ * This file is part of wiringPi:
6
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
7
+ *
8
+ * wiringPi is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU Lesser General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * wiringPi is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public License
19
+ * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
20
+ ***********************************************************************
21
+ */
22
+
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+ #include <stdint.h>
26
+ #include <stdarg.h>
27
+ #include <string.h>
28
+ #include <termios.h>
29
+ #include <unistd.h>
30
+ #include <fcntl.h>
31
+ #include <sys/ioctl.h>
32
+ #include <sys/types.h>
33
+ #include <sys/stat.h>
34
+
35
+ #include "wiringSerial.h"
36
+
37
+ /*
38
+ * serialOpen:
39
+ * Open and initialise the serial port, setting all the right
40
+ * port parameters - or as many as are required - hopefully!
41
+ *********************************************************************************
42
+ */
43
+
44
+ int serialOpen (const char *device, const int baud)
45
+ {
46
+ struct termios options ;
47
+ speed_t myBaud ;
48
+ int status, fd ;
49
+
50
+ switch (baud)
51
+ {
52
+ case 50: myBaud = B50 ; break ;
53
+ case 75: myBaud = B75 ; break ;
54
+ case 110: myBaud = B110 ; break ;
55
+ case 134: myBaud = B134 ; break ;
56
+ case 150: myBaud = B150 ; break ;
57
+ case 200: myBaud = B200 ; break ;
58
+ case 300: myBaud = B300 ; break ;
59
+ case 600: myBaud = B600 ; break ;
60
+ case 1200: myBaud = B1200 ; break ;
61
+ case 1800: myBaud = B1800 ; break ;
62
+ case 2400: myBaud = B2400 ; break ;
63
+ case 4800: myBaud = B4800 ; break ;
64
+ case 9600: myBaud = B9600 ; break ;
65
+ case 19200: myBaud = B19200 ; break ;
66
+ case 38400: myBaud = B38400 ; break ;
67
+ case 57600: myBaud = B57600 ; break ;
68
+ case 115200: myBaud = B115200 ; break ;
69
+ case 230400: myBaud = B230400 ; break ;
70
+ case 460800: myBaud = B460800 ; break ;
71
+ case 500000: myBaud = B500000 ; break ;
72
+ case 576000: myBaud = B576000 ; break ;
73
+ case 921600: myBaud = B921600 ; break ;
74
+ case 1000000: myBaud = B1000000 ; break ;
75
+ case 1152000: myBaud = B1152000 ; break ;
76
+ case 1500000: myBaud = B1500000 ; break ;
77
+ case 2000000: myBaud = B2000000 ; break ;
78
+ case 2500000: myBaud = B2500000 ; break ;
79
+ case 3000000: myBaud = B3000000 ; break ;
80
+ case 3500000: myBaud = B3500000 ; break ;
81
+ case 4000000: myBaud = B4000000 ; break ;
82
+
83
+ default:
84
+ return -2 ;
85
+ }
86
+
87
+ if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
88
+ return -1 ;
89
+
90
+ fcntl (fd, F_SETFL, O_RDWR) ;
91
+
92
+ // Get and modify current options:
93
+
94
+ tcgetattr (fd, &options) ;
95
+
96
+ cfmakeraw (&options) ;
97
+ cfsetispeed (&options, myBaud) ;
98
+ cfsetospeed (&options, myBaud) ;
99
+
100
+ options.c_cflag |= (CLOCAL | CREAD) ;
101
+ options.c_cflag &= ~PARENB ;
102
+ options.c_cflag &= ~CSTOPB ;
103
+ options.c_cflag &= ~CSIZE ;
104
+ options.c_cflag |= CS8 ;
105
+ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
106
+ options.c_oflag &= ~OPOST ;
107
+
108
+ options.c_cc [VMIN] = 0 ;
109
+ options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
110
+
111
+ tcsetattr (fd, TCSANOW, &options) ;
112
+
113
+ ioctl (fd, TIOCMGET, &status);
114
+
115
+ status |= TIOCM_DTR ;
116
+ status |= TIOCM_RTS ;
117
+
118
+ ioctl (fd, TIOCMSET, &status);
119
+
120
+ usleep (10000) ; // 10mS
121
+
122
+ return fd ;
123
+ }
124
+
125
+
126
+ /*
127
+ * serialFlush:
128
+ * Flush the serial buffers (both tx & rx)
129
+ *********************************************************************************
130
+ */
131
+
132
+ void serialFlush (const int fd)
133
+ {
134
+ tcflush (fd, TCIOFLUSH) ;
135
+ }
136
+
137
+
138
+ /*
139
+ * serialClose:
140
+ * Release the serial port
141
+ *********************************************************************************
142
+ */
143
+
144
+ void serialClose (const int fd)
145
+ {
146
+ close (fd) ;
147
+ }
148
+
149
+
150
+ /*
151
+ * serialPutchar:
152
+ * Send a single character to the serial port
153
+ *********************************************************************************
154
+ */
155
+
156
+ void serialPutchar (const int fd, const unsigned char c)
157
+ {
158
+ write (fd, &c, 1) ;
159
+ }
160
+
161
+
162
+ /*
163
+ * serialPuts:
164
+ * Send a string to the serial port
165
+ *********************************************************************************
166
+ */
167
+
168
+ void serialPuts (const int fd, const char *s)
169
+ {
170
+ write (fd, s, strlen (s)) ;
171
+ }
172
+
173
+ /*
174
+ * serialPrintf:
175
+ * Printf over Serial
176
+ *********************************************************************************
177
+ */
178
+
179
+ void serialPrintf (const int fd, const char *message, ...)
180
+ {
181
+ va_list argp ;
182
+ char buffer [1024] ;
183
+
184
+ va_start (argp, message) ;
185
+ vsnprintf (buffer, 1023, message, argp) ;
186
+ va_end (argp) ;
187
+
188
+ serialPuts (fd, buffer) ;
189
+ }
190
+
191
+
192
+ /*
193
+ * serialDataAvail:
194
+ * Return the number of bytes of data avalable to be read in the serial port
195
+ *********************************************************************************
196
+ */
197
+
198
+ int serialDataAvail (const int fd)
199
+ {
200
+ int result ;
201
+
202
+ if (ioctl (fd, FIONREAD, &result) == -1)
203
+ return -1 ;
204
+
205
+ return result ;
206
+ }
207
+
208
+
209
+ /*
210
+ * serialGetchar:
211
+ * Get a single character from the serial device.
212
+ * Note: Zero is a valid character and this function will time-out after
213
+ * 10 seconds.
214
+ *********************************************************************************
215
+ */
216
+
217
+ int serialGetchar (const int fd)
218
+ {
219
+ uint8_t x ;
220
+
221
+ if (read (fd, &x, 1) != 1)
222
+ return -1 ;
223
+
224
+ return ((int)x) & 0xFF ;
225
+ }