chiark / gitweb /
Quite a few changes here.
[wiringPi.git] / examples / gertboard.c
index 8344d482a951b0e591dcaa9cb18826ceddc6fe41..f02e27db5eacfba5ae026a4b64523aa19fb48403 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * gertboard.c:
  *     Simple test for the SPI bus on the Gertboard
  *     copy this value to D/A port 1 and use a 'scope on both D/A ports
  *     to check all's well.
  *
+ * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
+ ***********************************************************************
+ * This file is part of wiringPi:
+ *     https://projects.drogon.net/raspberry-pi/wiringpi/
+ *
+ *    wiringPi is free software: you can redistribute it and/or modify
+ *    it under the terms of the GNU Lesser General Public License as published by
+ *    the Free Software Foundation, either version 3 of the License, or
+ *    (at your option) any later version.
+ *
+ *    wiringPi is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU Lesser General Public License for more details.
+ *
+ *    You should have received a copy of the GNU Lesser General Public License
+ *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
+ ***********************************************************************
  */
 
 #include <stdio.h>
 #include <stdint.h>
-//#include <stdlib.h>
 #include <math.h>
 
+#define        B_SIZE  200
+#undef DO_TIMING
+
 #include <wiringPi.h>
 #include <gertboard.h>
 
 int main (void)
 {
-  int    angle ;
-  int      h1 ;
+  double angle ;
+  int i ;
   uint32_t x1 ;
+  int  buffer [B_SIZE] ;
+
+#ifdef DO_TIMING
+  unsigned int now, then ;
+#endif
 
   printf ("Raspberry Pi Gertboard SPI test program\n") ;
 
-  if (gertboardSPISetup () == -1)
+  if (wiringPiSetupSys () < 0)
+    return -1 ;
+
+  if (gertboardSPISetup () < 0)
     return 1 ;
 
+// Generate a Sine Wave
+
+  for (i = 0 ; i < B_SIZE ; ++i)
+  {
+    angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
+    buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
+  }
+
+
   for (;;)
   {
-    for (angle = 0 ; angle < 360 ; ++angle)
+#ifdef DO_TIMING
+    then = millis () ;
+#endif
+
+    for (i = 0 ; i < B_SIZE ; ++i)
     {
-      h1 = (int)rint (sin ((double)angle * M_PI / 180.0) * 127.0 + 128.0) ;
-      gertboardAnalogWrite (0, h1) ;
+      gertboardAnalogWrite (0, buffer [i]) ;
 
+#ifndef        DO_TIMING
       x1 = gertboardAnalogRead (0) ;
       gertboardAnalogWrite (1, x1 >> 2) ;      // 10-bit A/D, 8-bit D/A
+#endif
     }
+
+#ifdef DO_TIMING
+    now = millis () ;
+    printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ;
+    printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ;
+#endif
   }
 
   return 0 ;