This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
-- testing functions for use in arduino for hsb2rgb and rgb2hsb conversion against mac's built in conversion | ||
-- adapted from https://github.com/ratkins/RGBConverter/blob/master/RGBConverter.cpp | ||
-- converted to ranges used by Philips Hue bridge | ||
-- converted to lua for these tests | ||
|
||
local rgb2hsb = function(red, green, blue) | ||
local rd = red / 255 | ||
local gd = green / 255 | ||
local bd = blue / 255 | ||
local mx = math.max(rd, gd, bd) | ||
local mn = math.min(rd, gd, bd) | ||
local h, s | ||
local v = mx | ||
|
||
local d = mx - mn | ||
s = (mx == 0) and 0 or (d / mx) | ||
|
||
if (mx == mn) then | ||
h = 0 -- achromatic | ||
else | ||
if (mx == rd) then | ||
h = (gd - bd) / d + ((gd < bd) and 6 or 0) | ||
elseif (mx == gd) then | ||
h = (bd - rd) / d + 2 | ||
elseif (mx == bd) then | ||
h = (rd - gd) / d + 4 | ||
end | ||
h = h / 6 | ||
end | ||
|
||
return math.floor(h * 65535), math.floor(s * 255), math.floor(v * 255) | ||
end | ||
|
||
local hsb2rgb = function(hue, sat, bri) | ||
hue, sat, bri = hue / 65535, sat / 255, bri / 255 | ||
local r, g, b | ||
|
||
local i = math.floor(hue * 6) | ||
local f = hue * 6 - i | ||
local p = bri * (1 - sat) | ||
local q = bri * (1 - f * sat) | ||
local t = bri * (1 - (1 - f) * sat) | ||
|
||
if (i % 6) == 0 then | ||
r, g, b = bri, t, p | ||
elseif (i % 6) == 1 then | ||
r, g, b = q, bri, p | ||
elseif (i % 6) == 2 then | ||
r, g, b = p, bri, t | ||
elseif (i % 6) == 3 then | ||
r, g, b = p, q, bri | ||
elseif (i % 6) == 4 then | ||
r, g, b = t, p, bri | ||
elseif (i % 6) == 5 then | ||
r, g, b = bri, p, q | ||
end | ||
|
||
return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255) | ||
end | ||
|
||
-- now mac conversion functions | ||
|
||
local mac_rgb2hsb = function(red, green, blue) | ||
local color = require("hs.drawing").color | ||
local temp = color.asHSB{ red = red / 255, green = green / 255, blue = blue / 255 } | ||
return math.floor(temp.hue * 65535), math.floor(temp.saturation * 255), math.floor(temp.brightness * 255) | ||
end | ||
|
||
local mac_hsb2rgb = function(hue, sat, bri) | ||
local color = require("hs.drawing").color | ||
local temp = color.asRGB{ hue = hue / 65535, saturation = sat / 255, brightness = bri / 255 } | ||
return math.floor(temp.red * 255), math.floor(temp.green * 255), math.floor(temp.blue * 255) | ||
end | ||
|
||
|
||
|
||
require("hs.console").clearConsole() | ||
|
||
-- we'll accept conversions that are within a margin of error | ||
local closeEnough = function(a, b) return math.abs(a - b) <= 1 end | ||
|
||
local count, good = 0, 0 | ||
for r = 0, 1, .01 do for g = 0, 1, .01 do for b = 0, 1, .01 do -- for final test, do 1,000,000 comparisons | ||
count = count + 1 | ||
local r1, g1, b1 = math.floor(r * 255), math.floor(g * 255), math.floor(b * 255) | ||
local h2, s2, b2 = rgb2hsb(r1, g1, b1) | ||
local h3, s3, b3 = mac_rgb2hsb(r1, g1, b1) | ||
-- hue of 65535 == hue of 0, so catch both | ||
local st = (closeEnough(h2,h3) or ((h2 == 0) and (h3 == 65535)) or ((h2 == 65535) and (h3 == 0))) and (s2 == s3) and (b2 == b3) | ||
if st then | ||
good = good + 1 | ||
else | ||
hs.printf("%3d, %3d, %3d rgb2hsb == %5d, %3d, %3d mac_rgb2hsb == %5d, %3d, %3d %s", r1, g1, b1, h2, s2, b2, h3, s3, b3, st) | ||
end | ||
end end end | ||
|
||
hs.printf("rgb2hsb total %d, passed %d\n", count, good) | ||
|
||
local count, good = 0, 0 | ||
for h = 0, 1, .01 do for s = 0, 1, .01 do for b = 0, 1, .01 do | ||
count = count + 1 | ||
local h1, s1, b1 = math.floor(h * 65535), math.floor(s * 255), math.floor(b * 255) | ||
local r2, g2, b2 = hsb2rgb(h1, s1, b1) | ||
local r3, g3, b3 = mac_hsb2rgb(h1, s1, b1) | ||
local st = (r2 == r3) and (g2 == g3) and (b2 == b3) | ||
if st then | ||
good = good + 1 | ||
else | ||
hs.printf("%5d, %3d, %3d hsb2rgb == %3d, %3d, %3d mac_hsb2rgb == %3d, %3d, %3d %s", h1, s1, b1, r2, g2, b2, r3, g3, b3, st) | ||
end | ||
end end end | ||
|
||
hs.printf("hsb2rgb total %d, passed %d\n", count, good) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
local flagMasks = { | ||
activeFlag = 1 << 0, | ||
btnState = 1 << 7, | ||
cmdKey = 1 << 8, | ||
shiftKey = 1 << 9, | ||
alphaLock = 1 << 10, | ||
optionKey = 1 << 11, | ||
controlKey = 1 << 12, | ||
rightShiftKey = 1 << 13, | ||
rightOptionKey = 1 << 14, | ||
rightControlKey = 1 << 15, | ||
|
||
-- I think -- we can't set it, but I see this on keys that I also see this set for when using hs.eventtap | ||
functionKey = 1 << 17, | ||
} | ||
|
||
for i, v in ipairs(_xtras.hotkeys()) do | ||
local hotkey = rawget(hs.keycodes.map, v.kHISymbolicHotKeyCode) or ("{" .. tostring(v.kHISymbolicHotKeyCode) .. "}") | ||
local mods, mask = {}, v.kHISymbolicHotKeyModifiers | ||
for k2, v2 in pairs(flagMasks) do | ||
if (mask & v2) == v2 then | ||
mask = mask - v2 | ||
table.insert(mods, k2) | ||
end | ||
end | ||
if mask ~= 0 then table.insert(mods, mask) end | ||
print(string.format("%s %-12s %s", (v.kHISymbolicHotKeyEnabled and "+" or "-"), hotkey, finspect(mods))) | ||
end |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
local UUIDperipheralCurieBLE = "E924FF90-D5FD-41EB-A8BF-C0C928DE014F" | ||
local UUIDserviceCommand = "EA833120-DBE3-4035-B8EC-006AAD46BB07" | ||
local UUIDcharacteristicAction = "EA833121-DBE3-4035-B8EC-006AAD46BB07" | ||
|
||
local UUIDserviceIMU = "831BA4C0-4F24-4E46-85D9-1F04852731AA" | ||
local UUIDcharacteristicAccelerometer = "831BA4C1-4F24-4E46-85D9-1F04852731AA" | ||
local UUIDcharacteristicGyroscope = "831BA4C2-4F24-4E46-85D9-1F04852731AA" | ||
|
||
local module = {} | ||
|
||
local ble = require("hs._asm.btle") | ||
local timer = require("hs.timer") | ||
local hotkey = require("hs.hotkey") | ||
local mods = require("hs._asm.extras").mods | ||
|
||
local sendChar = function(char) | ||
local service = ble.discovered[UUIDperipheralCurieBLE].services[UUIDserviceCommand] | ||
if service then | ||
local characteristic = service.characteristics[UUIDcharacteristicAction] | ||
if characteristic then | ||
characteristic.characteristic:writeValue(((type(char) == "string") and char:sub(1,1) or "\0")) | ||
else | ||
hs.printf("missing characteristic %s for service %s", UUIDcharacteristicAction, UUIDserviceCommand) | ||
end | ||
else | ||
hs.printf("missing service %s", UUIDserviceCommand) | ||
end | ||
-- ble.discovered[UUIDperipheralCurieBLE]. | ||
-- services[UUIDserviceCommand]. | ||
-- characteristics[UUIDcharacteristicAction].characteristic:writeValue(((type(char) == "string") and char:sub(1,1) or "\0")) | ||
end | ||
|
||
ble.create() | ||
|
||
module._modalKeys = hotkey.modal.new() | ||
function module._modalKeys:entered() end | ||
module._modalKeys:bind(mods.cAsc, "w", function() sendChar("1") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "a", function() sendChar("2") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "s", function() sendChar("3") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "d", function() sendChar("4") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "f", function() sendChar("5") end, function() sendChar("\0") end) | ||
-- module._modalKeys:bind(mods.cAsc, "", function() sendChar("6") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "e", function() sendChar("7") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "r", function() sendChar("8") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "up", function() sendChar("a") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "down", function() sendChar("b") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "left", function() sendChar("c") end, function() sendChar("\0") end) | ||
module._modalKeys:bind(mods.cAsc, "right", function() sendChar("d") end, function() sendChar("\0") end) | ||
function module._modalKeys:exited() sendChar("\0") end | ||
|
||
module._setupTimer = timer.doEvery(1, function() | ||
if ble._manager then | ||
module._setupTimer:stop() | ||
module._setupTimer = timer.doEvery(1, function() | ||
if ble._manager:state() == "poweredOn" then | ||
module._setupTimer:stop() | ||
ble.startScanning() | ||
module._setupTimer = timer.doEvery(1, function() | ||
if ble.discovered[UUIDperipheralCurieBLE] then | ||
module._setupTimer:stop() | ||
module._setupTimer = nil | ||
print("Curie BLE discovered") | ||
end | ||
end) | ||
end | ||
end) | ||
end | ||
end) | ||
|
||
module.connect = function() | ||
if ble.discovered[UUIDperipheralCurieBLE].peripheral:state() == "disconnected" then | ||
ble._manager:connectPeripheral(ble.discovered[UUIDperipheralCurieBLE].peripheral) | ||
module._modalKeys:enter() | ||
else | ||
print("already connected or in progress") | ||
end | ||
end | ||
|
||
module.watch = function(watchUpdate) | ||
local service = ble.discovered[UUIDperipheralCurieBLE].services[UUIDserviceIMU] | ||
if service then | ||
local characteristic = service.characteristics[UUIDcharacteristicAccelerometer] | ||
if characteristic then | ||
characteristic.characteristic:watch(watchUpdate) | ||
else | ||
hs.printf("missing characteristic %s for service %s", UUIDcharacteristicAccelerometer, UUIDserviceIMU) | ||
end | ||
local characteristic = service.characteristics[UUIDcharacteristicGyroscope] | ||
if characteristic then | ||
characteristic.characteristic:watch(watchUpdate) | ||
else | ||
hs.printf("missing characteristic %s for service %s", UUIDcharacteristicGyroscope, UUIDserviceIMU) | ||
end | ||
ble.discovered[UUIDperipheralCurieBLE].fn = function(peripheral, message, ...) | ||
if message == "didUpdateValueForCharacteristic" then | ||
local characteristic, errMsg = ... | ||
local cUUID = characteristic:UUID() | ||
if errMsg then | ||
print("error with update to " .. cUUID .. ": " .. errMsg) | ||
else | ||
local label = "" | ||
if cUUID == UUIDcharacteristicAccelerometer then | ||
label = "Accelerometer" | ||
elseif cUUID == UUIDcharacteristicGyroscope then | ||
label = "Gyroscope" | ||
else | ||
print("not watching characteristic " .. cUUID) | ||
return | ||
end | ||
local value = characteristic:value() | ||
if #value == 12 then | ||
local x, y, z = string.unpack("fff", value) | ||
hs.printf("%-13s: X:%7.3f Y:%7.3f Z:%7.3f", label, x, y, z) | ||
else | ||
print("unexpected value length for " .. cUUID) | ||
end | ||
end | ||
end | ||
end | ||
else | ||
hs.printf("missing service %s", UUIDserviceIMU) | ||
end | ||
end | ||
|
||
|
||
module.disconnect = function() | ||
if ble.discovered[UUIDperipheralCurieBLE].peripheral:state() ~= "disconnected" then | ||
ble.discovered[UUIDperipheralCurieBLE].fn = nil | ||
module._modalKeys:exit() | ||
ble._manager:disconnectPeripheral(ble.discovered[UUIDperipheralCurieBLE].peripheral) | ||
else | ||
print("not connected") | ||
end | ||
end | ||
|
||
return module |
Oops, something went wrong.