first commit

This commit is contained in:
Eddy
2014-10-10 17:36:02 +04:00
commit 1a63497f04
62 changed files with 9055 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
PROGRAM = websocktest
LDFLAGS = $(shell pkg-config --libs libwebsockets)
SRCS = test.c
CC = gcc
DEFINES = -D_XOPEN_SOURCE=501 -DCUR_PATH=\"$(shell pwd)\"
CXX = gcc
CFLAGS = -Wall -Werror -Wextra $(DEFINES) $(shell pkg-config --cflags libwebsockets)
OBJS = $(SRCS:.c=.o)
all : $(PROGRAM) clean
$(PROGRAM) : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $(PROGRAM)
# some addition dependencies
# %.o: %.c
# $(CC) $(LDFLAGS) $(CFLAGS) $< -o $@
#$(SRCS) : %.c : %.h $(INDEPENDENT_HEADERS)
# @touch $@
clean:
/bin/rm -f *.o *~
depend:
$(CXX) -MM $(CXX.SRCS)

View File

@@ -0,0 +1,183 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#include <libwebsockets.h>
#include <termios.h> // tcsetattr
#include <stdint.h> // int types
#include <sys/stat.h> // read
#include <fcntl.h> // read
#define _U_ __attribute__((__unused__))
int force_exit = 0;
uint8_t buf[9];
char *comdev = "/dev/ttyUSB0";
int BAUD_RATE = B115200;
int comfd = -1; // TTY fd
int send_command(uint8_t *ninebytes){
uint8_t crc = 0;
int i;
printf("send: ");
for(i = 0; i < 8; crc += ninebytes[i++])
printf("%u,", ninebytes[i]);
ninebytes[8] = crc;
printf("%u\n",ninebytes[8]);
if(9 != write(comfd, ninebytes, 9)){
perror("Can't write to Trinamic");
return 0;
}
return 1;
}
void fillbuf(char *command){
memset(buf, 0, 9);
buf[0] = 1; // controller #
if(command[1] == 'X') buf[3] = 2; // X motor -> #2
else buf[3] = 0; // Y motor -> #0
if(command[0] == 'D'){ // start moving
if(command[2] == '+') buf[1] = 1; // ROR
else buf[1] = 2; // ROL
}else{ // stop
buf[1] = 3; // STP
}
buf[7] = 50; // speed = 50
send_command(buf);
}
void move_motor(char *where){
printf("moving %s\n", where);
}
void stop_motor(char *where){
printf("stoping %s\n", where);
}
static int
my_protocol_callback(_U_ struct libwebsocket_context *context,
_U_ struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
_U_ void *user, void *in, _U_ size_t len)
{
//int n, m;
//unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
// LWS_SEND_BUFFER_POST_PADDING];
//unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
char *msg = (char*) in;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
printf("New Connection\n");
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
break;
case LWS_CALLBACK_RECEIVE:
fillbuf(msg);
break;
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
break;
default:
break;
}
return 0;
}
//**************************************************************************//
/* list of supported protocols and callbacks */
//**************************************************************************//
static struct libwebsocket_protocols protocols[] = {
/* first protocol must always be HTTP handler */
{
"XY-protocol", // name
my_protocol_callback, // callback
30, // per_session_data_size
10, // max frame size / rx buffer
0, NULL, 0
},
{ NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
};
//**************************************************************************//
void sighandler(_U_ int sig){
close(comfd);
force_exit = 1;
}
//**************************************************************************//
//**************************************************************************//
int main(_U_ int argc, _U_ char **argv)
{
int n = 0;
struct libwebsocket_context *context;
int opts = 0;
const char *iface = NULL;
int syslog_options = LOG_PID | LOG_PERROR;
//unsigned int oldus = 0;
struct lws_context_creation_info info;
int debug_level = 7;
memset(&info, 0, sizeof info);
info.port = 9999;
signal(SIGINT, sighandler);
printf("\nOpen port...\n");
if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){
fprintf(stderr,"Can't use port %s\n",comdev);
return 1;
}
/* we will only try to log things according to our debug_level */
setlogmask(LOG_UPTO (LOG_DEBUG));
openlog("lwsts", syslog_options, LOG_DAEMON);
/* tell the library what debug level to emit and to send it to syslog */
lws_set_log_level(debug_level, lwsl_emit_syslog);
info.iface = iface;
info.protocols = protocols;
info.extensions = libwebsocket_get_internal_extensions();
info.ssl_cert_filepath = NULL;
info.ssl_private_key_filepath = NULL;
info.gid = -1;
info.uid = -1;
info.options = opts;
context = libwebsocket_create_context(&info);
if (context == NULL) {
lwsl_err("libwebsocket init failed\n");
return -1;
}
n = 0;
while (n >= 0 && !force_exit) {
n = libwebsocket_service(context, 50);
};//while n>=0
libwebsocket_context_destroy(context);
lwsl_notice("libwebsockets-test-server exited cleanly\n");
closelog();
return 0;
}//main

View File

@@ -0,0 +1,106 @@
<html>
<head>
<meta charset=koi8-r http-equiv="Content-Language" content="ru"/>
<title>A test</title>
<style type="text/css">
.content { vertical-align:top; text-align:center; background:#fffff0; padding:12px; border-radius:10px; }
button { display: block; width: 70px; text-align: center; }
.container {border: solid 1px; border-radius:10px; padding:12px; }
</style>
</head>
<body onload="Global.init()">
<div class="container" id="cont">
<table class="content" id="tab">
<tr><td></td>
<td align='center'><button id='Y+'>Y+</button></td>
<td></td></tr>
<tr>
<td align='center'><button id='X-'>X-</button></td>
<td></td>
<td align='center'><button id='X+'>X+</button></td>
</tr>
<tr><td></td>
<td align='center'><button id='Y-'>Y-</button></td>
<td></td></tr>
</table>
<p></p>
<div id = "connected">No connection</div>
<div id = "answer"></div>
</div>
<script>
Global = function(){
var socket = null;
var connected = 0;
function get_appropriate_ws_url(){
var pcol;
var u = document.URL;
if (u.substring(0, 5) == "https") {
pcol = "wss://";
u = u.substr(8);
} else {
pcol = "ws://";
if (u.substring(0, 4) == "http")
u = u.substr(7);
}
u = u.split('/');
return pcol + u[0] + ":9999";
}
function TryConnect(){
if(connected) return;
if(socket) delete socket;
if (typeof MozWebSocket != "undefined") {
socket = new MozWebSocket(get_appropriate_ws_url(),
"XY-protocol");
} else {
socket = new WebSocket(get_appropriate_ws_url(),
"XY-protocol");
}
try {
socket.onopen = function(){
document.getElementById("connected").style.backgroundColor = "#40ff40";
document.getElementById("connected").textContent = "Connection opened";
connected = 1;
}
socket.onmessage = function got_packet(msg) {
document.getElementById("answer").textContent = msg.data + "\n";
}
socket.onclose = function(){
document.getElementById("connected").style.backgroundColor = "#ff4040";
document.getElementById("connected").textContent = "Connection closed";
document.getElementById("answer").textContent = "";
connected = 0;
setTimeout(TryConnect, 1000);
}
} catch(exception) {
alert('Error' + exception);
}
}
function init(){
console.log("init");
document.getElementById("cont").style.width = document.getElementById("tab").clientWidth;
var Buttons = document.getElementsByTagName("button");
for(var i = 0; i < Buttons.length; i++){
//Buttons[i].addEventListener("click", btnclick);
Buttons[i].addEventListener("mousedown", btnmousedown);
Buttons[i].addEventListener("mouseup", btnmouseup);
}
TryConnect();
}
/*function btnclick(){
console.log("Click: " + this.id);
}*/
function btnmouseup(){
console.log("Mouse up: " + this.id);
if(connected) socket.send("U"+this.id);
}
function btnmousedown(){
console.log("Mouse down: " + this.id);
if(connected) socket.send("D"+this.id);
}
return{
init: init
}
}();
</script>
</body>
</html>