Compare commits

..

3 commits

Author SHA1 Message Date
7eb81404f0 Added gitignore file 2018-04-06 13:08:08 -07:00
d1ffb76141 Moved python service file. 2018-04-06 13:07:49 -07:00
8bfc406e85 First attempt at a service with python 2018-04-06 13:04:59 -07:00
5 changed files with 152 additions and 5 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
*.o
/build

67
main.js Normal file
View file

@ -0,0 +1,67 @@
const GPIO = require("onoff").Gpio;
const WS = require("ws");
const HEARTBEAT_INT = 400;
const button = new GPIO(4, "in", "both", {debounceTimeout: 30, activeLow: true});
const ledOnAir = new GPIO(24, "low");
const ledReady = new GPIO(23, "low");
const ws = new WS('ws://10.0.0.4:2000');
let currentID = 0;
let heartBeat;
ws.on('open', () => {
button.watch((e, value) => {
if(e) {
console.error("Error on button watch:", e);
return;
}
if(value) {
ws.send(JSON.stringify({type: 'button-press'}));
} else {
ws.send(JSON.stringify({type: 'button-release'}));
}
ledOnAir.write(value, (e) => e && console.error("Error writing to led: ", e));
});
heartBeat = setInterval(() => {
button.read((e, buttonState) => {
console.log("Button state!", buttonState);
if(e) {
return console.error("Error reading button state!", e);
}
ws.send(JSON.stringify({
type: 'button-update',
body: {buttonState}
}));
});
}, 400);
ledReady.write(1, console.error);
});
ws.on('error', (e) => {
console.error(e);
cleanup();
process.exit();
});
function cleanup() {
ledOnAir.unexport();
ledReady.unexport();
button.unexport();
ws.close();
if(heartBeat) {
cancelInterval(heartBeat);
heartBeat = null;
}
}
process.on('SIGINT', () => {
cleanup();
});

15
main.py
View file

@ -2,15 +2,21 @@
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
CONNECT_TO = ("10.0.0.4", 2000)
def buttonEventHandler(value):
print "buton event!"
if value is 0:
GPIO.output(24, True)
sock.sendall("button_press")
print('Button Pressed...')
else:
GPIO.output(24, False)
sock.sendall("button_release")
print('Button Released...')
def main():
@ -23,12 +29,15 @@ def main():
#GPIO.add_event_detect(23, GPIO.FALLING)
#GPIO.add_event_callback(23, buttonEventHandler)
try:
sock.connect(CONNECT_TO)
while True:
GPIO.wait_for_edge(23, GPIO.BOTH)
GPIO.wait_for_edge(23, GPIO.BOTH, bouncetime=100)
buttonEventHandler(GPIO.input(23))
except KeyboardInterrupt as e:
except:
GPIO.cleanup()
sock.close()
raise
if __name__== "__main__":
main()

65
miccontrol.c Normal file
View file

@ -0,0 +1,65 @@
/*
* miccontrol.c
* Copyright (C) 2018 jshaver <jshaver@je-laptop>
*
* Distributed under terms of the MIT license.
*/
#include "stdio.h"
#include "pulse/pulseaudio.h"
const char *NAME = "miccontrol";
static pa_context *context = NULL;
static pa_mainloop_api *mainloop_api = NULL;
static pa_mainloop *mainloop = NULL;
void querySources(pa_context *c, void *userdata);
void handleSourceList(struct pa_context *c, const pa_source_info *i, int eol, void *userdata);
int main() {
int ret = 1;
mainloop = pa_mainloop_new();
mainloop_api = pa_mainloop_get_api(mainloop);
context = pa_context_new(mainloop_api, NULL);
pa_context_set_state_callback(context, querySources, NULL);
pa_context_connect(context, NULL, 0, NULL);
//pa_operation_unref(getSourceOp);
pa_mainloop_run(mainloop, &ret);
pa_context_unref(context);
pa_mainloop_free(mainloop);
return ret;
}
void querySources(pa_context *c, void *userdata) {
if(pa_context_get_state(context) != PA_CONTEXT_READY) {
return;
}
pa_operation *getSourceOp = NULL;
getSourceOp = pa_context_get_source_info_list(context, handleSourceList, NULL);
if(!getSourceOp) {
} else {
pa_operation_state_t state = pa_operation_get_state(getSourceOp);
switch(state) {
case PA_OPERATION_RUNNING:
break;
case PA_OPERATION_DONE:
break;
case PA_OPERATION_CANCELLED:
}
}
}
void handleSourceList(pa_context *c, const pa_source_info *i, int eol, void *userdata) {
printf("Callback called...\n");
printf("eol is %d\n", eol);
printf(i->description);
printf("\n");
return;
}

View file

@ -14,13 +14,16 @@ def main():
sourceNumber = getSourceSelection(sources)
server = SocketServer.TCPServer(LISTEN_ON, RequestHandler)
try:
server = SocketServer.TCPServer(LISTEN_ON, RequestHandler)
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
server.server_close()
except:
server.shutdown()
server.server_close()
def getSourceSelection(sources):
@ -58,7 +61,7 @@ class RequestHandler(SocketServer.StreamRequestHandler):
message = line.strip()
if message in messageTypes:
messageTypes[message]()
self.wfile.write(message)
print("message: ".message)
print("Closed!")
self.request.close()