/* 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=] * todo 74HC595 - Serial to Parallel Shifting-Out - from 8 ports to many if connected in paraller * based on https://www.arduino.cc/en/tutorial/ShiftOut * 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 Rottary Swich { GND, +, SW, DT, CLK } - to provide LCD menu access * TODO structure of port is in _Slave_Ports.h * _Slave_Port _Slave_Ports[] = { {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 }, {"CAN", - Base port origin protocol 0x102, - Base port origin protocol address 0x80, - int PortTypes; 0xFF = 1111 1111 0x01 = 0000 0001 = PWM port { bit 1 - PWM port bit 2 - not allocated bit 3 - not allocated bit 4 - not allocated bit 2 - not allocated bit 5 - not allocated bit 6 - not allocated bit 7 - not allocated bit 8 - not allocated }, 0x00, int Address - local address on e.g. I2C bus - 0x40 etc 0x00, - int LocalPort - local port number when used e.g. I2C extension 0x01 - Exp_state - 0xFF = 1111 1111 0x00 = 0000 0000 = OFF = 0V 0x01 = 0000 0001 = ON = PWM max { bit 1 - OFF = 0V bit 2 - ON = PWM max bit 3 - not allocated bit 4 - not allocated bit 2 - not allocated bit 5 - not allocated bit 6 - not allocated bit 7 - not allocated bit 8 - not allocated }, 0x6d - testowy ttl - 10 sekund do przedawnienia stanu portu w tablicy i koniecznosci odswiezenia tablicy } }; - 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 * TO ALLOW OF NEXT PACKETS TYPES WITH DIFFERENT MEANINGS * - dimming * logic */ #define _ControllerID 0x101 #define _Slave1_ID 0x103 #include "config_Active_ControllerID.h" //#include "Active_ControllerID_0x103.h" //#define Active_ControllerID _ControllerID //#define Active_ControllerID _Slave1_ID #include "_Slave_Ports_Struct.h" #include "Queue.h" Queue _Slave_Ports_queue = Queue(10); #if Active_ControllerID == _ControllerID String lcdPattern = "s" ; #endif #if Active_ControllerID == _Slave1_ID String lcdPattern = "." ; #endif //BEGIN 1602 #if defined(HAS_LCD1602) #include // 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 ); #include "LCD1602.h" #endif #if defined(HAS_TFT_ILI9163C) #include "_TFT_ILI9163C.h" #endif //BEGIN CAN #include #include 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 //REMOTE PORTS DEFINITIONS #include "_Slave_Ports_Protocol.h" #include "Slave_Ports_Status_S.h" _Slave_Port _Slave_Ports[] = { {"CAN", 0x102, 0x07, 0x00, 0x00, 0x00, 0x00 } //, //{"CAN", 0x102, 0x01, 0x00, 0x01, 0xFF, 0x6d } //testowy ttl }; Slave_Ports_Status_T Slave_Ports_Status_A[100]; Slave_Ports_Status_add_port_count_T lave_Ports_Status_add_port_count_A = 0; #include "_Slave_Ports_LOCAL.h" #include "_Slave_Ports.h" // #include "_Slave_Ports_ttl_decrement_POP.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); // #define __CSpin 53 // 10 // chip select #include "_DISPLAY_init.h" #if defined(HAS_ROTTARY_SW) #include "_ROTTARY_SW.h" #endif // include the library code /**********************************************************/ //EOF BEGIN 1602 #include "_PCINT.h" void setup() { Serial.begin(115200); // _LCD_print_queue.push(1); // _LCD_print_queue.push(2); // _LCD_print_queue.push(3); //String character = "Geas"; //char b = abc.charAt(0); //int b_ascii_value = b; // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000); // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000); // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000); // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000); //todo PCINT service #include "_PCINT_setup.h" #if defined(HAS_ROTTARY_SW) #include "_ROTTARY_SW_setup.h" #endif 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); LCD_print(LCDarray1, LCDarray2, LCDtim/5); #endif #if defined(HAS_TFT_ILI9163C) TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin); float p = 3.1415926; display.begin(); display.clearScreen(); display.setCursor(0,0); display.print(LCDarray1); display.setCursor(0,10); display.print(LCDarray2); delay(LCDtim); #endif //BEGIN CAN //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) { if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) { LCDarray1="MCP2515 Init Okay"; LCDarray2="500KBPS 16MHZ"; Serial.print(LCDarray1 + LCDarray2); #if defined(HAS_LCD1602) LCD_print(LCDarray1, LCDarray2, LCDtim/5); #endif #if defined(HAS_TFT_ILI9163C) delay(LCDtim); display.setCursor(0,20); display.print("MCP2515 Init Okay!!\r\n"); delay(LCDtim); #endif } else { LCDarray1 = "MCP2515 Init Failed"; LCDarray2 = "CAN_CSpin: "; LCDarray2.concat(CAN_CSpin); LCDarray2.concat("CAN_INTpin: "); LCDarray2.concat(CAN_INTpin); Serial.print(LCDarray1); #if defined(HAS_LCD1602) LCD_print(LCDarray1, LCDarray2, LCDtim / 2 ) ; #endif #if defined(HAS_TFT_ILI9163C) delay(LCDtim); display.setCursor(0,20); display.print(LCDarray1); delay(LCDtim); display.setCursor(0,50); display.print(LCDarray2); delay(LCDtim); #endif } #if Active_ControllerID == _Slave1_ID #include "_SlaveID_setup.h" #elif Active_ControllerID == _ControllerID LCDarray1 = "Master Module "; LCDarray2 = "enabling remotes "; #endif Serial.println(""); #if defined(HAS_LCD1602) LCD_print(LCDarray1, LCDarray2, LCDtim / 5 ) ; 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) LCDarray1 = "loop.... "; LCDarray2 = "...."; LCD_print(LCDarray1, LCDarray2, LCDtim/10); #endif //_Slave_Ports_queue.push(_LCD_print_queue.peek() + 1); Serial.println("#298 _Slave_Ports_queue__list"); _Slave_Ports_queue__INIT(_Slave_Ports_queue_tasks_LIST); _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST); int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ; // int _Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ; //int _Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ; _Slave_Ports_queue__add( *_Slave_Ports_queue_tasks_LIST, 0x01 ,0x00 ,100, 5000 ) ; // _Slave_Ports_queue_peek, _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1), _Slave_Ports_queue.peek()); //void _Slave_Ports_queue__add( _Slave_Ports_queue_tasksT* _Slave_Ports_queue_tasks_LIST,int _Slave_Ports_queue_task_type,int _Slave_Port_link,long time_seq_id, long time_min_run //, _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, TASK_TYPE_T_Slave_Ports_broadcast____ ,0x00 ,100, 5000 ) ; // "_Slave_Ports_broadcast", //4 _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, TASK_TYPE_T_Slave_Ports_set_I2C______ ,0x00 ,100, 5000 ) ; //"_Slave_Ports_set_I2C" //5 - to set ports associated on PW // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ; //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ; //_Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ; //_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x02 ,0x00 ,100, 5000, _Slave_Ports_queue_peek, _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1), _Slave_Ports_queue.peek()); // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ; //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ; // _Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ; //_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000, _Slave_Ports_queue_peek, //_Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1), //_Slave_Ports_queue.peek()); Serial.println("#301 _Slave_Ports_queue__list"); _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST); Serial.print("#304 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count()); // _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000 ) ; //Serial.println("#323 _Slave_Ports_queue__list"); _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST); // Serial.print("#324 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count()); } //#if Active_ControllerID == _ControllerID // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; //#endif //String LCDarray1 ; //String LCDarray2 ; unsigned long time; void loop() { #include "_PCINT_loop.h" time = millis(); Serial.print("Time: " ); Serial.println(time); // put your main code here, to run repeatedly: //BEGIN CAN #if Active_ControllerID == _Slave1_ID #include "_SlaveID_loop1.h" #elif Active_ControllerID == _ControllerID #include "_ControllerID_loop1.h" #endif _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST) ; int _Slave_Ports_queue_count = _Slave_Ports_queue.count(); int _Slave_Ports_queue_front = _Slave_Ports_queue.front(); int _Slave_Ports_queue_back = _Slave_Ports_queue.back(); int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek(); int _Slave_Ports_queue_pop = _Slave_Ports_queue.pop(); _Slave_Ports_queue__POP( _Slave_Ports_queue_tasks_LIST, _Slave_Ports_queue_count, _Slave_Ports_queue_pop, _Slave_Ports_queue_front, _Slave_Ports_queue_back, _Slave_Ports_queue_peek, LCDtim, _Slave_Ports); //_LCD_print_buffors_list(_LCD_print_buffors) ; //_LCD_print_screens_list(_LCD_print_screens); delay(5000); } #include "_PCINT_func.h" #include "_Slave_Ports_func.h"