|
|
@@ -0,0 +1,346 @@
|
|
|
+/* P5_Automation_can-dev
|
|
|
+ * 2020-06 testing communications CAN for integrate further with Procesy5
|
|
|
+ * A.Binder
|
|
|
+ * Uses:
|
|
|
+ * LCD1602 - big 2x16 character display
|
|
|
+ * TFT_ILI9163C 128x128 lcd display
|
|
|
+ * MCP_2515 - can port controller
|
|
|
+ *
|
|
|
+ * controls
|
|
|
+ * Slave ports via:
|
|
|
+ * I2C
|
|
|
+ * ]PCA9685 - 16port 12bit PWM driver - can controll 16 eg. LEDs which can be 0....12V dimmed=]
|
|
|
+ * or 16 servos
|
|
|
+ * - can have addressed many of PCA9685 on I2C bus - so can controll over 64 ports
|
|
|
+ * to be added another devices tested
|
|
|
+ * TODO structure of port is in _Slave_Ports.h
|
|
|
+ * _Slave_Port _Slave_Ports[] = {
|
|
|
+ {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 },
|
|
|
+ {"CAN", 0x102, 0x80, 0x00, 1, 0xFF, 0x20 } //testowy ttl
|
|
|
+ };
|
|
|
+ - to be optimalized for inegration
|
|
|
+ - to allow of subscribe ports
|
|
|
+ - to allow of concurent routing
|
|
|
+ - to allow new port detectioj
|
|
|
+ - to allow creating ports dependencies e.g. IF
|
|
|
+ - port has value
|
|
|
+ - port AND port
|
|
|
+ THEN
|
|
|
+ - set port
|
|
|
+ - probably should be avilable creations of virtual ports which would describe
|
|
|
+ - state of port AND port etc...
|
|
|
+ * TO BE INTEGRATED WITH p5
|
|
|
+ * - interface and WFST
|
|
|
+ * TO BE INGEGRATED WITH MARLIN 3D PRINTERS - machine and automation purpose
|
|
|
+ * - controll remote ports via g-code
|
|
|
+ * - allows interrupts
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+#define _ControllerID 0x101
|
|
|
+#define _Slave1_ID 0x103
|
|
|
+
|
|
|
+#define Active_ControllerID _ControllerID
|
|
|
+//#define Active_ControllerID _Slave1_ID
|
|
|
+
|
|
|
+
|
|
|
+ //REMOTE PORTS DEFINITIONS
|
|
|
+ #include "_Slave_Ports.h"
|
|
|
+ // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
|
|
|
+ //#define _CAN_P_0x102_PWMServoDriver0x40
|
|
|
+
|
|
|
+ // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
|
|
|
+ // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
|
|
|
+
|
|
|
+
|
|
|
+ #if Active_ControllerID == _ControllerID
|
|
|
+ String lcdPattern = "s" ;
|
|
|
+ #define HAS_LCD1602
|
|
|
+ // #define HAS_TFT_ILI9163C
|
|
|
+ #endif
|
|
|
+ #if Active_ControllerID == _Slave1_ID
|
|
|
+ String lcdPattern = "." ;
|
|
|
+ #define HAS_TFT_ILI9163C
|
|
|
+ // #define HAS_LCD1602
|
|
|
+ #endif
|
|
|
+// #define __CSpin 53 // 10 // chip select
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ int LCDtim = 250; //the value of delay time
|
|
|
+
|
|
|
+//BEGIN 1602
|
|
|
+#if defined(HAS_LCD1602)
|
|
|
+ #include <LiquidCrystal.h>
|
|
|
+ // initialize the library with the numbers of the interface pins
|
|
|
+ //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
|
|
|
+ //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
|
|
|
+ const int LCD1602rs=12, LCD1602en=11, LCD1602db4=5, LCD1602db5=4, LCD1602db6=3, LCD1602db7=2; // lcd keypad shield pins
|
|
|
+ LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
|
|
|
+ int lcdPos = 0 ;
|
|
|
+
|
|
|
+#endif
|
|
|
+// include the library code
|
|
|
+/**********************************************************/
|
|
|
+//EOF BEGIN 1602
|
|
|
+
|
|
|
+#if defined(HAS_TFT_ILI9163C)
|
|
|
+ #include <SPI.h>
|
|
|
+ #include <Adafruit_GFX.h>
|
|
|
+ #include <TFT_ILI9163C.h>
|
|
|
+
|
|
|
+ //#define TFT_ILI9163C_CSpin __CSpin // 53 // 10 // chip select
|
|
|
+ #define TFT_ILI9163C_CSpin 7 // __CSpin // 53 // 10 // chip select
|
|
|
+
|
|
|
+ #define TFT_ILI9163C_DCpin 8 // A0 data/command pin. sometimes called DC, RS, ...
|
|
|
+
|
|
|
+ #define TFT_ILI9163C__RSTpin 9 //reset pin. you must connect. (or pullup)
|
|
|
+ //#define __SDA 51 //11 //+ bidirectional data pin. Connect to MOSI
|
|
|
+ //#define __SCK 52 //13 //+ clock. sometimes called SCLK, SCL, ...
|
|
|
+
|
|
|
+
|
|
|
+ // Color definitions
|
|
|
+ #define BLACK 0x0000
|
|
|
+ #define BLUE 0x001F
|
|
|
+ #define RED 0xF800
|
|
|
+ #define GREEN 0x07E0
|
|
|
+ #define CYAN 0x07FF
|
|
|
+ #define MAGENTA 0xF81F
|
|
|
+ #define YELLOW 0xFFE0
|
|
|
+ #define WHITE 0xFFFF
|
|
|
+
|
|
|
+ TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin);
|
|
|
+ float p = 3.1415926;
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+//BEGIN CAN
|
|
|
+
|
|
|
+
|
|
|
+#include <mcp_can.h>
|
|
|
+#include <SPI.h>
|
|
|
+
|
|
|
+long unsigned int CANrxId;
|
|
|
+unsigned char CANlen = 0;
|
|
|
+unsigned char CANrxBuf[8];
|
|
|
+const int CAN_CSpin = 53 , CAN_INTpin = 10 ;
|
|
|
+MCP_CAN CAN0(CAN_CSpin); // Set CS to pin 10
|
|
|
+//EOF CAN
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void setup() {
|
|
|
+
|
|
|
+
|
|
|
+ String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
|
|
|
+ String LCDarray2="Testing id "; //the string to print onthe LCD
|
|
|
+ LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // put your setup code here, to run once:
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ lcd.begin(16, 2); // set up the LCD's number of columns and rows:
|
|
|
+ lcd.print("TESTING id:");
|
|
|
+ lcd.print(Active_ControllerID);
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print(LCDarray2);
|
|
|
+ #endif
|
|
|
+
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ display.begin();
|
|
|
+ display.clearScreen();
|
|
|
+ display.setCursor(0,0);
|
|
|
+ display.print(LCDarray1);
|
|
|
+ display.setCursor(0,10);
|
|
|
+ display.print(LCDarray2);
|
|
|
+ delay(LCDtim);
|
|
|
+ #endif
|
|
|
+
|
|
|
+ //BEGIN CAN
|
|
|
+ Serial.begin(115200);
|
|
|
+ //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
|
|
|
+ if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
|
|
|
+
|
|
|
+ Serial.print("MCP2515 Init Okay!!\r\n");
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ delay(LCDtim);
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print("MCP2515 Init Okay!!\r\n");
|
|
|
+ delay(LCDtim);
|
|
|
+ // lcd.clear();
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ delay(LCDtim);
|
|
|
+ display.setCursor(0,20);
|
|
|
+ display.print("MCP2515 Init Okay!!\r\n");
|
|
|
+ delay(LCDtim);
|
|
|
+ #endif
|
|
|
+ } else {
|
|
|
+ Serial.print("MCP2515 Init Failed!!\r\n");
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ delay(LCDtim);
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print("MCP2515 Init Failed!!\r\n");
|
|
|
+ delay(LCDtim);
|
|
|
+ lcd.setCursor(0,0);
|
|
|
+ lcd.print("CAN_CSpin: ");
|
|
|
+ lcd.print(CAN_CSpin);
|
|
|
+ lcd.print("CAN_INTpin: ");
|
|
|
+ lcd.print(CAN_INTpin);
|
|
|
+ delay(LCDtim);
|
|
|
+
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ delay(LCDtim);
|
|
|
+ display.setCursor(0,20);
|
|
|
+ display.print("MCP2515 Init Failed!! Check: \r\n");
|
|
|
+ delay(LCDtim);
|
|
|
+ display.setCursor(0,50);
|
|
|
+ display.print("CAN_CSpin: ");
|
|
|
+ display.print(CAN_CSpin);
|
|
|
+ display.print("CAN_INTpin: ");
|
|
|
+ display.print(CAN_INTpin);
|
|
|
+ delay(LCDtim);
|
|
|
+ #endif
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #if Active_ControllerID == _Slave1_ID
|
|
|
+ LCDarray1 = "Slave Module ";
|
|
|
+ LCDarray2 = "enabling filters";
|
|
|
+ //CAN0.init_Mask(0,0,0x010F0000); // Init first mask...
|
|
|
+ //CAN0.init_Filt(0,0,0x01000000); // Init first filter...
|
|
|
+ //CAN0.init_Filt(1,0,0x01010000); // Init second filter...
|
|
|
+
|
|
|
+ //CAN0.init_Mask(1,0,0x010F0000); // Init second mask...
|
|
|
+ //CAN0.init_Filt(2,0,0x01030000); // Init third filter...
|
|
|
+ //CAN0.init_Filt(3,0,0x01040000); // Init fouth filter...
|
|
|
+ //CAN0.init_Filt(4,0,0x01060000); // Init fifth filter...
|
|
|
+ //CAN0.init_Filt(5,0,0x01070000); // Init sixth filter...
|
|
|
+
|
|
|
+ //CAN0.init_Mask(0, 0, 0x01FF);
|
|
|
+ //CAN0.init_Filt(0, 0, 0x103);
|
|
|
+
|
|
|
+ //CAN.init_Mask(1, 0, 0x04FF);
|
|
|
+ //CAN.init_Filt(1, 0, 0x4C0);
|
|
|
+ #elif Active_ControllerID == _ControllerID
|
|
|
+ LCDarray1 = "Master Module ";
|
|
|
+ LCDarray2 = "enabling remotes";
|
|
|
+ #endif
|
|
|
+
|
|
|
+
|
|
|
+ Serial.println("");
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ delay(LCDtim);
|
|
|
+ lcd.setCursor(0,0);
|
|
|
+ lcd.print(LCDarray1);
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print(LCDarray2);
|
|
|
+ delay(LCDtim * 10);
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ delay(LCDtim);
|
|
|
+ display.setCursor(0,30);
|
|
|
+ display.print(LCDarray1); display.print(" ");
|
|
|
+ display.print(LCDarray2);
|
|
|
+
|
|
|
+ delay(LCDtim);
|
|
|
+ #endif
|
|
|
+ CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
|
|
|
+ pinMode(CAN_INTpin, INPUT); // Setting pin 2 for /INT input
|
|
|
+
|
|
|
+ //EOF CAN
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print("loop...");
|
|
|
+ lcdPos = 7;
|
|
|
+ #endif
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+ #if Active_ControllerID == _ControllerID
|
|
|
+ byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
|
|
|
+ #endif
|
|
|
+
|
|
|
+ String LCDarray1 ;
|
|
|
+ String LCDarray2 ;
|
|
|
+void loop() {
|
|
|
+ // put your main code here, to run repeatedly:
|
|
|
+ //BEGIN CAN
|
|
|
+ #if Active_ControllerID == _Slave1_ID
|
|
|
+ if(!digitalRead(CAN_INTpin)) // If pin 2 is low, read receive buffer
|
|
|
+ {
|
|
|
+ CAN0.readMsgBuf(&CANrxId, &CANlen, CANrxBuf); // Read data: len = data length, buf = data byte(s)
|
|
|
+ Serial.print("ID: ");
|
|
|
+ Serial.print(CANrxId, HEX);
|
|
|
+ Serial.print(" Data: ");
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ lcd.setCursor(0,0);
|
|
|
+ lcd.print("ID: ");
|
|
|
+ lcd.print(CANrxId, HEX);
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print(" Data: ");
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ display.setCursor(0,40);
|
|
|
+ display.print("ID: ");
|
|
|
+ display.print(CANrxId, HEX);
|
|
|
+ display.setCursor(0,50);
|
|
|
+ display.print(" Data: ");
|
|
|
+ #endif
|
|
|
+ for(int i = 0; i<CANlen; i++) // Print each byte of the data
|
|
|
+ {
|
|
|
+ if(CANrxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
|
|
|
+ {
|
|
|
+ Serial.print("0");
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ lcd.print("0");
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ display.print("0");
|
|
|
+ #endif
|
|
|
+ }
|
|
|
+ Serial.print(CANrxBuf[i], HEX);
|
|
|
+ Serial.print(" ");
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ lcd.print(CANrxBuf[i], HEX);
|
|
|
+ lcd.print(" ");
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ display.print(CANrxBuf[i], HEX);
|
|
|
+ display.print(" ");
|
|
|
+ #endif
|
|
|
+ }
|
|
|
+ Serial.println();
|
|
|
+ }
|
|
|
+ //EOF CAN
|
|
|
+ #if defined(HAS_LCD1602)
|
|
|
+ delay(LCDtim);
|
|
|
+ //lcd.setCursor(0,1);
|
|
|
+ lcd.print(lcdPattern);
|
|
|
+ //lcd.print(lcdPos % 10);
|
|
|
+ if(lcdPos == 16) {
|
|
|
+ //lcd.print("N");
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcdPos = 0;
|
|
|
+ if(lcdPattern==".") lcdPattern = "-" ;
|
|
|
+ else lcdPattern = "." ;
|
|
|
+ }
|
|
|
+ lcdPos ++;
|
|
|
+ #endif
|
|
|
+ #if defined(HAS_TFT_ILI9163C)
|
|
|
+ delay(LCDtim);
|
|
|
+ display.setCursor(0,60);
|
|
|
+ display.print("loop....");
|
|
|
+ #endif
|
|
|
+
|
|
|
+ #elif Active_ControllerID == _ControllerID
|
|
|
+ #include "_ControllerID_loop1.h"
|
|
|
+
|
|
|
+ #endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|