| 1 | # Author: Matthew Mallard | ||
| 2 | # Website: www.q-technologies.com.au | ||
| 3 | # Date: 6th October 2016 | ||
| 4 | # | ||
| 5 | # ABSTRACT: Control the GPIO pins on the original NTC Chip | ||
| 6 | |||
| 7 | |||
| 8 | package Device::NTCChip::GPIO; | ||
| 9 | use Moose; | ||
| 10 | use Moose::Exporter; | ||
| 11 | use 5.10.0; | ||
| 12 | |||
| 13 | #use Log::MixedColor; | ||
| 14 | #use MooseX::Storage; | ||
| 15 | #use Data::Dumper; | ||
| 16 | use File::Spec::Functions; | ||
| 17 | |||
| 18 | with Storage( | ||
| 19 | 'format' => 'JSON', | ||
| 20 | 'io' => 'File', | ||
| 21 | traits => ['DisableCycleDetection'] | ||
| 22 | ); | ||
| 23 | |||
| 24 | #has 'log' => ( | ||
| 25 | #is => 'rw', | ||
| 26 | #isa => 'Log::MixedColor', | ||
| 27 | #); | ||
| 28 | |||
| 29 | #has 'verbose' => ( | ||
| 30 | #is => 'rw', | ||
| 31 | #isa => 'Bool', | ||
| 32 | #default => 0, | ||
| 33 | #predicate => 'is_verbose', | ||
| 34 | #); | ||
| 35 | |||
| 36 | #has 'debug' => ( | ||
| 37 | #is => 'rw', | ||
| 38 | #isa => 'Bool', | ||
| 39 | #default => 0, | ||
| 40 | #predicate => 'is_debug', | ||
| 41 | #); | ||
| 42 | |||
| 43 | has 'label' => ( | ||
| 44 | is => 'rw', | ||
| 45 | isa => 'Str', | ||
| 46 | required => 1, | ||
| 47 | default => "pcf8574a", | ||
| 48 | predicate => 'has_label', | ||
| 49 | ); | ||
| 50 | |||
| 51 | has 'devpath' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => 'Str', | ||
| 54 | required => 1, | ||
| 55 | default => "/sys/class/gpio", | ||
| 56 | predicate => 'has_devpath', | ||
| 57 | ); | ||
| 58 | |||
| 59 | has 'gpiopath' => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => 'Str', | ||
| 62 | predicate => 'has_gpiopath', | ||
| 63 | ); | ||
| 64 | |||
| 65 | has 'baseaddr' => ( | ||
| 66 | is => 'rw', | ||
| 67 | isa => 'Int', | ||
| 68 | predicate => 'has_baseaddr', | ||
| 69 | ); | ||
| 70 | |||
| 71 | has 'numaddr' => ( | ||
| 72 | is => 'rw', | ||
| 73 | isa => 'Int', | ||
| 74 | predicate => 'has_numaddr', | ||
| 75 | ); | ||
| 76 | |||
| 77 | sub BUILD { | ||
| 78 | my $self = shift; | ||
| 79 | my $args = shift; | ||
| 80 | |||
| 81 | #$self->log( Log::MixedColor->new( $args ) ); | ||
| 82 | $self->_find_addrs(); | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | sub export_all_pins { | ||
| 87 | } | ||
| 88 | |||
| 89 | sub unexport_all_pins { | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | sub turn_on { | ||
| 94 | my $self = shift; | ||
| 95 | my $pin = shift; | ||
| 96 | say "Turning on pin $pin"; | ||
| 97 | $self->set_pin_value( $pin, 1 ); | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | sub turn_off { | ||
| 104 | my $self = shift; | ||
| 105 | my $pin = shift; | ||
| 106 | say "Turning off pin $pin"; | ||
| 107 | $self->set_pin_value( $pin, 0 ); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | sub relay_on { | ||
| 113 | |||
| 114 | # Relays reverse the logic | ||
| 115 | my $self = shift; | ||
| 116 | my $pin = shift; | ||
| 117 | say "Turning on relay $pin"; | ||
| 118 | $self->set_pin_value( $pin, 0 ); | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | sub relay_off { | ||
| 125 | |||
| 126 | # Relays reverse the logic | ||
| 127 | my $self = shift; | ||
| 128 | my $pin = shift; | ||
| 129 | say "Turning off relay $pin"; | ||
| 130 | $self->set_pin_value( $pin, 1 ); | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | sub read { | ||
| 135 | my $self = shift; | ||
| 136 | my $pin = shift; | ||
| 137 | $self->export_pin($pin); | ||
| 138 | $self->set_pin_direction( $pin, 'in' ); | ||
| 139 | #say Dumper( $self->get_pin_direction($pin) ); | ||
| 140 | #say Dumper( $self->get_pin_value($pin) ); | ||
| 141 | |||
| 142 | #$self->unexport_pin($pin); | ||
| 143 | return $self->get_pin_value($pin); | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | sub export_pin { | ||
| 148 | my $self = shift; | ||
| 149 | my $pin = shift; | ||
| 150 | my $ref = $pin + $self->baseaddr; | ||
| 151 | say "Making sure pin $pin is exported"; | ||
| 152 | |||
| 153 | # Only export pin if it is not already exported | ||
| 154 | if ( !-e "/sys/class/gpio/gpio$ref" ) { | ||
| 155 | open SYS, ">/sys/class/gpio/export" or die $!; | ||
| 156 | say SYS $ref; | ||
| 157 | close SYS; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub unexport_pin { | ||
| 162 | my $self = shift; | ||
| 163 | my $pin = shift; | ||
| 164 | my $ref = $pin + $self->baseaddr; | ||
| 165 | say "Unexporting pin $pin"; | ||
| 166 | open SYS, ">/sys/class/gpio/unexport" or die $!; | ||
| 167 | say SYS $ref; | ||
| 168 | close SYS; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub get_pin_direction { | ||
| 172 | my $self = shift; | ||
| 173 | my $pin = shift; | ||
| 174 | my $ref = $pin + $self->baseaddr; | ||
| 175 | open SYS, "</sys/class/gpio/gpio$ref/direction" or die $!; | ||
| 176 | chomp( my @direction = <SYS> ); | ||
| 177 | close SYS; | ||
| 178 | |||
| 179 | #say Dumper( \@direction ); | ||
| 180 | return $direction[0]; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub get_pin_value { | ||
| 184 | my $self = shift; | ||
| 185 | my $pin = shift; | ||
| 186 | my $ref = $pin + $self->baseaddr; | ||
| 187 | open SYS, "</sys/class/gpio/gpio$ref/value" or die $!; | ||
| 188 | chomp( my @reading = <SYS> ); | ||
| 189 | close SYS; | ||
| 190 | |||
| 191 | #say Dumper( \@reading ); | ||
| 192 | return $reading[0]; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub set_pin_direction { | ||
| 196 | my $self = shift; | ||
| 197 | my $pin = shift; | ||
| 198 | my $direction = shift; | ||
| 199 | my $ref = $pin + $self->baseaddr; | ||
| 200 | open SYS, ">/sys/class/gpio/gpio$ref/direction" or die $!; | ||
| 201 | say SYS $direction; | ||
| 202 | close SYS; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub set_pin_value { | ||
| 206 | my $self = shift; | ||
| 207 | my $pin = shift; | ||
| 208 | my $value = shift; | ||
| 209 | my $ref = $pin + $self->baseaddr; | ||
| 210 | $self->export_pin($pin); | ||
| 211 | $self->set_pin_direction( $pin, 'out' ); | ||
| 212 | open SYS, ">/sys/class/gpio/gpio$ref/value" or die $!; | ||
| 213 | say SYS $value; | ||
| 214 | close SYS; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub _find_addrs { | ||
| 218 | my $self = shift; | ||
| 219 | |||
| 220 | opendir( GD, $self->devpath ) | ||
| 221 | or die "Could not read " . $self->devpath . " " . $!; | ||
| 222 | my @contents = grep !/^\./, readdir GD; | ||
| 223 | for my $subdir (@contents) { | ||
| 224 | my $file = catfile( $self->devpath, $subdir, "label" ); | ||
| 225 | if ( -f $file ) { | ||
| 226 | open IN, "<$file" or die "Could not read " . $file . " " . $!; | ||
| 227 | chomp( my $content = join( '', <IN> ) ); | ||
| 228 | if ( $content eq $self->label ) { | ||
| 229 | $self->gpiopath( catfile( $self->devpath, $subdir ) ); | ||
| 230 | last; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | closedir GD; | ||
| 235 | |||
| 236 | if ( $self->has_gpiopath ) { | ||
| 237 | say $self->gpiopath; | ||
| 238 | $self->baseaddr( | ||
| 239 | $self->_get_num_from_file( catfile( $self->gpiopath, "base" ) ) ); | ||
| 240 | $self->numaddr( | ||
| 241 | $self->_get_num_from_file( catfile( $self->gpiopath, "ngpio" ) ) ); | ||
| 242 | } | ||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | sub _get_num_from_file { | ||
| 247 | my $self = shift; | ||
| 248 | my $file = shift; | ||
| 249 | |||
| 250 | if ( -f $file ) { | ||
| 251 | open B, "<$file" or die "Could not read " . $file . " " . $!; | ||
| 252 | chomp( my $content = join( '', <B> ) ); | ||
| 253 | $content =~ s/\s+//g; | ||
| 254 | if ( $content =~ /^(\d+)$/ ) { | ||
| 255 | return $1; # untainted | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | die "Unexpected error: $file has unexpected contents"; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | else { | ||
| 262 | die "Unexpected error: $file does not exist"; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | 1; | ||
| 268 | |||
| 269 | __END__ |