updates for splitting up assets and code etc
[swamp-bikeopera:code.git] / src / swamp
1 #!/usr/bin/lua
2
3 -- Swamp Bike Opera embedded system for Kaffe Matthews 
4 -- Copyright (C) 2012 Wolfgang Hauptfleisch, Dave Griffiths
5 --
6 -- This program is free software: you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as published by
8 -- the Free Software Foundation, either version 3 of the License, or
9 -- (at your option) any later version.
10 --
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 -- GNU General Public License for more details.
15 --
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 package.path = package.path..";./?.lua;../lib/?.lua"
20
21 --require 'swamp'
22 require 'gps'
23 require 'posix'
24 require 'poly'
25 require 'map'
26 require 'direction'
27 require 'random'
28 require 'audioc'
29 require 'socket'
30
31 dofile '../config/swamp.conf'
32 dofile '../config/random.conf'
33
34 os.execute("touch /tmp/fake_gps > /dev/null")
35
36 function gpslog(lat, lon)
37    local  file = io.open(CONFIG.install_path.."log/gps.log", "a")
38    local logmessage = os.date().." "..lat.." "..lon.."\n"
39    file:write(logmessage)
40    file:close()
41 end
42
43 function swamplog(logmessage)
44    local  file = io.open(CONFIG.install_path.."log/swamp.log", "a")
45    file:write(os.date().." "..logmessage.."\n")
46    file:close()
47    if CONFIG.debug then
48      io.write(os.date().." "..logmessage.."\n")
49    end
50
51 end
52
53
54 -- load the mapfile
55 ZONES = map.load()
56
57 swamplog("mapfile loaded sucessfully")
58
59 -- bootstrap the system
60 -- start the player
61 os.execute("./swamp_playd &") --  > /dev/null &")
62
63 swamplog("swamp_playd started")
64
65 print("waiting for playd")
66 posix.sleep(10)
67
68 -- start the gps daemon
69 os.execute("./swamp_gpsd &")
70
71 swamplog("swamp_gpsd started")
72 print("swamp_gpsd started")
73
74 -- we give the gps some time to catch up
75 posix.sleep(4)
76
77 --- connect to the gps pipe
78 local file = io.open("/tmp/swamp_gps","r")
79 --- this is the main loop
80
81 local last_lat, last_lon
82 local last_time = os.time()
83
84 local last_zone = 0
85
86 print("STARTING LOOP")
87
88 -- play a test sound
89 audioc.play(1)
90
91 while true do
92     math.randomseed(socket.gettime())
93     local ani = random.animal()
94     local chan = random.channel()                      
95
96     local line = file:read()
97         if line then
98
99     ----- random events
100
101     if random.by_probability(2) then
102        print("choosing ..")
103            print("PLAYING RANDOM: "..ani.." "..chan)
104            audioc.stop(ani)
105            audioc.play(ani, chan)
106     end
107
108
109
110           --- THIS IS JUST A FAKE FOR LOCAL TESTING
111           if CONFIG.fake_gps then
112             local fakezone = gps.fake_zone()
113               if fakezone ~= 0 then
114                if fakezone ~= last_zone then
115                       audioc.stop(last_zone)
116                print("NEW FAKEZONE: "..fakezone)
117                audioc.play(fakezone)
118                last_zone = fakezone
119                end
120             end
121           end
122
123
124            local lat, lon = string.match(line, "^(.-)%s(.-)$") 
125            if tonumber(lat) and tonumber(lon) then
126               gpslog(lat, lon)
127
128               if not last_lat then
129                  last_lat = lat
130                  last_lon = lon
131               end
132
133               -- caculate where we are              
134               for i, polygon in ipairs(ZONES) do
135                   location = poly.is_in_polygon(polygon, lat , lon)
136                   if location then
137                      --- trigger static sound
138                      swamplog("INSIDE "..i)
139
140              
141                      if i ~= last_zone then 
142                         audioc.stop(last_zone)
143                         swamplog("stopping "..last_zone)
144                         swamplog("playing "..i)
145                         audioc.play(i)
146                         last_zone = i
147                      end
148
149                      local area_centre_lat , area_centre_lon = poly.centroid(ZONES[i])
150                      swamplog("distance from centre: "..std.round(poly.distance(area_centre_lat , area_centre_lon, tonumber(lat), tonumber(lon)), 7)) 
151                   end
152               end
153                
154
155                -- calculate distance since last time
156                local time_diff = os.time() - last_time
157  
158                if time_diff > CONFIG.direction_track_time then
159                   -- print("time", last_lat, last_lon)
160                   last_time = os.time()
161                if tonumber(last_lat) and tonumber(last_lon) then
162                   swamplog("In last 10 seconds: "..std.round(poly.distance(tonumber(lat), tonumber(lon), tonumber(last_lat), tonumber(last_lon)), 7))
163                   local direction_resolved = direction.resolve(poly.direction(tonumber(lat), tonumber(lon), tonumber(last_lat), tonumber(last_lon)))
164                   --print(direction_resolved) 
165                   swamplog("direction: "..direction_resolved)
166                   last_lat = lat
167                   last_lon = lon
168                end
169                end
170
171            end
172
173
174            posix.sleep(1)
175         end
176 end
177
178