traffic_light 0.1.0

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.
@@ -0,0 +1,80 @@
1
+ // Define LED outputs
2
+ const unsigned int LED_RED_PIN = 11;
3
+ const unsigned int LED_ORANGE_PIN = 12;
4
+ const unsigned int LED_GREEN_PIN = 10;
5
+
6
+ // Define BAUD_RATE
7
+ const unsigned int BAUD_RATE = 9600;
8
+
9
+ // Define working variables
10
+ char c;
11
+ int red = 0;
12
+ int orange = 0;
13
+ int green = 0;
14
+ String result;
15
+
16
+ void setup() {
17
+ // initialize serial:
18
+ Serial.begin(9600);
19
+
20
+ // make the pins outputs:
21
+ pinMode(LED_RED_PIN, OUTPUT);
22
+ pinMode(LED_ORANGE_PIN, OUTPUT);
23
+ pinMode(LED_GREEN_PIN, OUTPUT);
24
+
25
+ // setup default state:
26
+ digitalWrite(LED_RED_PIN, LOW);
27
+ digitalWrite(LED_ORANGE_PIN, LOW);
28
+ digitalWrite(LED_GREEN_PIN, LOW);
29
+ }
30
+
31
+ void loop() {
32
+ // if there's any serial available, read it:
33
+ while (Serial.available()) {
34
+ if (Serial.available() > 0) {
35
+ result = "";
36
+ c = Serial.read();
37
+
38
+ Serial.print(String(c));
39
+ result = result + " -> ";
40
+
41
+ // Green
42
+ if(c & 1) {
43
+ green = 1;
44
+ result = result + "G";
45
+ }
46
+ else {
47
+ green = 0;
48
+ result = result + "-";
49
+ }
50
+
51
+ // Orange
52
+ if(c & 2) {
53
+ orange = 1;
54
+ result = result + "O";
55
+ }
56
+ else {
57
+ orange = 0;
58
+ result = result + "-";
59
+ }
60
+
61
+ // Red
62
+ if(c & 4) {
63
+ red = 1;
64
+ result = result + "R";
65
+ }
66
+ else {
67
+ red = 0;
68
+ result = result + "-";
69
+ }
70
+
71
+ // Put right colors on LEDs
72
+ digitalWrite(LED_RED_PIN , red );
73
+ digitalWrite(LED_ORANGE_PIN, orange);
74
+ digitalWrite(LED_GREEN_PIN , green );
75
+
76
+ // And show outputs
77
+ Serial.println(result);
78
+ }
79
+ }
80
+ }
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ Arduino/traffic_light/traffic_light.ino
2
+ Guardfile
3
+ README.rdoc
4
+ Rakefile
5
+ lib/traffic_light.rb
6
+ spec/spec_helper.rb
7
+ spec/traffic_light_spec.rb
8
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,83 @@
1
+ = Traffic Light
2
+
3
+ Ruby gem to pilot a traffic light build with an Arduino.
4
+
5
+ == Install
6
+
7
+ gem install traffic_light
8
+
9
+ Load Arduino/traffic_light/traffic_light.ino into your Arduino.
10
+
11
+ Plug # color -> on pin #
12
+ Red -> 11
13
+ Orange -> 12
14
+ Green -> 10
15
+
16
+ == Usage
17
+
18
+ require "traffic_light"
19
+
20
+ sp = TrafficLight.new
21
+ sp.green
22
+ sp.orange
23
+ sp.red
24
+
25
+ == License
26
+
27
+ Copyright (c) 2012 Nicolas Ledez
28
+
29
+ You can redistribute it and/or modify it under either the terms of the
30
+ 2-clause BSDL (see the file BSDL), or the conditions below:
31
+
32
+ 1. You may make and give away verbatim copies of the source form of the
33
+ software without restriction, provided that you duplicate all of the
34
+ original copyright notices and associated disclaimers.
35
+
36
+ 2. You may modify your copy of the software in any way, provided that
37
+ you do at least ONE of the following:
38
+
39
+ a) place your modifications in the Public Domain or otherwise
40
+ make them Freely Available, such as by posting said
41
+ modifications to Usenet or an equivalent medium, or by allowing
42
+ the author to include your modifications in the software.
43
+
44
+ b) use the modified software only within your corporation or
45
+ organization.
46
+
47
+ c) give non-standard binaries non-standard names, with
48
+ instructions on where to get the original software distribution.
49
+
50
+ d) make other distribution arrangements with the author.
51
+
52
+ 3. You may distribute the software in object code or binary form,
53
+ provided that you do at least ONE of the following:
54
+
55
+ a) distribute the binaries and library files of the software,
56
+ together with instructions (in the manual page or equivalent)
57
+ on where to get the original distribution.
58
+
59
+ b) accompany the distribution with the machine-readable source of
60
+ the software.
61
+
62
+ c) give non-standard binaries non-standard names, with
63
+ instructions on where to get the original software distribution.
64
+
65
+ d) make other distribution arrangements with the author.
66
+
67
+ 4. You may modify and include the part of the software into any other
68
+ software (possibly commercial). But some files in the distribution
69
+ are not written by the author, so that they are not under these terms.
70
+
71
+ For the list of those files and their copying conditions, see the
72
+ file LEGAL.
73
+
74
+ 5. The scripts and library files supplied as input to or produced as
75
+ output from the software do not automatically fall under the
76
+ copyright of the software, but belong to whomever generated them,
77
+ and may be sold commercially, and may be aggregated with this
78
+ software.
79
+
80
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
81
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
82
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
83
+ PURPOSE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('traffic_light', '0.1.0') do |p|
6
+ p.description = "Drive a traffic light with an Arduino."
7
+ p.url = "http://github.com/nledez/traffic_light"
8
+ p.author = "Nicolas Ledez"
9
+ p.email = "rubygems@ledez.net"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,60 @@
1
+ require "serialport"
2
+
3
+ class TrafficLight
4
+ attr_reader :message
5
+
6
+ def initialize(port="/dev/tty.usbmodemfd121")
7
+ @port_str = port
8
+ @baud_rate = 9600
9
+ @data_bits = 8
10
+ @stop_bits = 1
11
+ @parity = SerialPort::NONE
12
+
13
+ @sp = SerialPort.new(@port_str, @baud_rate, @data_bits, @stop_bits, @parity)
14
+ @sp.read_timeout = 1000
15
+
16
+ clear
17
+ end
18
+
19
+ def read
20
+ @message = @sp.read.chomp
21
+ end
22
+
23
+ def setColors(colors)
24
+ read
25
+ @sp.write colors
26
+ read
27
+ end
28
+
29
+ def clear
30
+ setColors(0)
31
+ end
32
+
33
+ def green
34
+ setColors(1)
35
+ end
36
+
37
+ def orange
38
+ setColors(2)
39
+ end
40
+
41
+ def green_orange
42
+ setColors(3)
43
+ end
44
+
45
+ def red
46
+ setColors(4)
47
+ end
48
+
49
+ def green_red
50
+ setColors(5)
51
+ end
52
+
53
+ def orange_red
54
+ setColors(6)
55
+ end
56
+
57
+ def all
58
+ setColors(7)
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+ end
@@ -0,0 +1,47 @@
1
+ require "traffic_light"
2
+
3
+ describe "Can change light colors" do
4
+ before(:all) do
5
+ @traffic = TrafficLight.new
6
+ end
7
+
8
+ it "Set off" do
9
+ @traffic.clear
10
+ @traffic.message.should == "0 -> ---"
11
+ end
12
+
13
+ it "Set green" do
14
+ @traffic.green
15
+ @traffic.message.should == "1 -> G--"
16
+ end
17
+
18
+ it "Set orange" do
19
+ @traffic.orange
20
+ @traffic.message.should == "2 -> -O-"
21
+ end
22
+
23
+ it "Set red" do
24
+ @traffic.red
25
+ @traffic.message.should == "4 -> --R"
26
+ end
27
+
28
+ it "Set green & orange" do
29
+ @traffic.green_orange
30
+ @traffic.message.should == "3 -> GO-"
31
+ end
32
+
33
+ it "Set green & red" do
34
+ @traffic.green_red
35
+ @traffic.message.should == "5 -> G-R"
36
+ end
37
+
38
+ it "Set orange & red" do
39
+ @traffic.orange_red
40
+ @traffic.message.should == "6 -> -OR"
41
+ end
42
+
43
+ it "Set all light" do
44
+ @traffic.all
45
+ @traffic.message.should == "7 -> GOR"
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "traffic_light"
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Nicolas Ledez"]
9
+ s.date = "2012-07-28"
10
+ s.description = "Drive a traffic light with an Arduino."
11
+ s.email = "rubygems@ledez.net"
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/traffic_light.rb"]
13
+ s.files = ["Arduino/traffic_light/traffic_light.ino", "Guardfile", "README.rdoc", "Rakefile", "lib/traffic_light.rb", "spec/spec_helper.rb", "spec/traffic_light_spec.rb", "Manifest", "traffic_light.gemspec"]
14
+ s.homepage = "http://github.com/nledez/traffic_light"
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Traffic_light", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = "traffic_light"
18
+ s.rubygems_version = "1.8.23"
19
+ s.summary = "Drive a traffic light with an Arduino."
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traffic_light
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicolas Ledez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Drive a traffic light with an Arduino.
15
+ email: rubygems@ledez.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ - lib/traffic_light.rb
21
+ files:
22
+ - Arduino/traffic_light/traffic_light.ino
23
+ - Guardfile
24
+ - README.rdoc
25
+ - Rakefile
26
+ - lib/traffic_light.rb
27
+ - spec/spec_helper.rb
28
+ - spec/traffic_light_spec.rb
29
+ - Manifest
30
+ - traffic_light.gemspec
31
+ homepage: http://github.com/nledez/traffic_light
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --line-numbers
36
+ - --inline-source
37
+ - --title
38
+ - Traffic_light
39
+ - --main
40
+ - README.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ requirements: []
56
+ rubyforge_project: traffic_light
57
+ rubygems_version: 1.8.23
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Drive a traffic light with an Arduino.
61
+ test_files: []