Commits

iorodeo committed a46f75e

Added serial interface to enable users to set the frame dt. Added matlab class
PanelsNano enabling using to communicate with the controller from Matlab.

Comments (0)

Files changed (3)

 *.pkl
 *.dxf
 *.komodo*
+*.asv
 *.egg-info
 .DS_Store
 build

matlab_serial_interface/PanelsNano.m

+% PanelsNano.m 
+%
+% Provides a simple interface to the Arduino Nano based panels controller
+% enabling the user to set the frame dt in ms. 
+%
+% Usage:
+%  
+%  dev = PanelsNano('com5')
+%  dev.open()
+%  dev.setFrameDt(100)  % Set frame dt to 100ms
+%  dev.close()
+%
+% Author: Will Dickson, IO Rodeo Inc.
+% -------------------------------------------------------------------------
+
+classdef  PanelsNano < handle
+    
+    properties
+        dev = [];    
+    end
+    
+    properties (Constant)
+        % Serial communications parameters
+        baudrate = 9600;
+        databits = 8;
+        stopbits = 1;
+        timeout = 0.5;
+        openDelay = 2.0;
+    end
+    
+    methods
+        
+        function self = PanelsNano(port)
+            % Constructor
+            if nargin ~= 1
+                error('Usage: self = PanelsNano(port)');
+            end
+            self.dev = serial( ...
+                port, ...
+                'baudrate', self.baudrate, ...
+                'databits', self.databits, ...
+                'stopbits', self.stopbits, ...
+                'timeout',  self.timeout ...
+                );
+        end
+        
+        function open(self)
+            % Opens a serial connection to panels controller
+            fopen(self.dev);
+            pause(self.openDelay);
+        end
+        
+        function close(self)
+            % Closes the seraial connection to the panaels controller
+            fclose(self.dev);
+        end
+         
+        function setFrameDt(self,dt)
+            % Set the frame dt in ms 
+            if (dt < 1) || (dt > 255)
+                error('frame dt must be between 1 and 255');
+            end
+            fwrite(self.dev, char(dt));    
+        end
+      
+    end
+    
+end

panels_controller_nano_firmware.pde

  * Author: Peter Polidoro, IO Rodeo Inc. 02/16/2011
  *******************************************************************************/
 #include <Wire.h>
+#include <util/atomic.h>
 
 #define GENERAL_CALL_ADDRESS       0
 #define INITIAL_PATTERN_BYTE_COUNT 8
 #define PATTERN_FRAME_COUNT        4
-#define PATTERN_FRAME_DELAY        200
+// Set up timer 2 w/ 1kHz overflow
+#define TIMER_TCCR2A 0x03
+#define TIMER_TCCR2B 0x0C
+#define TIMER_OCR2A 249
+#define DFLT_MAX_DELAY_CNT 200
 
+boolean sendFrame = false;
+uint8_t delayCnt = 0;
+uint8_t maxDelayCnt = DFLT_MAX_DELAY_CNT;
+uint8_t frame_n = 0;
 unsigned char INITIAL_PATTERN[INITIAL_PATTERN_BYTE_COUNT] =              {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
 unsigned char ALL_OFF_PATTERN[INITIAL_PATTERN_BYTE_COUNT] =              {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 unsigned char ALL_ON_PATTERN[INITIAL_PATTERN_BYTE_COUNT] =               {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 unsigned char PATTERN[PATTERN_FRAME_COUNT][INITIAL_PATTERN_BYTE_COUNT] = {{0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33},
-                                                                          {0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99},
-                                                                          {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC},
-                                                                          {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}};
+                                                                         {0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99},
+                                                                         {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC},
+                                                                         {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}};
 
 /*******************************************************************************
  * setup
  *
  *******************************************************************************/
 void setup() {
+  // Setup serial communications
+  Serial.begin(9600);
+
   Wire.begin(); // join i2c bus (address optional for master)
   Wire.beginTransmission(GENERAL_CALL_ADDRESS); // transmit to all devices
   Wire.send(INITIAL_PATTERN,INITIAL_PATTERN_BYTE_COUNT);
   Wire.endTransmission();    // stop transmitting
-  delay(3000);
+
+  // Initialize timer 2 
+  TCCR2A = TIMER_TCCR2A;
+  TCCR2B = TIMER_TCCR2B;
+  OCR2A = TIMER_OCR2A;
+  // Timer 2 overflow interrupt enable 
+  TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
+  TCNT2 = 0;
 }
 
 /*******************************************************************************
  * loop
  *
  *******************************************************************************/
-int frame_n = 0;
 void loop() {
-  Wire.beginTransmission(GENERAL_CALL_ADDRESS); // transmit to all devices
-  Wire.send(PATTERN[frame_n],INITIAL_PATTERN_BYTE_COUNT);
-  Wire.endTransmission();    // stop transmitting
-  delay(PATTERN_FRAME_DELAY);
-  frame_n = (frame_n + 1) % PATTERN_FRAME_COUNT;
+  while (Serial.available() > 0) {
+    uint8_t byte = (uint8_t) Serial.read();
+    if (byte > 0) {
+      ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+        maxDelayCnt = byte;
+        delayCnt = 0;
+      }
+    }
+  }
+  if (sendFrame == true) {
+    Serial.print(frame_n,DEC);
+    Serial.print(", ");
+    Serial.println(maxDelayCnt,DEC);
+    Wire.beginTransmission(GENERAL_CALL_ADDRESS); // transmit to all devices
+    Wire.send(PATTERN[frame_n],INITIAL_PATTERN_BYTE_COUNT);
+    Wire.endTransmission();    // stop transmitting
+    frame_n = (frame_n + 1) % PATTERN_FRAME_COUNT;
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 
+      sendFrame = false;
+    }
+  }
 }
+
+
+/******************************************************************************
+ * Timer 2 overflow interrupt service routine
+ *
+ ******************************************************************************/
+ISR(TIMER2_OVF_vect) {
+  delayCnt++;
+  if (delayCnt >= maxDelayCnt) {
+    delayCnt = 0;
+    sendFrame = true;
+  }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.