]> rtime.felk.cvut.cz Git - mf6xx.git/blobdiff - src/comedi/simple_driver/userspace/dac_example/main.c
Added Comedi userspace program for setting DACs. Tested. Working
[mf6xx.git] / src / comedi / simple_driver / userspace / dac_example / main.c
diff --git a/src/comedi/simple_driver/userspace/dac_example/main.c b/src/comedi/simple_driver/userspace/dac_example/main.c
new file mode 100644 (file)
index 0000000..7e70a41
--- /dev/null
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <comedilib.h>
+#include "../mf624.h"
+
+/* Converts Volts into format needed by card.
+Input should be integer (no float) */
+int phys_to_comedi(int val)
+{
+       if (val >= -10 && val <= 10) {
+               return 0x2000 + (val * (0x2000/10));
+       }
+       else {
+               return 0x2000; // 0V
+       }
+}
+
+int main(int argc, char* argv[])
+{
+       comedi_t* comedi_dev;
+       int status;
+       int channel = 0;
+       int range = 0;
+       int aref = 0;
+
+       comedi_dev = comedi_open("/dev/comedi0");
+       if (comedi_dev == NULL) {
+               comedi_perror("comedi_open");
+               return 1;
+       }
+
+       while(1) {
+               status = comedi_data_write(comedi_dev, MF624_AO_SUBDEV, channel, range, aref, phys_to_comedi(5));
+               if (status == -1) {
+                       comedi_perror("comedi_data_write");
+               }
+               sleep(1);
+       }
+
+       return 0;
+}