Class: Mushy::SenseHatLedMatrix

Inherits:
SimplePythonProgram show all
Defined in:
lib/mushy/fluxs/sense_hat_led_matrix.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SimplePythonProgram

default_config, #process

Methods inherited from Bash

#process

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, #ignore_these_results, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #process, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



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
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 5

def self.details
  {
    name: 'SenseHatLedMatrix',
    description: 'Interface with the LED Matrix.',
    config: Mushy::SimplePythonProgram.default_config.tap do |config|
      config[:get_pixels] = {
                              description: 'Specify the pixels you want returned as events. Use "all" to return all 64, 3,3 to return x:3 y:3, or "none" to return none.',
                              type:        'text',
                              shrink:      true,
                              value:       'all',
                            }
      config[:set_pixel] = {
                             description: 'Set a single pixel to the RGB color.',
                             type:        'text',
                             shrink:      true,
                             value:       '',
                           }
      config[:set_pixels] = {
                              description: 'An array of multiple pixels to set. The records must have coordinates and rgb values, just like what comes out of the get pixels call.',
                              type:        'text',
                              shrink:      true,
                              value:       '',
                            }
      config[:rgb] = {
                        description: 'The RGB value as a comma-delimited list. Leave blank to not set a color.',
                        type:        'text',
                        shrink:      true,
                        value:       '',
                     }
      config[:background_color] = {
                                    description: 'The RGB of the background, which is used in some API calls.',
                                    type:        'text',
                                    shrink:      true,
                                    value:       '',
                                  }
      config[:clear] = {
                         description: 'The RGB color to apply to the entire grid.',
                         type:        'text',
                         shrink:      true,
                         value:       '',
                       }
      config[:show_letter] = {
                               description: 'Show a single letter on the grid. Uses Rgb and Background Color.',
                               type:        'text',
                               shrink:      true,
                               value:       '',
                             }
      config[:show_message] = {
                                description: 'Scroll a message across the grid. Uses Rgb and Background Color.',
                                type:        'text',
                                shrink:      true,
                                value:       '',
                              }
      config[:load_image] = {
                              description: 'Load a 8x8 image.',
                              type:        'text',
                              shrink:      true,
                              value:       '',
                            }
      config[:set_rotation] = {
                                description: 'Rotate the image by these degrees.',
                                type:        'select',
                                options:     ['', '0', '90', '180', '270'],
                                shrink:      true,
                                value:       '',
                              }
      config[:redraw] = {
                          description: 'Redraw.',
                          type:        'boolean',
                          shrink:      true,
                          value:       '',
                        }
      config[:low_light] = {
                          description: 'Use the low light to turn down the brightness.',
                          type:        'boolean',
                          shrink:      true,
                          value:       '',
                        }
      config[:flip_h] = {
                          description: 'Flips the image horizontally.',
                          type:        'boolean',
                          shrink:      true,
                          value:       '',
                        }
      config[:flip_v] = {
                          description: 'Flips the image vertically.',
                          type:        'boolean',
                          shrink:      true,
                          value:       '',
                        }
      config[:target] = {
                          description: 'The target of these commands. "hat" is a SenseHAT plugged into your Raspberry Pi, and "emu" is the SenseHAT emulator. Defaults to "hat".',
                          type:        'select',
                          options:     ['', 'hat' , 'emu'],
                          shrink:      true,
                          value:       '',
                        }
    end
  }
end

Instance Method Details

#adjust(data, event, config) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 135

def adjust data, event, config
  limit = 8

  coordinates = coordinates_from config[:get_pixels]

  records = coordinates ? [data[:all]] : data[:all]

  results = records.each_with_index.map do |item, index|
    {
      x: index % limit,
      y: index / limit,
      r: item[0],
      g: item[1],
      b: item[2],
    }
  end

  if coordinates
    results[0][:x] = coordinates[:x]
    results[0][:y] = coordinates[:y]
  end

  results.each do |record|
    record[:coordinate] = "#{record[:x]},#{record[:y]}"
    record[:rgb] = "#{record[:r]},#{record[:g]},#{record[:b]}"
  end

  results
end

#clear_pixels_code_from(event, config) ⇒ Object



205
206
207
208
209
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 205

def clear_pixels_code_from event, config
  clear = rgb_from config[:clear]
  return '' unless clear
  "sense.clear(#{clear[:r]}, #{clear[:g]}, #{clear[:b]})"
end

#coordinates_from(config) ⇒ Object



172
173
174
175
176
177
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 172

def coordinates_from config
  coordinate_split = config.to_s.split ','
  return nil unless coordinate_split.count == 2
  return [:x, :y].each_with_index
                  .reduce({}) { |t, i| t[i[0]] = coordinate_split[i[1]].to_s.to_i ; t}
end

#flip_h_code_from(event, config) ⇒ Object



255
256
257
258
259
260
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 255

def flip_h_code_from event, config
  return '' unless config[:flip_h].to_s == 'true'
  args = []
  args << config[:redraw].to_s.capitalize if config[:redraw].to_s != ''
  "sense.flip_h(#{args.join(',')})"
end

#flip_v_code_from(event, config) ⇒ Object



262
263
264
265
266
267
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 262

def flip_v_code_from event, config
  return '' unless config[:flip_v].to_s == 'true'
  args = []
  args << config[:redraw].to_s.capitalize if config[:redraw].to_s != ''
  "sense.flip_v(#{args.join(',')})"
end

#get_pixels_code_from(event, config) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 269

def get_pixels_code_from event, config
  get_pixel_coordinates = coordinates_from config[:get_pixels]

  if get_pixel_coordinates
    "sense.get_pixel(#{get_pixel_coordinates[:x]}, #{get_pixel_coordinates[:y]})"
  elsif config[:get_pixels].to_s.downcase == 'all'
    'sense.get_pixels()'
  else
    '[]'
  end
end

#load_images_code_from(event, config) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 235

def load_images_code_from event, config
  return '' if config[:load_image].to_s == ''

  args = ["\"#{config[:load_image]}\""]
  args << config[:redraw].to_s.capitalize if config[:redraw].to_s != ''
  "sense.load_image(#{args.join(',')})"
end

#low_light_code_from(event, config) ⇒ Object



250
251
252
253
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 250

def low_light_code_from event, config
  return '' if config[:low_light].to_s == ''
  "sense.low_light = #{config[:low_light].capitalize}"
end

#python_program(event, config) ⇒ Object



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
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 106

def python_program event, config

  commands = [
    :set_pixel_code_from,
    :set_pixels_code_from,
    :clear_pixels_code_from,
    :show_letters_code_from,
    :show_message_code_from,
    :load_images_code_from,
    :set_rotation_code_from,
    :low_light_code_from,
    :flip_h_code_from,
    :flip_v_code_from,
  ].map { |x| self.send x, event, config }
   .select { |x| x.to_s != '' }
   .join("\n")

  hat = config[:target] == 'emu' ? 'sense_emu' : 'sense_hat'

  <<PYTHON
from #{hat} import SenseHat
import json
sense = SenseHat()
#{commands}
value = json.dumps({"all": #{get_pixels_code_from(event, config)}})
print(value)
PYTHON
end

#rgb_from(config) ⇒ Object



165
166
167
168
169
170
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 165

def rgb_from config
  color_split = config.to_s.split ','
  return nil unless color_split.count == 3
  return [:r, :g, :b].each_with_index
                      .reduce({}) { |t, i| t[i[0]] = color_split[i[1]].to_s.to_i ; t}
end

#set_pixel_code_from(event, config) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 179

def set_pixel_code_from event, config
  rgb = rgb_from config[:rgb]
  set_pixel_coordinates = coordinates_from config[:set_pixel]
  return '' unless rgb
  return '' unless set_pixel_coordinates

  turn_these_to_pixels([ {
                           coordinate: "#{set_pixel_coordinates[:x]},#{set_pixel_coordinates[:y]}",
                           rgb: "#{rgb[:r]},#{rgb[:g]},#{rgb[:b]}",
                         } ])
end

#set_pixels_code_from(event, config) ⇒ Object



191
192
193
194
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 191

def set_pixels_code_from event, config
  return '' unless config[:set_pixels]
  turn_these_to_pixels config[:set_pixels]
end

#set_rotation_code_from(event, config) ⇒ Object



243
244
245
246
247
248
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 243

def set_rotation_code_from event, config
  return '' if config[:set_rotation].to_s == ''
  args = ["#{config[:set_rotation]}"]
  args << config[:redraw].to_s.capitalize if config[:redraw].to_s != ''
  "sense.set_rotation(#{args.join(',')})"
end

#show_letters_code_from(event, config) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 211

def show_letters_code_from event, config
  return '' if config[:show_letter].to_s == ''

  rgb = rgb_from config[:rgb]
  background_color = rgb_from config[:background_color]

  args = ["\"#{config[:show_letter]}\""]
  args << "text_colour=[#{rgb[:r]}, #{rgb[:g]}, #{rgb[:b]}]" if rgb
  args << "back_colour=[#{background_color[:r]}, #{background_color[:g]}, #{background_color[:b]}]" if background_color
  "sense.show_letter(#{args.join(',')})"
end

#show_message_code_from(event, config) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 223

def show_message_code_from event, config
  return '' if config[:show_message].to_s == ''

  rgb = rgb_from config[:rgb]
  background_color = rgb_from config[:background_color]

  args = ["\"#{config[:show_message]}\""]
  args << "text_colour=[#{rgb[:r]}, #{rgb[:g]}, #{rgb[:b]}]" if rgb
  args << "back_colour=[#{background_color[:r]}, #{background_color[:g]}, #{background_color[:b]}]" if background_color
  "sense.show_message(#{args.join(',')})"
end

#turn_these_to_pixels(values) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/mushy/fluxs/sense_hat_led_matrix.rb', line 196

def turn_these_to_pixels values
  options = [:coordinate, :rgb]
  values.map do |pixel|
    options.reduce({}) { |t, i| t[i] = pixel[i].split(',') ; t}
  end.map do |pixel|
    "sense.set_pixel(#{pixel[:coordinate][0]}, #{pixel[:coordinate][1]}, [#{pixel[:rgb][0]}, #{pixel[:rgb][1]}, #{pixel[:rgb][2]}])"
  end.join("\n")
end