examples: annotate examples
[nit.git] / contrib / wiringPi / examples / shift.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module wiringPi is example
18
19 import wiringPi
20
21 if args.is_empty or args.length > 2 then
22 print "usage:"
23 print "\tshift <register> <state>"
24 print "\tshift clear"
25 print "\tshift all"
26 exit(1)
27 end
28
29 # init wiringPi lib
30 wiringPi_setup
31
32 # register layout and pin numbering
33 var nb_pins = 8
34 var ser_pin = 7
35 var rclk_pin = 6
36 var srclk_pin = 5
37 var sr = new SR595(nb_pins, ser_pin, rclk_pin, srclk_pin)
38
39 # set all registers to 0
40 if args.first == "clear" then
41 sr.clear_registers
42 # set all registers to 1
43 else if args.first == "all" then
44 var regs = new Array[Bool].filled_with(true, nb_pins)
45 sr.clear_registers
46 sr.write_all(regs)
47 # set specified register to specified value
48 else
49 if args[1].to_i == 0 then
50 sr.write(args.first.to_i, false)
51 else
52 sr.write(args.first.to_i, true)
53 end
54 end