// Raw sensor counts, not lux. CommonJS tests + QML import. var labels = ["dark", "dim", "bright", "sunlight"]; var defaults = [0.25, 0.5, 0.75, 1]; function validThresholds(value) { return Array.isArray(value) && value.length === 3 && value.every(function (n, i) { return typeof n === "number" && Number.isSafeInteger(n) && n > 0 && (i === 0 || n > value[i - 1]); }); } function normalizeConfig(value) { var source = value && typeof value === "object" ? value : {}; var targets = {}; var input = source.targets; if (input && typeof input === "object" && !Array.isArray(input)) { Object.keys(input).forEach(function (id) { if (id !== "backlight" && !/^ddc:[A-Za-z0-9-]+$/.test(id)) return; var row = input[id]; if (!Array.isArray(row) || row.length !== 4) return; targets[id] = row.map(function (n, i) { return typeof n === "number" && isFinite(n) && n >= 0 && n <= 1 ? n : defaults[i]; }); }); } return { enabled: typeof source.enabled === "boolean" ? source.enabled : true, thresholds: validThresholds(source.thresholds) ? source.thresholds.slice() : [25, 100, 300], targets: targets }; } // Reject nonintegers/nonpositive inputs; clamp valid edits between adjacent bounds. function editThreshold(thresholds, index, value) { if (!Number.isInteger(index) || index < 0 || index > 2 || typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) return null; var minimum = index === 0 ? 1 : thresholds[index - 1] + 1; var maximum = index === 2 ? Number.MAX_SAFE_INTEGER : thresholds[index + 1] - 1; var result = thresholds.slice(); result[index] = Math.max(minimum, Math.min(maximum, value)); return result; } function smooth(previous, raw) { return previous === null ? raw : previous + 0.25 * (raw - previous); } // Ease actuator motion in time, never interpolate targets across ambient buckets. function easedLevel(from, target, progress) { var p = Math.max(0, Math.min(1, progress)); return from + (target - from) * p * p * (3 - 2 * p); } function bucket(value, thresholds, current) { if (value === null || !isFinite(value) || value < 0) return -1; var next = Number.isInteger(current) && current >= 0 && current <= 3 ? current : -1; if (next < 0) { next = 0; while (next < 3 && value >= thresholds[next]) next++; return next; } while (next < 3 && value >= thresholds[next] + Math.max(3, thresholds[next] * 0.1)) next++; while (next > 0 && value < thresholds[next - 1] - Math.max(3, thresholds[next - 1] * 0.1)) next--; return next; } function target(config, id, index) { return config.targets[id] ? config.targets[id][index] : defaults[index]; } function learn(config, id, index, actual) { if (!Number.isInteger(index) || index < 0 || index > 3 || typeof actual !== "number" || !isFinite(actual) || actual < 0 || actual > 1) return config; var next = normalizeConfig(config); var row = next.targets[id] || defaults.slice(); row[index] = actual; next.targets[id] = row; return next; } function buckets(config) { return labels.map(function (label, index) { var targets = {}; Object.keys(config.targets).forEach(function (id) { targets[id] = config.targets[id][index]; }); return { id: label, label: label, target: defaults[index], targets: targets }; }); } if (typeof module !== "undefined" && module.exports) { module.exports = { normalizeConfig: normalizeConfig, validThresholds: validThresholds, editThreshold: editThreshold, smooth: smooth, easedLevel: easedLevel, bucket: bucket, target: target, learn: learn, buckets: buckets }; }