-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlcd.coffee
179 lines (147 loc) · 5.85 KB
/
lcd.coffee
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
module.exports = (env) ->
# Require the bluebird promise library
Promise = env.require 'bluebird'
S = env.require 'string'
M = env.matcher
_ = env.require 'lodash'
# Require the [cassert library](https://github.com/rhoot/cassert).
assert = env.require 'cassert'
LCD = require 'i2c-lcd'
class LCDPlugin extends env.plugins.Plugin
prepareConfig: (config) =>
if _.isNumber config.address
config.address = "#{config.address}"
init: (app, @framework, @config) =>
# parseInt is used without the radix parameter to allow for, both, decimal and hexadecimal number strings
lcd = new LCD(@config.bus, parseInt @config.address)
lcd.pendingOperation = lcd.init()
lcd._printedLines = []
@framework.ruleManager.addActionProvider(
new LCDDisplayActionProvider @framework, lcd, @config
)
@framework.ruleManager.addActionProvider(
new LCDBacklightActionProvider @framework, lcd, @config
)
class LCDDisplayActionProvider extends env.actions.ActionProvider
constructor: (@framework, @lcd, @pluginConfig) ->
return
parseAction: (input, context) =>
textTokens = null
setText = (m, tokens) => textTokens = tokens
lineNumber = [ 1 ]
m = M(input, context)
.match('display ')
.match('text ', optional: yes)
.matchStringWithVars(setText)
.match(" on lcd")
if m.hadMatch()
m.match(" line ", (next) =>
next.matchNumericExpression( (next, tokens) =>
lineNumber = tokens
m = next
)
)
if m.hadMatch()
match = m.getFullMatch()
assert Array.isArray(textTokens)
assert Array.isArray(lineNumber)
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new LCDDisplayActionHandler(
@framework, @lcd, @pluginConfig, textTokens, lineNumber
)
}
class LCDDisplayActionHandler extends env.actions.ActionHandler
constructor: (@framework, @lcd, @pluginConfig, @textTokens, @lineNumber) ->
executeAction: (simulate, context) ->
Promise.all( [
@framework.variableManager.evaluateStringExpression(@textTokens)
@framework.variableManager.evaluateNumericExpression(@lineNumber)
]).then( ([text, line]) =>
@lastLine = line
if simulate
# just return a promise fulfilled with a description about what we would do.
return __("would display \"%s\" on lcd line %s", text, line)
else
rows = @pluginConfig.rows
cols = @pluginConfig.cols
unless 1 <= line <= rows
throw new Error("line must be between 1 and #{rows}")
if text.length > cols
printText = text.substring(0, cols)
else if text.length < cols
printText = S(text).padRight(cols).s
else
printText = text
return @lcd.pendingOperation = @lcd.pendingOperation
.then( => @lcd.setCursor(0, line-1) )
.then( =>
@lastPrintText = printText
@lcd._printedLines[line-1] = printText # remember new text
return @lcd.print(printText)
).then( => __("displaying \"%s\" on lcd line %s", text, line) )
)
hasRestoreAction: -> yes
# ### executeRestoreAction()
executeRestoreAction: (simulate) ->
if simulate
return Promise.resolve __("would clear LCD line %s", @lastLine)
else
# only clear if it not changed in between
if @lastPrintText is @lcd._printedLines[@lastLine-1]
return @lcd.pendingOperation = @lcd.pendingOperation
.then( => @lcd.setCursor(0, @lastLine-1) )
.then( =>
printText = S(" ").repeat(@pluginConfig.cols).s
@lcd._printedLines[@lastLine-1] = printText
return @lcd.print(printText)
).then( => __("cleared LCD line %s", @lastLine) )
else
Promise.resolve __("didn't clear LCD line %s because it changed", @lastLine)
class LCDBacklightActionProvider extends env.actions.ActionProvider
constructor: (@framework, @lcd, @pluginConfig) ->
return
parseAction: (input, context) =>
state = null
setState = (next, match) => state = (match.trim() is "on")
m = M(input, context)
.match('turn ')
.match('the ', optional: yes)
.match('LCD')
.match(' backlight', optional: yes)
.match([' on', ' off'], setState)
if m.hadMatch()
match = m.getFullMatch()
assert typeof state is "boolean"
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new LCDBacklightActionHandler(
@framework, @lcd, @pluginConfig, state
)
}
class LCDBacklightActionHandler extends env.actions.ActionHandler
constructor: (@framework, @lcd, @pluginConfig, @state) ->
executeAction: (simulate, context) ->
return Promise.resolve().then( =>
if simulate
# just return a promise fulfilled with a description about what we would do.
return __("would turn LCD %s", (if @state then __("on") else __("off") ) )
else
return (
if @state
@lcd.pendingOperation = @lcd.pendingOperation.then( => @lcd.on() )
else
@lcd.pendingOperation = @lcd.pendingOperation.then( => @lcd.off() )
).then( =>
return __("turned LCD backlight %s", (if @state then __("on") else __("off") ) )
)
)
module.exports.LCDDisplayActionHandler = LCDDisplayActionHandler
module.exports.LCDBacklightActionProvider = LCDBacklightActionProvider
# ###Finally
# Create a instance of my plugin
lcdPlugin = new LCDPlugin
# and return it to the framework.
return lcdPlugin