Fixed error handling for client lib
Changed Makefile to link against twinkclient.so Update matrix.py to use client lib
This commit is contained in:
parent
f3911bfdf9
commit
217c511eb0
2
Makefile
2
Makefile
|
@ -25,7 +25,7 @@ $(OBJDIR)/%.o : %.c Makefile $(HEADERS)
|
||||||
$(OBJDIR)/twinklclient.so : $(OBJDIR)/twinklsocket.o $(OBJDIR)/message_wrapper.o
|
$(OBJDIR)/twinklclient.so : $(OBJDIR)/twinklsocket.o $(OBJDIR)/message_wrapper.o
|
||||||
$(CC) $+ -shared $(LDFLAGS) -o $@
|
$(CC) $+ -shared $(LDFLAGS) -o $@
|
||||||
|
|
||||||
$(OBJDIR)/twinkl-client : $(OBJDIR)/main.o $(OBJDIR)/twinklsocket.o
|
$(OBJDIR)/twinkl-client : $(OBJDIR)/main.o $(OBJDIR)/twinklclient.so
|
||||||
$(CC) $+ $(LDFLAGS) -o $@
|
$(CC) $+ $(LDFLAGS) -o $@
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
|
|
24
README.md
24
README.md
|
@ -49,9 +49,9 @@ They can be connected to a twinkle-client process using a pipe.
|
||||||
**Examples:**
|
**Examples:**
|
||||||
|
|
||||||
```
|
```
|
||||||
python2 fullwithe.py | ../bin/twinkl-client lightwall.lan 7
|
|
||||||
python2 gradient.py | ../bin/twinkl-client lightwall.lan 7
|
python2 gradient.py | ../bin/twinkl-client lightwall.lan 7
|
||||||
python2 matrix.py | ../bin/twinkl-client lightwall.lan 7
|
python2 fullwithe.py lightwall.lan 7
|
||||||
|
python2 matrix.py lightwall.lan 7
|
||||||
|
|
||||||
python2 random.py | ../bin/twinkl-client ampel.lan 7
|
python2 random.py | ../bin/twinkl-client ampel.lan 7
|
||||||
```
|
```
|
||||||
|
@ -67,11 +67,11 @@ It offers the following functions:
|
||||||
* Functions for handling sockets
|
* Functions for handling sockets
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Opens a 'connected' upd socket for this host and port, returns the filedescriptor
|
// Opens a 'connected' upd socket for this host and port. Returns the filedescriptor or -1 on error.
|
||||||
int twinklsocket_open(const char *host, const char *port);
|
int twinklsocket_open(const char *host, const char *port);
|
||||||
|
|
||||||
// Sends a twinkl message
|
// Sends a twinkl message. Returns 0 on success else -1.
|
||||||
void twinklsocket_send(int sockfd, const struct twinkl_message *message);
|
int twinklsocket_send(int sockfd, const struct twinkl_message *message);
|
||||||
|
|
||||||
// Closes the socket
|
// Closes the socket
|
||||||
void twinklsocket_close(int sockfd);
|
void twinklsocket_close(int sockfd);
|
||||||
|
@ -119,12 +119,15 @@ Using the wrapper functions it is only necessary to deal with integers and point
|
||||||
See `animations/twinklclient.py` and the example below for details.
|
See `animations/twinklclient.py` and the example below for details.
|
||||||
|
|
||||||
```C
|
```C
|
||||||
twinkl_server = "127.0.0.1"
|
twinkl_server = "127.0.0.1";
|
||||||
port = "1337";
|
port = "1337";
|
||||||
|
|
||||||
fd = twinklsocket_open(twinkl_server, port)
|
fd = twinklsocket_open(twinkl_server, port);
|
||||||
|
if(fd == -1) {
|
||||||
|
PANIC();
|
||||||
|
}
|
||||||
|
|
||||||
msg = twinklmsg_create()
|
msg = twinklmsg_create();
|
||||||
|
|
||||||
twinklmsg_set_priority(msg, 0);
|
twinklmsg_set_priority(msg, 0);
|
||||||
|
|
||||||
|
@ -132,7 +135,10 @@ twinklmsg_set_value(msg, 23, 42);
|
||||||
twinklmsg_set_value(msg, 46, 5);
|
twinklmsg_set_value(msg, 46, 5);
|
||||||
// ... more stuff
|
// ... more stuff
|
||||||
|
|
||||||
twinklsocket_send(fd, msg);
|
result = twinklsocket_send(fd, msg);
|
||||||
|
if(result == -1) {
|
||||||
|
PANIC();
|
||||||
|
}
|
||||||
|
|
||||||
// Tear down
|
// Tear down
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import signal
|
||||||
|
|
||||||
from random import randint
|
from random import randint
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from subprocess import Popen, PIPE
|
|
||||||
|
from twinklclient import TwinklSocket, TwinklMessage
|
||||||
|
|
||||||
WIDTH = 6
|
WIDTH = 6
|
||||||
HEIGHT = 8
|
HEIGHT = 8
|
||||||
|
@ -20,21 +24,16 @@ BOX_MAP = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
channels = {}
|
msg = TwinklMessage()
|
||||||
|
socket = None
|
||||||
|
priority = 0
|
||||||
|
|
||||||
def set_box(x,y,r,g,b):
|
def set_box(x,y,r,g,b):
|
||||||
if x >= 0 and y >= 0 and x < WIDTH and y < HEIGHT:
|
if x >= 0 and y >= 0 and x < WIDTH and y < HEIGHT:
|
||||||
base_address = BOX_MAP[y][x]
|
base_address = BOX_MAP[y][x]
|
||||||
channels[base_address] = r
|
msg[base_address] = int(r)
|
||||||
channels[base_address + 1] = g
|
msg[base_address + 1] = int(g)
|
||||||
channels[base_address + 2] = b
|
msg[base_address + 2] = int(b)
|
||||||
|
|
||||||
|
|
||||||
def output_channels():
|
|
||||||
for channel, value in channels.items():
|
|
||||||
print "%d : %d" % (channel, value)
|
|
||||||
|
|
||||||
print ""
|
|
||||||
|
|
||||||
|
|
||||||
def clear():
|
def clear():
|
||||||
|
@ -64,6 +63,29 @@ class Column(object):
|
||||||
self.y += 1
|
self.y += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def terminate(signal, frame):
|
||||||
|
msg.reset()
|
||||||
|
msg.set_priority(priority)
|
||||||
|
socket.send(msg)
|
||||||
|
|
||||||
|
if socket:
|
||||||
|
socket.close()
|
||||||
|
msg.destroy()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, terminate)
|
||||||
|
|
||||||
|
if len(sys.argv) != 3:
|
||||||
|
print "Usage: %s host priority" % sys.argv[0]
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
socket = TwinklSocket(sys.argv[1], "1337")
|
||||||
|
|
||||||
|
priority = int(sys.argv[2])
|
||||||
|
msg.set_priority(priority)
|
||||||
|
|
||||||
columns = []
|
columns = []
|
||||||
# Add some initial collums
|
# Add some initial collums
|
||||||
for i in range(0, 16):
|
for i in range(0, 16):
|
||||||
|
@ -81,5 +103,5 @@ while(True):
|
||||||
columns[i].update()
|
columns[i].update()
|
||||||
columns[i].render()
|
columns[i].render()
|
||||||
|
|
||||||
output_channels()
|
socket.send(msg)
|
||||||
sleep(0.050)
|
sleep(0.05)
|
||||||
|
|
|
@ -29,18 +29,23 @@ _TWINKL_CLIENT.twinklsocket_open.argtypes = [c_char_p, c_char_p]
|
||||||
_TWINKL_CLIENT.twinklsocket_open.restype = c_int
|
_TWINKL_CLIENT.twinklsocket_open.restype = c_int
|
||||||
|
|
||||||
_TWINKL_CLIENT.twinklsocket_send.argtypes = [c_int, c_void_p]
|
_TWINKL_CLIENT.twinklsocket_send.argtypes = [c_int, c_void_p]
|
||||||
_TWINKL_CLIENT.twinklsocket_send.restype = None
|
_TWINKL_CLIENT.twinklsocket_send.restype = c_int
|
||||||
|
|
||||||
|
|
||||||
class TwinklSocket(object):
|
class TwinklSocket(object):
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port):
|
||||||
self._socket = _TWINKL_CLIENT.twinklsocket_open(host, port)
|
self._socket = _TWINKL_CLIENT.twinklsocket_open(host, port)
|
||||||
|
if self._socket < 0:
|
||||||
|
print self._socket
|
||||||
|
raise RuntimeError("Could not open socket.")
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
_TWINKL_CLIENT.twinklsocket_close(self._socket)
|
_TWINKL_CLIENT.twinklsocket_close(self._socket)
|
||||||
|
|
||||||
def send(self, msg):
|
def send(self, msg):
|
||||||
_TWINKL_CLIENT.twinklsocket_send(self._socket, msg._pointer)
|
result = _TWINKL_CLIENT.twinklsocket_send(self._socket, msg._pointer)
|
||||||
|
if result < 0:
|
||||||
|
raise RuntimeError("Could not send packet")
|
||||||
|
|
||||||
|
|
||||||
class TwinklMessage(object):
|
class TwinklMessage(object):
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
int twinklsocket_open(const char *host, const char *port);
|
int twinklsocket_open(const char *host, const char *port);
|
||||||
|
|
||||||
void twinklsocket_send(int sockfd, const struct twinkl_message *message);
|
int twinklsocket_send(int sockfd, const struct twinkl_message *message);
|
||||||
|
|
||||||
void twinklsocket_close(int sockfd);
|
void twinklsocket_close(int sockfd);
|
||||||
|
|
||||||
|
|
11
main.c
11
main.c
|
@ -27,6 +27,9 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
twinklsocket = twinklsocket_open(argv[1], port);
|
twinklsocket = twinklsocket_open(argv[1], port);
|
||||||
|
if(twinklsocket == -1) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
struct twinkl_message msg;
|
struct twinkl_message msg;
|
||||||
|
|
||||||
|
@ -47,14 +50,18 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Line format: <channel> : <value>
|
* Line format: <channel> : <value>
|
||||||
* With abitrary many spaces btween the numbers and the colon
|
* With arbitrary many spaces between the numbers and the colon
|
||||||
* Example: "5 : 42"
|
* Example: "5 : 42"
|
||||||
*/
|
*/
|
||||||
while(!feof(stdin)) {
|
while(!feof(stdin)) {
|
||||||
|
|
||||||
// Try to read a line from stdin, if the line is empty send the packet
|
// Try to read a line from stdin, if the line is empty send the packet
|
||||||
if(getline(&line, &len, stdin) <= 1) {
|
if(getline(&line, &len, stdin) <= 1) {
|
||||||
twinklsocket_send(twinklsocket, &msg);
|
int result = twinklsocket_send(twinklsocket, &msg);
|
||||||
|
if(result != 0) {
|
||||||
|
printf("There was an error while sending the twinkl packet.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
printf("Twinkl paket sent.\n");
|
printf("Twinkl paket sent.\n");
|
||||||
|
|
||||||
twinkl_init_message(&msg);
|
twinkl_init_message(&msg);
|
||||||
|
|
|
@ -24,7 +24,7 @@ int twinklsocket_open(const char *host, const char *port) {
|
||||||
result = getaddrinfo(host, port, &hints, &servinfo);
|
result = getaddrinfo(host, port, &hints, &servinfo);
|
||||||
if(result != 0) {
|
if(result != 0) {
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop through all the results and make a socket
|
// loop through all the results and make a socket
|
||||||
|
@ -37,13 +37,13 @@ int twinklsocket_open(const char *host, const char *port) {
|
||||||
|
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
fprintf(stderr, "failed to create socket\n");
|
fprintf(stderr, "failed to create socket\n");
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = connect(sockfd, p->ai_addr, p->ai_addrlen);
|
result = connect(sockfd, p->ai_addr, p->ai_addrlen);
|
||||||
if(result != 0) {
|
if(result != 0) {
|
||||||
perror("connect");
|
perror("connect");
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(servinfo);
|
freeaddrinfo(servinfo);
|
||||||
|
@ -51,14 +51,16 @@ int twinklsocket_open(const char *host, const char *port) {
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
void twinklsocket_send(int sockfd, const struct twinkl_message *message) {
|
int twinklsocket_send(int sockfd, const struct twinkl_message *message) {
|
||||||
int numbytes;
|
int numbytes;
|
||||||
|
|
||||||
numbytes = send(sockfd, message, sizeof(struct twinkl_message), 0);
|
numbytes = send(sockfd, message, sizeof(struct twinkl_message), 0);
|
||||||
if (numbytes == -1) {
|
if (numbytes == -1) {
|
||||||
perror("sendto");
|
perror("sendto");
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void twinklsocket_close(int sockfd) {
|
void twinklsocket_close(int sockfd) {
|
||||||
|
|
Loading…
Reference in New Issue