|
| 1 | +// Copyright (c) 2016, Intel Corporation. |
| 2 | + |
| 3 | +console.log("Test GroveLCD APIs"); |
| 4 | + |
| 5 | +var grove_lcd = require("grove_lcd"); |
| 6 | + |
| 7 | +var total = 0; |
| 8 | +var passed = 0; |
| 9 | + |
| 10 | +function assert(actual, description) { |
| 11 | + total += 1; |
| 12 | + |
| 13 | + var label = "\033[1m\033[31mFAIL\033[0m"; |
| 14 | + if (actual === true) { |
| 15 | + passed += 1; |
| 16 | + label = "\033[1m\033[32mPASS\033[0m"; |
| 17 | + } |
| 18 | + |
| 19 | + console.log(label + " - " + description); |
| 20 | +} |
| 21 | + |
| 22 | +function expectThrow(description, func) { |
| 23 | + var threw = false; |
| 24 | + try { |
| 25 | + func(); |
| 26 | + } |
| 27 | + catch (err) { |
| 28 | + threw = true; |
| 29 | + } |
| 30 | + assert(threw, description); |
| 31 | +} |
| 32 | + |
| 33 | +// Check pins defined and typeof Number |
| 34 | +function checkDefined(name) { |
| 35 | + assert(name in grove_lcd && typeof grove_lcd[name] === "number", |
| 36 | + "groveLCD: defined as '" + name + "'"); |
| 37 | +} |
| 38 | + |
| 39 | +// test GroveLCD device defined |
| 40 | +var glcd = grove_lcd.init(); |
| 41 | +assert(glcd !== null && typeof glcd === "object", |
| 42 | + "groveLCD: GroveLCD device is defined"); |
| 43 | + |
| 44 | +// set cursor |
| 45 | +glcd.setCursorPos(0, 0); |
| 46 | + |
| 47 | +expectThrow("cursor: set col as '16'", function () { |
| 48 | + glcd.setCursorPos(16, 0); |
| 49 | +}); |
| 50 | + |
| 51 | +expectThrow("cursor: set row as '2'", function () { |
| 52 | + glcd.setCursorPos(0, 2); |
| 53 | +}); |
| 54 | + |
| 55 | +expectThrow("cursor: set col as 'value'", function () { |
| 56 | + glcd.setCursorPos("value", 0); |
| 57 | +}); |
| 58 | + |
| 59 | +// print text |
| 60 | +var printText = "Hello World"; |
| 61 | +glcd.print(printText); |
| 62 | + |
| 63 | +expectThrow("print: set text as '-1024'", function () { |
| 64 | + glcd.print(-1024); |
| 65 | +}); |
| 66 | + |
| 67 | +// select LCD color as key |
| 68 | +var colorKey = [ |
| 69 | + "GROVE_RGB_RED", |
| 70 | + "GROVE_RGB_GREEN", |
| 71 | + "GROVE_RGB_BLUE", |
| 72 | + "GROVE_RGB_WHITE" |
| 73 | +]; |
| 74 | + |
| 75 | +for (var i = 0; i < colorKey.length; i++) { |
| 76 | + checkDefined(colorKey[i]); |
| 77 | +} |
| 78 | + |
| 79 | +// set LCD color as num |
| 80 | +glcd.setColor(0, 0, 0); |
| 81 | + |
| 82 | +expectThrow("color: set invalid value as '256'", function () { |
| 83 | + glcd.setColor(256, 0, 0); |
| 84 | +}); |
| 85 | + |
| 86 | +expectThrow("color: set invalid value as '-1'", function () { |
| 87 | + glcd.setColor(-1, 0, 0); |
| 88 | +}); |
| 89 | + |
| 90 | +expectThrow("color: set invalid value as 'value'", function () { |
| 91 | + glcd.setColor("value", 0, 0); |
| 92 | +}); |
| 93 | + |
| 94 | +glcd.setColor(255, 255, 255); |
| 95 | + |
| 96 | +// clear display |
| 97 | +glcd.clear(); |
| 98 | + |
| 99 | +// set and get function with one keyword |
| 100 | +var funcOne = [ |
| 101 | + [grove_lcd.GLCD_FS_ROWS_1, 0, "GLCD_FS_ROWS_1"], |
| 102 | + [grove_lcd.GLCD_FS_ROWS_2, 8, "GLCD_FS_ROWS_2"], |
| 103 | + [grove_lcd.GLCD_FS_8BIT_MODE, 16, "GLCD_FS_8BIT_MODE"], |
| 104 | + [grove_lcd.GLCD_FS_DOT_SIZE_BIG, 4, "GLCD_FS_DOT_SIZE_BIG"], |
| 105 | + [grove_lcd.GLCD_FS_DOT_SIZE_LITTLE, 0, "GLCD_FS_DOT_SIZE_LITTLE"] |
| 106 | +]; |
| 107 | + |
| 108 | +for (var i = 0; i < funcOne.length; i++) { |
| 109 | + checkDefined(funcOne[i][2]); |
| 110 | + |
| 111 | + glcd.setFunction(funcOne[i][0]); |
| 112 | + |
| 113 | + assert(glcd.getFunction() === funcOne[i][1], |
| 114 | + "function: set and get as '" + funcOne[i][2]) + "'"; |
| 115 | +} |
| 116 | + |
| 117 | +// set and get function with more keyword combination |
| 118 | +var funcConfig1 = [ |
| 119 | + [0, 0, 0], |
| 120 | + [grove_lcd.GLCD_FS_ROWS_1, 0, "ROW1"], |
| 121 | + [grove_lcd.GLCD_FS_ROWS_2, 8, "ROW2"] |
| 122 | +]; |
| 123 | + |
| 124 | +var funcConfig2 = [ |
| 125 | + [0, 0, 0], |
| 126 | + [grove_lcd.GLCD_FS_8BIT_MODE, 16, "8BIT"] |
| 127 | +]; |
| 128 | + |
| 129 | +var funcConfig3 = [ |
| 130 | + [0, 0, 0], |
| 131 | + [grove_lcd.GLCD_FS_DOT_SIZE_BIG, 4, "BIG"], |
| 132 | + [grove_lcd.GLCD_FS_DOT_SIZE_LITTLE, 0, "LIT"] |
| 133 | +]; |
| 134 | + |
| 135 | +var funcConfig, funcValue, funcLog; |
| 136 | +for (var i = 0; i < funcConfig1.length; i++) { |
| 137 | + for (var j = 0; j < funcConfig2.length; j++) { |
| 138 | + for (var k = 0; k < funcConfig3.length; k++) { |
| 139 | + funcConfig = funcConfig1[i][0] |
| 140 | + | funcConfig2[j][0] |
| 141 | + | funcConfig3[k][0]; |
| 142 | + |
| 143 | + funcValue = funcConfig1[i][1] |
| 144 | + + funcConfig2[j][1] |
| 145 | + + funcConfig3[k][1]; |
| 146 | + |
| 147 | + funcLog = funcConfig1[i][2] + " and " |
| 148 | + + funcConfig2[j][2] + " and " |
| 149 | + + funcConfig3[k][2]; |
| 150 | + |
| 151 | + glcd.setFunction(funcConfig); |
| 152 | + |
| 153 | + if (i === 0 && j === 0 && k === 0) continue; |
| 154 | + if (i === 0 && j === 0) continue; |
| 155 | + if (i === 0 && k === 0) continue; |
| 156 | + if (j === 0 && k === 0) continue; |
| 157 | + |
| 158 | + if (i !== 0) { |
| 159 | + if (j !== 0) { |
| 160 | + if (k === 0) { |
| 161 | + funcLog = funcConfig1[i][2] + " and " |
| 162 | + + funcConfig2[j][2]; |
| 163 | + } |
| 164 | + } else { |
| 165 | + if (k !== 0) { |
| 166 | + funcLog = funcConfig1[i][2] + " and " |
| 167 | + + funcConfig3[k][2]; |
| 168 | + } |
| 169 | + } |
| 170 | + } else { |
| 171 | + if (j !== 0 && k !== 0) { |
| 172 | + funcLog = funcConfig2[j][2] + " and " |
| 173 | + + funcConfig3[k][2]; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + assert(glcd.getFunction() === funcValue, |
| 178 | + "function: keyword as " + funcLog); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// set and get display state with one keyword |
| 184 | +var displayOne = [ |
| 185 | + [grove_lcd.GLCD_DS_DISPLAY_OFF, 0, "GLCD_DS_DISPLAY_OFF"], |
| 186 | + [grove_lcd.GLCD_DS_DISPLAY_ON, 4, "GLCD_DS_DISPLAY_ON"], |
| 187 | + [grove_lcd.GLCD_DS_CURSOR_OFF, 0, "GLCD_DS_CURSOR_OFF"], |
| 188 | + [grove_lcd.GLCD_DS_CURSOR_ON, 2, "GLCD_DS_CURSOR_ON"], |
| 189 | + [grove_lcd.GLCD_DS_BLINK_OFF, 0, "GLCD_DS_BLINK_OFF"], |
| 190 | + [grove_lcd.GLCD_DS_BLINK_ON, 1, "GLCD_DS_BLINK_ON"] |
| 191 | +]; |
| 192 | + |
| 193 | +for (var i = 0; i < displayOne.length; i++) { |
| 194 | + checkDefined(displayOne[i][2]); |
| 195 | + |
| 196 | + glcd.setDisplayState(displayOne[i][0]); |
| 197 | + |
| 198 | + assert(glcd.getDisplayState() === displayOne[i][1], |
| 199 | + "display: set and get as '" + displayOne[i][2] + "'"); |
| 200 | +} |
| 201 | + |
| 202 | +// set and get cursor 'on' and 'off' |
| 203 | +var displayTwo = grove_lcd.GLCD_DS_DISPLAY_ON | grove_lcd.GLCD_DS_CURSOR_ON; |
| 204 | +glcd.setDisplayState(displayTwo); |
| 205 | + |
| 206 | +assert(glcd.getDisplayState() === 6, |
| 207 | + "display: set and get with cursor 'on'"); |
| 208 | + |
| 209 | +displayTwo = grove_lcd.GLCD_DS_DISPLAY_ON | grove_lcd.GLCD_DS_CURSOR_OFF; |
| 210 | +glcd.setDisplayState(displayTwo); |
| 211 | + |
| 212 | +assert(glcd.getDisplayState() === 4, |
| 213 | + "display: set and get with cursor 'off'"); |
| 214 | + |
| 215 | +// set and get blink 'on' and 'off' |
| 216 | +var displayThree = grove_lcd.GLCD_DS_DISPLAY_ON |
| 217 | + | grove_lcd.GLCD_DS_CURSOR_ON |
| 218 | + | grove_lcd.GLCD_DS_BLINK_ON; |
| 219 | +glcd.setDisplayState(displayThree); |
| 220 | + |
| 221 | +assert(glcd.getDisplayState() === 7, |
| 222 | + "display: set and get with blink 'on'"); |
| 223 | + |
| 224 | +displayThree = grove_lcd.GLCD_DS_DISPLAY_ON |
| 225 | + | grove_lcd.GLCD_DS_CURSOR_ON |
| 226 | + | grove_lcd.GLCD_DS_BLINK_OFF; |
| 227 | +glcd.setDisplayState(displayThree); |
| 228 | + |
| 229 | +assert(glcd.getDisplayState() === 6, |
| 230 | + "display: set and get with blink 'off'"); |
| 231 | + |
| 232 | +// set and get input state with one keyword |
| 233 | +var inputOne = [ |
| 234 | + [grove_lcd.GLCD_IS_SHIFT_DECREMENT, 0, "GLCD_IS_SHIFT_DECREMENT"], |
| 235 | + [grove_lcd.GLCD_IS_SHIFT_INCREMENT, 2, "GLCD_IS_SHIFT_INCREMENT"], |
| 236 | + [grove_lcd.GLCD_IS_ENTRY_RIGHT, 0, "GLCD_IS_ENTRY_RIGHT"], |
| 237 | + [grove_lcd.GLCD_IS_ENTRY_LEFT, 1, "GLCD_IS_ENTRY_LEFT"] |
| 238 | +]; |
| 239 | + |
| 240 | +for (var i = 0; i < inputOne.length; i++) { |
| 241 | + checkDefined(inputOne[i][2]); |
| 242 | + |
| 243 | + glcd.setInputState(inputOne[i][0]); |
| 244 | + |
| 245 | + assert(glcd.getInputState() === inputOne[i][1], |
| 246 | + "input: set and get as '" + inputOne[i][2] + "'"); |
| 247 | +} |
| 248 | + |
| 249 | +// set and get input state with enter and shift |
| 250 | +var inputConfig = grove_lcd.GLCD_IS_ENTRY_RIGHT |
| 251 | + | grove_lcd.GLCD_IS_SHIFT_DECREMENT; |
| 252 | +glcd.setInputState(inputConfig); |
| 253 | + |
| 254 | +assert(glcd.getInputState() === 0, "input: right enter and decrement"); |
| 255 | + |
| 256 | +inputConfig = grove_lcd.GLCD_IS_ENTRY_RIGHT |
| 257 | + | grove_lcd.GLCD_IS_SHIFT_INCREMENT; |
| 258 | +glcd.setInputState(inputConfig); |
| 259 | + |
| 260 | +assert(glcd.getInputState() === 2, "input: right enter and increment"); |
| 261 | + |
| 262 | +inputConfig = grove_lcd.GLCD_IS_ENTRY_LEFT |
| 263 | + | grove_lcd.GLCD_IS_SHIFT_DECREMENT; |
| 264 | +glcd.setInputState(inputConfig); |
| 265 | + |
| 266 | +assert(glcd.getInputState() === 1, "input: left enter and decrement"); |
| 267 | + |
| 268 | +inputConfig = grove_lcd.GLCD_IS_ENTRY_LEFT |
| 269 | + | grove_lcd.GLCD_IS_SHIFT_INCREMENT; |
| 270 | +glcd.setInputState(inputConfig); |
| 271 | + |
| 272 | +assert(glcd.getInputState() === 3, "input: left enter and increment"); |
| 273 | + |
| 274 | +console.log("TOTAL: " + passed + " of " + total + " passed"); |
0 commit comments