|
| 1 | +#============================================================================= |
| 2 | + GPIO Wrapper using sysfs |
| 3 | +
|
| 4 | + Provides Julia interface for GPIO control via /sys/class/gpio/ |
| 5 | + No external library dependencies - pure file I/O. |
| 6 | +=============================================================================# |
| 7 | + |
| 8 | +module GPIO |
| 9 | + |
| 10 | +export GPIOPin, INPUT, OUTPUT |
| 11 | +export export_pin, unexport_pin, set_direction, set_value, get_value |
| 12 | + |
| 13 | +const GPIO_SYSFS_PATH = "/sys/class/gpio" |
| 14 | + |
| 15 | +# Line direction |
| 16 | +@enum LineDirection begin |
| 17 | + INPUT |
| 18 | + OUTPUT |
| 19 | +end |
| 20 | + |
| 21 | +#============================================================================= |
| 22 | + GPIO Pin Handle |
| 23 | +=============================================================================# |
| 24 | + |
| 25 | +mutable struct GPIOPin |
| 26 | + pin::Int |
| 27 | + path::String |
| 28 | + exported::Bool |
| 29 | + direction::LineDirection |
| 30 | +end |
| 31 | + |
| 32 | +#============================================================================= |
| 33 | + Low-level sysfs Operations |
| 34 | +=============================================================================# |
| 35 | + |
| 36 | +""" |
| 37 | + sysfs_write(path::String, value::String) |
| 38 | +
|
| 39 | +Write a value to a sysfs file. |
| 40 | +""" |
| 41 | +function sysfs_write(path::String, value::String) |
| 42 | + f = open(path, "w") |
| 43 | + try |
| 44 | + write(f, value) |
| 45 | + finally |
| 46 | + close(f) |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +""" |
| 51 | + sysfs_read(path::String) -> String |
| 52 | +
|
| 53 | +Read a value from a sysfs file. |
| 54 | +""" |
| 55 | +function sysfs_read(path::String) |
| 56 | + f = open(path, "r") |
| 57 | + try |
| 58 | + return strip(read(f, String)) |
| 59 | + finally |
| 60 | + close(f) |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +#============================================================================= |
| 65 | + Pin Export/Unexport |
| 66 | +=============================================================================# |
| 67 | + |
| 68 | +""" |
| 69 | + export_pin(pin::Int) -> GPIOPin |
| 70 | +
|
| 71 | +Export a GPIO pin for userspace control. |
| 72 | +""" |
| 73 | +function export_pin(pin::Int) |
| 74 | + pin_path = joinpath(GPIO_SYSFS_PATH, "gpio$pin") |
| 75 | + |
| 76 | + # Export if not already exported |
| 77 | + if !isdir(pin_path) |
| 78 | + export_file = joinpath(GPIO_SYSFS_PATH, "export") |
| 79 | + sysfs_write(export_file, string(pin)) |
| 80 | + # Wait for sysfs to create the directory |
| 81 | + sleep(0.05) |
| 82 | + end |
| 83 | + |
| 84 | + if !isdir(pin_path) |
| 85 | + error("Failed to export GPIO pin $pin") |
| 86 | + end |
| 87 | + |
| 88 | + return GPIOPin(pin, pin_path, true, INPUT) |
| 89 | +end |
| 90 | + |
| 91 | +""" |
| 92 | + unexport_pin(gpio::GPIOPin) |
| 93 | +
|
| 94 | +Unexport a GPIO pin. |
| 95 | +""" |
| 96 | +function unexport_pin(gpio::GPIOPin) |
| 97 | + if gpio.exported |
| 98 | + unexport_file = joinpath(GPIO_SYSFS_PATH, "unexport") |
| 99 | + sysfs_write(unexport_file, string(gpio.pin)) |
| 100 | + gpio.exported = false |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +#============================================================================= |
| 105 | + Direction Control |
| 106 | +=============================================================================# |
| 107 | + |
| 108 | +""" |
| 109 | + set_direction(gpio::GPIOPin, direction::LineDirection) |
| 110 | +
|
| 111 | +Set the GPIO pin direction (INPUT or OUTPUT). |
| 112 | +""" |
| 113 | +function set_direction(gpio::GPIOPin, direction::LineDirection) |
| 114 | + direction_file = joinpath(gpio.path, "direction") |
| 115 | + dir_str = direction == OUTPUT ? "out" : "in" |
| 116 | + sysfs_write(direction_file, dir_str) |
| 117 | + gpio.direction = direction |
| 118 | +end |
| 119 | + |
| 120 | +#============================================================================= |
| 121 | + Value Read/Write |
| 122 | +=============================================================================# |
| 123 | + |
| 124 | +""" |
| 125 | + set_value(gpio::GPIOPin, value::Int) |
| 126 | +
|
| 127 | +Set the value of an output pin (0 or 1). |
| 128 | +""" |
| 129 | +function set_value(gpio::GPIOPin, value::Int) |
| 130 | + value_file = joinpath(gpio.path, "value") |
| 131 | + sysfs_write(value_file, value == 0 ? "0" : "1") |
| 132 | +end |
| 133 | + |
| 134 | +""" |
| 135 | + get_value(gpio::GPIOPin) -> Int |
| 136 | +
|
| 137 | +Get the value of a pin (0 or 1). |
| 138 | +""" |
| 139 | +function get_value(gpio::GPIOPin) |
| 140 | + value_file = joinpath(gpio.path, "value") |
| 141 | + val_str = sysfs_read(value_file) |
| 142 | + return parse(Int, val_str) |
| 143 | +end |
| 144 | + |
| 145 | +#============================================================================= |
| 146 | + Convenience Functions (compatible with previous API) |
| 147 | +=============================================================================# |
| 148 | + |
| 149 | +# Compatibility types |
| 150 | +const GPIOChip = Nothing |
| 151 | +const GPIOLine = GPIOPin |
| 152 | + |
| 153 | +""" |
| 154 | + open_chip(path::String="/dev/gpiochip0") -> Nothing |
| 155 | +
|
| 156 | +Dummy function for API compatibility. sysfs doesn't use chip handles. |
| 157 | +""" |
| 158 | +function open_chip(path::String="/dev/gpiochip0") |
| 159 | + return nothing |
| 160 | +end |
| 161 | + |
| 162 | +""" |
| 163 | + close_chip(chip::Nothing) |
| 164 | +
|
| 165 | +Dummy function for API compatibility. |
| 166 | +""" |
| 167 | +function close_chip(chip::Nothing) |
| 168 | + return nothing |
| 169 | +end |
| 170 | + |
| 171 | +""" |
| 172 | + get_line(chip::Nothing, pin::Int) -> GPIOPin |
| 173 | +
|
| 174 | +Export and return a GPIO pin. Chip argument ignored (sysfs doesn't use it). |
| 175 | +""" |
| 176 | +function get_line(chip::Nothing, pin::Int) |
| 177 | + return export_pin(pin) |
| 178 | +end |
| 179 | + |
| 180 | +""" |
| 181 | + request_output(gpio::GPIOPin, consumer::String, default_value::Int=0) |
| 182 | +
|
| 183 | +Configure pin as output with initial value. |
| 184 | +""" |
| 185 | +function request_output(gpio::GPIOPin, consumer::String, default_value::Int=0) |
| 186 | + set_direction(gpio, OUTPUT) |
| 187 | + set_value(gpio, default_value) |
| 188 | + return 0 |
| 189 | +end |
| 190 | + |
| 191 | +""" |
| 192 | + request_input(gpio::GPIOPin, consumer::String) |
| 193 | +
|
| 194 | +Configure pin as input. |
| 195 | +""" |
| 196 | +function request_input(gpio::GPIOPin, consumer::String) |
| 197 | + set_direction(gpio, INPUT) |
| 198 | + return 0 |
| 199 | +end |
| 200 | + |
| 201 | +""" |
| 202 | + release_line(gpio::GPIOPin) |
| 203 | +
|
| 204 | +Unexport the GPIO pin. |
| 205 | +""" |
| 206 | +function release_line(gpio::GPIOPin) |
| 207 | + unexport_pin(gpio) |
| 208 | +end |
| 209 | + |
| 210 | +""" |
| 211 | + digitalWrite(gpio::GPIOPin, value::Bool) |
| 212 | +
|
| 213 | +Set a GPIO output pin high (true) or low (false). |
| 214 | +""" |
| 215 | +function digitalWrite(gpio::GPIOPin, value::Bool) |
| 216 | + set_value(gpio, value ? 1 : 0) |
| 217 | +end |
| 218 | + |
| 219 | +""" |
| 220 | + digitalRead(gpio::GPIOPin) -> Bool |
| 221 | +
|
| 222 | +Read a GPIO input pin state. |
| 223 | +""" |
| 224 | +function digitalRead(gpio::GPIOPin) |
| 225 | + return get_value(gpio) == 1 |
| 226 | +end |
| 227 | + |
| 228 | +end # module GPIO |
0 commit comments