-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsmileometer.rb
More file actions
138 lines (117 loc) · 2.91 KB
/
smileometer.rb
File metadata and controls
138 lines (117 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
class Smiles < Array
MINUTES = 60
HOURS = 60*MINUTES
RECENT = 10*MINUTES
LOG_FILE = File.expand_path('~/Documents/smileometer.csv')
attr_accessor :start
def initialize(*args)
super
@start = now
end
def dump
log_exists = File.exists?(LOG_FILE)
File.open(LOG_FILE, 'a') do |f|
f << "time,smiles_per_hour\n" unless log_exists
f << [Time.at(last), per_hour.round(2)].join(',') << "\n"
end
end
def now
Time.now.to_f
end
def elapsed
count < 1 ? 0 : now - start
end
def log
push now
dump
end
def in_last(seconds)
smiles = self.class.new select { |time| (now - time) < seconds }
smiles.start = now - seconds
smiles
end
def per_hour
if elapsed > RECENT
in_last(RECENT).per_hour_smoothed
else
per_hour_total
end
end
def per_hour_smoothed
start_time = first || (now - RECENT)
end_time = now
range = end_time - start_time
average_time = inject(:+) / size.to_f
position = 1.0 / range * (average_time - start_time)
per_hour_total * position * 2
end
def per_hour_total
if elapsed == 0
0.0
else
count * (HOURS / elapsed)
end
end
end
Shoes.app :width => 300, :height => 200, :title => 'Smileometer' do
@cx, @cy = width / 2, height / 2
@border = 15
@radius = @cx - @border
@bottom_y = @border + @radius
@max = 1000.0
@sph = 0.0
@sph_smooth = 0.0
animate(4) do
@sph = $smiles.per_hour
@sph = 0.0 if @sph.nan? || @sph < 0
end
animate(16) do
clear do
@sph_smooth += (@sph - @sph_smooth) / 5
@sph_smooth = @max if @sph_smooth > @max
gauge
indicator @sph_smooth
readout "#{@sph.round} smiles per hour", 'left'
readout "#{$smiles.size} smiles", 'right'
end
end
def gauge
background black
stroke white
strokewidth 3
nofill
#face
arc @cx, @bottom_y, @radius*2, @radius*2, PI, TWO_PI
line @border, @bottom_y, @cx+@radius, @bottom_y
#dot
strokewidth 0
fill red
size = 6
oval @cx-size, @bottom_y-size, size*2, size*2
ticks
end
def ticks(number=10)
(0..number).each do |i|
amount = @max / number * i
indicator amount, 0.97..1.03, 1, white
end
end
def indicator(value, range=0.0..0.9, width=3, color=red)
strokewidth width
stroke color
x = @radius * Math.cos(value * PI / @max)
y = @radius * Math.sin(value * PI / @max)
line @cx-x*range.begin, @bottom_y-y*range.begin,
@cx-x*range.end, @bottom_y-y*range.end
end
def readout(text, align='left')
para text, :align => align, :stroke => white, :top => height-50, :margin => @border
end
end
$smiles = Smiles.new
$detector = IO.popen File.join(File.dirname(__FILE__), 'Smileometer\ Camera.app/Contents/MacOS/Smileometer\ Camera')
Thread.new do
while line = $detector.readline
$smiles.log if line.chomp == ":)"
end
end