scooty
3 years ago
4 changed files with 149 additions and 0 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,67 @@ |
|||
// squeeky door laptop hinge haha |
|||
// |
|||
// lmutracker.mm |
|||
// |
|||
// clang -o lmutracker lmutracker.mm -framework IOKit -framework CoreFoundation |
|||
//// |
|||
// mplayer -ss 00:00:01.90 -endpos 00:00:02.90 door.mp3 |
|||
// |
|||
// via:https://stackoverflow.com/questions/17625495/how-do-you-programmatically-access-the-ambient-light-sensor-on-mac-os-x-10-5 |
|||
|
|||
|
|||
#include <mach/mach.h> |
|||
#import <IOKit/IOKitLib.h> |
|||
#import <CoreFoundation/CoreFoundation.h> |
|||
|
|||
static double updateInterval = 0; //0 means only run once |
|||
static io_connect_t dataPort = 0; |
|||
|
|||
void updateTimerCallBack(CFRunLoopTimerRef timer, void *info) { |
|||
kern_return_t kr; |
|||
uint32_t outputs = 2; |
|||
uint64_t values[outputs]; |
|||
|
|||
kr = IOConnectCallMethod(dataPort, 0, nil, 0, nil, 0, values, &outputs, nil, 0); |
|||
if (kr == KERN_SUCCESS) { |
|||
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%8lld", values[0]); |
|||
//return; |
|||
exit(0); // exit program after getting value! |
|||
} |
|||
|
|||
if (kr == kIOReturnBusy) { |
|||
return; |
|||
} |
|||
|
|||
mach_error("I/O Kit error:", kr); |
|||
exit(kr); |
|||
} |
|||
|
|||
int main(void) { |
|||
kern_return_t kr; |
|||
io_service_t serviceObject; |
|||
CFRunLoopTimerRef updateTimer; |
|||
|
|||
serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController")); |
|||
if (!serviceObject) { |
|||
fprintf(stderr, "failed to find ambient light sensors\n"); |
|||
exit(1); |
|||
} |
|||
|
|||
kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort); |
|||
IOObjectRelease(serviceObject); |
|||
if (kr != KERN_SUCCESS) { |
|||
mach_error("IOServiceOpen:", kr); |
|||
exit(kr); |
|||
} |
|||
|
|||
setbuf(stdout, NULL); |
|||
printf("%8ld", 0L); |
|||
|
|||
updateTimer = CFRunLoopTimerCreate(kCFAllocatorDefault, |
|||
CFAbsoluteTimeGetCurrent() + updateInterval, updateInterval, |
|||
0, 0, updateTimerCallBack, NULL); |
|||
CFRunLoopAddTimer(CFRunLoopGetCurrent(), updateTimer, kCFRunLoopDefaultMode); |
|||
CFRunLoopRun(); |
|||
|
|||
exit(0); |
|||
} |
@ -0,0 +1,82 @@ |
|||
import subprocess |
|||
import shlex |
|||
from threading import Thread |
|||
from time import sleep |
|||
import pyaudio #pip3 install pyaudio |
|||
import wave |
|||
|
|||
# set desired values |
|||
start = 1 |
|||
length = 3 |
|||
|
|||
# open wave file |
|||
wave_file = wave.open('door.wav', 'rb') |
|||
|
|||
# initialize audio |
|||
py_audio = pyaudio.PyAudio() |
|||
stream = py_audio.open(format=py_audio.get_format_from_width(wave_file.getsampwidth()), |
|||
channels=wave_file.getnchannels(), |
|||
rate=wave_file.getframerate(), |
|||
output=True) |
|||
|
|||
# skip unwanted frames |
|||
n_frames = int(start * wave_file.getframerate()) |
|||
wave_file.setpos(n_frames) |
|||
|
|||
# write desired frames to audio buffer |
|||
n_frames = int(length * wave_file.getframerate()) |
|||
frames = wave_file.readframes(n_frames) |
|||
|
|||
|
|||
|
|||
def run_command(command): |
|||
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE) |
|||
while True: |
|||
output = process.stdout.readline().decode() |
|||
if output == '' and process.poll() is not None: |
|||
break |
|||
if output: |
|||
output = output.strip() |
|||
return int(output[20:]) |
|||
rc = process.poll() |
|||
return rc |
|||
|
|||
|
|||
class Worker(Thread): |
|||
def __init__(self, custom_handler): |
|||
|
|||
self.handler = custom_handler |
|||
|
|||
def run(self): |
|||
|
|||
currentValue = 0 #store values to check if ambient light is increasing or decreasing, dont do anything if no change, play from position based on scale(0-100k is mapped to 0-100% of song) |
|||
previousValue = 0 |
|||
while(True): |
|||
sleep(0.05) |
|||
#sleep(0.05) |
|||
currentValue = run_command("./lmutracker3") |
|||
if currentValue > previousValue: |
|||
print("light is increasing") |
|||
stream.write(frames) |
|||
|
|||
# close and terminate everything properly |
|||
#stream.close() |
|||
#py_audio.terminate() |
|||
#wave_file.close() |
|||
elif currentValue < previousValue: |
|||
print("light is decreasing") |
|||
else: |
|||
print("light remains the same") |
|||
previousValue = currentValue |
|||
#print(testval) |
|||
#check handler |
|||
if self.handler: |
|||
#do something |
|||
pass |
|||
handler = {} |
|||
worker = Worker(handler) |
|||
|
|||
#start thread |
|||
worker.run() |
|||
|
|||
|
Loading…
Reference in new issue