summaryrefslogtreecommitdiffstats
path: root/Usb-Driver/test-mocca.c
diff options
context:
space:
mode:
Diffstat (limited to 'Usb-Driver/test-mocca.c')
-rw-r--r--Usb-Driver/test-mocca.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/Usb-Driver/test-mocca.c b/Usb-Driver/test-mocca.c
new file mode 100644
index 0000000..70c4760
--- /dev/null
+++ b/Usb-Driver/test-mocca.c
@@ -0,0 +1,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;
+}