1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
uint8_t relayOn[] = {
0x04, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x13,
0x02, 0x0b, 0x70, 0x07, 0x00, 0x00, 0x01, 0x00
};
uint8_t relayOff[] = {
0x04, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x13,
0x02, 0x0b, 0x70, 0x07, 0x00, 0x00, 0x00, 0x00
};
uint8_t readBuffer[1024];
static int32_t repeat = 10;
static pthread_t readingThread;
static bool allowRun = true;
static int fd = -1;
void* executeReading ( void* arg )
{
uint32_t bytesRead;
uint32_t i;
while ( allowRun ) {
bytesRead = read ( fd, readBuffer, sizeof ( readBuffer ) );
if ( bytesRead > 0 ) {
printf ( "Received bytes (hex): " );
for ( i = 0; i < bytesRead; i++ ) {
printf ( "%02X ", readBuffer[i] );
}
printf ( "\n" );
}
}
pthread_exit ( 0 );
}
int main ( int argc, char* argv[] )
{
int ret;
if ( ( fd = open ( "/dev/mocca0", O_RDWR|O_SYNC ) ) < 0 ) {
printf ( "Failed to open mocca0 device\n" );
exit ( -4 );
}
ret = pthread_create ( &readingThread, NULL, executeReading, NULL );
if ( ret ) {
printf ( "Failed to start reader thread\n" );
} else {
while ( --repeat >= 0 ) {
write ( fd, relayOn, sizeof ( relayOn ) );
usleep ( 1000000 );
write ( fd, relayOff, sizeof ( relayOff ) );
usleep ( 1000000 );
}
}
allowRun = false;
close ( fd );
return 0;
}
|