|
| 1 | +/** |
| 2 | + * Author Kevin Ryu |
| 3 | + */ |
| 4 | +function Module() { |
| 5 | + this.initialBuffer = new Array(20); |
| 6 | + this.localBuffer = new Array(20); |
| 7 | + this.remoteBuffer = new Array(17); |
| 8 | + |
| 9 | + for (let i = 0; i < 20; ++i) { |
| 10 | + this.initialBuffer[i] = 0; |
| 11 | + this.localBuffer[i] = 0; |
| 12 | + } |
| 13 | + for (let i = 0; i < 17; ++i) { |
| 14 | + this.remoteBuffer[i] = 0; |
| 15 | + } |
| 16 | + this.initialBuffer[0] = 0xFF; |
| 17 | + this.initialBuffer[1] = 0xFF; |
| 18 | + this.initialBuffer[18] = 0xFE; |
| 19 | + this.initialBuffer[19] = 0xFE; |
| 20 | + this.localBuffer[0] = 0xFF; |
| 21 | + this.localBuffer[1] = 0xFF; |
| 22 | + this.localBuffer[18] = 0xFE; |
| 23 | + this.localBuffer[19] = 0xFE; |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +Module.prototype.SENSOR_MAP = { |
| 28 | + 1: 'light', |
| 29 | + 2: 'IR', |
| 30 | + 3: 'touch', |
| 31 | + 4: 'potentiometer', |
| 32 | + 5: 'MIC', |
| 33 | + 6: 'ultrasonicSensor', |
| 34 | + 7: 'temperature', |
| 35 | + 10: 'vibrationSensor', |
| 36 | + 21: 'UserSensor', |
| 37 | + 11: 'UserInput', |
| 38 | + 20: 'LED', |
| 39 | + 19: 'SERVO', |
| 40 | + 18: 'DC', |
| 41 | +}; |
| 42 | + |
| 43 | +Module.prototype.PORT_MAP = { |
| 44 | + 'buzzer': 2, |
| 45 | + '5': 4, |
| 46 | + '6': 6, |
| 47 | + '7': 8, |
| 48 | + '8': 10, |
| 49 | + 'LEDR': 12, |
| 50 | + 'LEDG': 14, |
| 51 | + 'LEDB': 16, |
| 52 | +}; |
| 53 | + |
| 54 | +const CoalaBoardType = { |
| 55 | + |
| 56 | + B16_START_1: 0xFF, |
| 57 | + B16_START_2: 0xFF, |
| 58 | + |
| 59 | + B16_END_1: 0xFE, |
| 60 | + B16_END_2: 0xFE, |
| 61 | + |
| 62 | + P_NO: 0x00, |
| 63 | + P_BRI: 0x01, |
| 64 | + P_IRS: 0x02, |
| 65 | + P_BUT: 0x03, |
| 66 | + P_POT: 0x04, |
| 67 | + P_MIC: 0x05, |
| 68 | + P_ULT: 0x06, |
| 69 | + P_TEM: 0x07, |
| 70 | + P_VIB: 0x0A, |
| 71 | + P_USR: 0x0B, |
| 72 | + |
| 73 | + P_M_SV: 0x13, |
| 74 | + P_M_DC: 0x12, |
| 75 | + P_LED: 0x14, |
| 76 | + |
| 77 | + RANGE_SNESOR_PORT: ['1', '2', '3', '4'], |
| 78 | + RANGE_MOTOR_PORT: ['A', 'B', 'C', 'D'], |
| 79 | + RANGE_LED_PORT: ['E'], |
| 80 | + |
| 81 | + DC_MOTOR_ADJUSTMENT: 128, |
| 82 | + SERVO_ADJUSTMENT: 1, |
| 83 | + BUZZER_ADJUSTMENT: 11, |
| 84 | + |
| 85 | +}; |
| 86 | + |
| 87 | +Module.prototype.init = function(handler, config) { |
| 88 | +}; |
| 89 | + |
| 90 | +Module.prototype.requestInitialData = function() { |
| 91 | + return this.initialBuffer; |
| 92 | +}; |
| 93 | + |
| 94 | +Module.prototype.checkInitialData = function(data, config) { |
| 95 | + if (data && data.length == 17) { |
| 96 | + if (data[0] === 0xFF && data[1] === 0xFF && data[15] === 0xFE && data[16] === 0xFE) { |
| 97 | + return true; |
| 98 | + } else { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +Module.prototype.handleRemoteData = function(handler) { |
| 105 | + const buffer = this.localBuffer; |
| 106 | + for (const key in this.PORT_MAP) { |
| 107 | + const port = this.PORT_MAP[key]; |
| 108 | + const value = handler.read(key); |
| 109 | + if (value === undefined) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + buffer[port] = value >> 8; |
| 113 | + buffer[port + 1] = value & (Math.pow(2, 9) - 1); |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +Module.prototype.requestLocalData = function() { |
| 118 | + return this.localBuffer; |
| 119 | +}; |
| 120 | + |
| 121 | +Module.prototype.handleLocalData = function(data) { // data: Native Buffer |
| 122 | + const buffer = this.remoteBuffer; |
| 123 | + if (data && data.length == 17) { |
| 124 | + if (data[0] === 0xFF && data[1] === 0xFF && data[15] === 0xFE && data[16] === 0xFE) { |
| 125 | + for (let i = 0; i < 17; ++i) { |
| 126 | + buffer[i] = data[i]; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +Module.prototype.requestRemoteData = function(handler) { |
| 133 | + const buffer = this.remoteBuffer; |
| 134 | + for (let i = 2; i < 17; i += 1) { |
| 135 | + const value = buffer[i] * Math.pow(2, 8) + buffer[i + 1]; |
| 136 | + let sensorType = this.SENSOR_MAP[buffer[i]]; |
| 137 | + if (i < 10) { |
| 138 | + sensorType = this.SENSOR_MAP[buffer[i] >> 2]; |
| 139 | + } |
| 140 | + const sensorValue = value & (Math.pow(2, 10) - 1); |
| 141 | + if (i < 10) { |
| 142 | + if (sensorType) { |
| 143 | + if (sensorType == 'temperature') { // 7: 온도센서 |
| 144 | + // [온도센서] |
| 145 | + // 1. 코알라에서 PC로 전달하는 값의 범위 : 0 ~ 160 |
| 146 | + // 2. 실제로 엔트리에서 보여줘야 하는 실제 온도 : - 40 ~ 120 |
| 147 | + // 3. 정리 : 전달되는 값에서 -40을 하면 됨. |
| 148 | + handler.write(i / 2, { type: sensorType, value: sensorValue - 40 }); |
| 149 | + } else { |
| 150 | + handler.write(i / 2, { type: sensorType, value: sensorValue }); |
| 151 | + } |
| 152 | + } else { |
| 153 | + handler.write(i / 2, null); |
| 154 | + } |
| 155 | + i += 1; |
| 156 | + } else { |
| 157 | + if (sensorType) { |
| 158 | + handler.write((i - 5), { type: sensorType }); |
| 159 | + } else { |
| 160 | + handler.write((i - 5), null); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + // 1: 'light', |
| 165 | + // 2: 'IR', |
| 166 | + // 3: 'touch', |
| 167 | + // 4: 'potentiometer', |
| 168 | + // 5: 'MIC', |
| 169 | + // 6: 'ultrasonicSensor', |
| 170 | + // 7: 'temperature', |
| 171 | + // 10: 'vibrationSensor', |
| 172 | + // 21: 'UserSensor', |
| 173 | + // 11: 'UserInput', |
| 174 | + // 20: 'LED', |
| 175 | + // 19: 'SERVO', |
| 176 | + // 18: 'DC' |
| 177 | + |
| 178 | + const p1 = this._decodeSensorPortNum(buffer[2], buffer[3]); // port 1 |
| 179 | + const p2 = this._decodeSensorPortNum(buffer[4], buffer[5]); // port 2, temperature |
| 180 | + const p3 = this._decodeSensorPortNum(buffer[6], buffer[7]); // port 3, light |
| 181 | + const p4 = this._decodeSensorPortNum(buffer[8], buffer[9]); // port 4, touch |
| 182 | + |
| 183 | + let valueUserInput = ' '; |
| 184 | + let valuePotentiometer = ' '; |
| 185 | + let valueMic = ' '; |
| 186 | + let valueIr = ' '; |
| 187 | + |
| 188 | + if (p1.sensorType == CoalaBoardType.P_NO) { |
| 189 | + } else if (p1.sensorType == CoalaBoardType.P_IRS) { |
| 190 | + valueIr = p1.sensorValue; |
| 191 | + } else if (p1.sensorType == CoalaBoardType.P_POT) { |
| 192 | + valuePotentiometer = p1.sensorValue; |
| 193 | + } else if (p1.sensorType == CoalaBoardType.P_MIC) { |
| 194 | + valueMic = p1.sensorValue; |
| 195 | + } else if (p1.sensorType == CoalaBoardType.P_ULT) { |
| 196 | + } else if (p1.sensorType == CoalaBoardType.P_VIB) { |
| 197 | + } else if (p1.sensorType == CoalaBoardType.P_USR) { |
| 198 | + valueUserInput = p1.sensorValue; |
| 199 | + } else { |
| 200 | + } |
| 201 | + |
| 202 | + // UserInput |
| 203 | + // potentiometer |
| 204 | + // MIC |
| 205 | + // IR |
| 206 | + // temperature |
| 207 | + // light |
| 208 | + // touch |
| 209 | + |
| 210 | + Module.prototype.SENSOR_MAP = { |
| 211 | + 1: 'light', |
| 212 | + 2: 'IR', |
| 213 | + 3: 'touch', |
| 214 | + 4: 'potentiometer', |
| 215 | + 5: 'MIC', |
| 216 | + 6: 'ultrasonicSensor', |
| 217 | + 7: 'temperature', |
| 218 | + 10: 'vibrationSensor', |
| 219 | + 21: 'UserSensor', |
| 220 | + 11: 'UserInput', |
| 221 | + 20: 'LED', |
| 222 | + 19: 'SERVO', |
| 223 | + 18: 'DC', |
| 224 | + }; |
| 225 | + |
| 226 | + handler.write(this.SENSOR_MAP[11], { type: this.SENSOR_MAP[11], value: valueUserInput }); // UserInput |
| 227 | + handler.write(this.SENSOR_MAP[4], { type: this.SENSOR_MAP[4], value: valuePotentiometer }); // potentiometer |
| 228 | + handler.write(this.SENSOR_MAP[5], { type: this.SENSOR_MAP[5], value: valueMic }); // MIC |
| 229 | + handler.write(this.SENSOR_MAP[2], { type: this.SENSOR_MAP[2], value: valueIr }); // IR |
| 230 | + handler.write(this.SENSOR_MAP[7], { type: this.SENSOR_MAP[7], value: p2.sensorValue }); // temperature |
| 231 | + handler.write(this.SENSOR_MAP[1], { type: this.SENSOR_MAP[1], value: p3.sensorValue }); // light |
| 232 | + handler.write(this.SENSOR_MAP[3], { type: this.SENSOR_MAP[3], value: p4.sensorValue }); // button(touch) |
| 233 | + // ==================================================================== |
| 234 | +}; |
| 235 | + |
| 236 | +Module.prototype.reset = function() { |
| 237 | + const buffer = this.localBuffer; |
| 238 | + for (let i = 2; i < 18; ++i) { |
| 239 | + buffer[i] = 0; |
| 240 | + } |
| 241 | +}; |
| 242 | + |
| 243 | +/** |
| 244 | + * |
| 245 | + * @param number portNum1 |
| 246 | + * @param number portNum2 |
| 247 | + */ |
| 248 | +Module.prototype._decodeSensorPortNum = function(portNum1, portNum2) { |
| 249 | + const sP1 = portNum1.toString(2).padStart(8, '0'); |
| 250 | + const sMsb = sP1.substr(0, 1); |
| 251 | + const sSensorType = sP1.substr(1, 5); |
| 252 | + const sV1 = sP1.substr(6, 2); |
| 253 | + const sV2 = portNum2.toString(2).padStart(8, '0'); |
| 254 | + const sSensorVal = sV1 + sV2; |
| 255 | + |
| 256 | + let iSensorType = parseInt(sSensorType, 2); // 2 => 10 |
| 257 | + let iSensorVal = parseInt(sSensorVal, 2); // 2 => 10 |
| 258 | + |
| 259 | + if (iSensorType == CoalaBoardType.P_TEM) { |
| 260 | + // [온도센서] |
| 261 | + // 1. 비트브릭에서 PC로 전달하는 값의 범위 : 0 ~ 160 |
| 262 | + // 2. 실제로 스크래치에서 보여줘야 하는 실제 온도 : - 40 ~ 120 |
| 263 | + // 3. 정리 : 전달되는 값에서 -40을 하면 됨. |
| 264 | + iSensorVal = iSensorVal - 40; |
| 265 | + } else if ( |
| 266 | + (iSensorType == CoalaBoardType.P_BRI) || |
| 267 | + (iSensorType == CoalaBoardType.P_IRS) || |
| 268 | + (iSensorType == CoalaBoardType.P_BUT) || |
| 269 | + (iSensorType == CoalaBoardType.P_POT) || |
| 270 | + (iSensorType == CoalaBoardType.P_MIC) || |
| 271 | + (iSensorType == CoalaBoardType.P_ULT) || |
| 272 | + (iSensorType == CoalaBoardType.P_VIB) || |
| 273 | + (iSensorType == CoalaBoardType.P_USR) |
| 274 | + ) { |
| 275 | + // it's a sensor |
| 276 | + } else { |
| 277 | + // it's not a sensor |
| 278 | + iSensorType = CoalaBoardType.P_NO; |
| 279 | + iSensorVal = 0; |
| 280 | + } |
| 281 | + return { sensorType: iSensorType, sensorValue: iSensorVal }; |
| 282 | +}; |
| 283 | + |
| 284 | +module.exports = new Module(); |
0 commit comments