Tag Archives: code

Detecting motion: DLP-TILT 2-axis accelerometer

Using an accelerometer to assess musculoskeletal stresses

Accelerometers are helpful for characterising device usage and detecting the mode of use. Most of these devices have an internal accelerometer, but it is convenient for our purposes to use an external device (requires less programming and does not require a jailbroken device).

Setup

In order to use this device, you must first set up the serial port with a line like the following:

stty -F /dev/ttyUSB0 raw ispeed 38400 ospeed 38400 cs8 -ignpar -cstopb -echo

Code

The code probably won’t tell you all that much, but here’s one way to make your life much easier: assuming you’re running this on Linux, set your device ID as /dev/dlptilt, then change the line ‘/dev/ttyUSB0’ to read ‘/dev/dlptilt’ in the code before compiling.

#include
#include
#include
#include
#include
#include
#include
#include 

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1         //POSIX compliant source
#define FALSE 0
#define TRUE 1
#define DEBUG 0

volatile int STOP=FALSE;

void signal_handler_IO (int status);    //definition of signal handler
int wait_flag=TRUE;                     //TRUE while no signal received
char devicename[80];
long Baud_Rate = 38400;         // default Baud Rate (110 through 38400)
long BAUD;                      // derived baud rate from command line
long DATABITS;
long STOPBITS;
long PARITYON;
long PARITY;
int Data_Bits = 8;              // Number of data bits
int Stop_Bits = 1;              // Number of stop bits
int Parity = 0;                 // Parity as follows:
                  // 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space
int Format = 4;
FILE *input;
FILE *output;
int status;

main(int Parm_Count, char *Parms[])
{

   int fd, tty, c, res, i, error;
   char In1, Key;
   struct termios oldtio, newtio;
   struct termios oldkey, newkey;
   struct sigaction saio;
   char buf[255];
   char message[90];

   // set device name here
   strcpy(devicename,"/dev/ttyUSB0"); 

   newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
   newkey.c_iflag = IGNPAR;
   newkey.c_oflag = 0;
   newkey.c_lflag = 0;
   newkey.c_cc[VMIN]=1;
   newkey.c_cc[VTIME]=0;
   BAUD=38400;
   DATABITS=CS8;
   STOPBITS=CSTOPB;
   PARITYON=0;
   PARITY=0;
   fd = open(devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);
   if (fd < 0)
   {
   	perror(devicename);
   	exit(-1);
   }

   saio.sa_handler = signal_handler_IO;
   sigemptyset(&saio.sa_mask);
   saio.sa_flags = 0;
   saio.sa_restorer = NULL;
   sigaction(SIGIO,&saio,NULL);

   fcntl(fd, F_SETOWN, getpid());
   fcntl(fd, F_SETFL, FASYNC);

   tcgetattr(fd,&oldtio);
   newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
   newtio.c_iflag = IGNPAR;
   newtio.c_oflag = 0;
   newtio.c_lflag = 0;
   newtio.c_cc[VMIN]=1;
   newtio.c_cc[VTIME]=0;
   tcflush(fd, TCIFLUSH);
   tcsetattr(fd,TCSANOW,&newtio);
   write(fd,"T",1);
   while (STOP==FALSE) {
       status=1;
       if (status==1) // so redundant...
		{
	if(DEBUG){
   		write(fd,"P",1);
		printf("Printed Pn");
	} else {
		// 7 - single a d conversion to host on current settings
		// z 3 bytes (both channels x and y of accelerometer)
		// s - 7 bytes (all 7 channels)
		// 8 - streaming a/d conversion data using current settings
   		write(fd,"z",1);
	}
	} 

         if (wait_flag==FALSE)  //if output is available
	{
		res = read(fd,buf,255);
		if (res>0)
		{

			// Print serial output
			for (i=0; i

CSS3 – Better Late Than Never

CSS 1/2

Back in the year 2000, we, and by that I mean anyone who wrote web-pages, were bungling around with CSS. I was being told tables were evil, I need to separate code (content) and style and that CSS was the answer to my prayers. While the second was clearly something to aim for, it became obvious that CSS at the time wasn’t really capable of helping me achieve this. Don’t get me wrong, it was a step in the right direction and rightfully consigned laying out sites in tables as a thing of the past. We were encouraged to lay elements out as divs and most of the time it was possible to get at least most of the way into true separation between style and code, for static sites at least.

Dynamic Sites

One problem I repeatedly encountered was when rendering out lists of elements of undetermined size into a tabular style. Maybe I wanted odd elements as a different colour, or my boss might come in and want the last element in cornflower blue to match his Tuesday tie. It was mostly possible but was a pain and tied the code into the visuals in several places.

CSS3 to the rescue

Finally eleven years later CSS3 comes to the rescue with some fancy new selectors (there were a few in CSS2). Here are some good ones I have used already.

This will add ‘>>’ at the end of any external links automatically. Repeat for https if required.

a[href^="http://"]:not([href*="www.domain.com"]):after { 
    content: " >>"; 
}

This will highlight any links that open in a new window.

a[target^="_blank"] { 
    color:#855; 
}

 

It also allows you to write code that is almost (other than the class name) completely detached from the visuals. This lays out divs in columns of three.

/*Loop in your language of choice*/ {
Content
}
.splitThird {height: auto; clear: both; }

.splitThird div:nth-child(3n+1) {
    float:left; width: 33%; clear:left; margin-bottom: 20px;
}

.splitThird div:nth-child(3n+2) {
    float: left; width: 33%; clear: none;
}

.splitThird div:nth-child(3n+3){
    float: right; width: 33%;
}