P5_Automation_can-dev.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* P5_Automation_can-dev
  2. * 2020-06 testing communications CAN for integrate further with Procesy5
  3. * A.Binder
  4. * Uses:
  5. * LCD1602 - big 2x16 character display
  6. * TFT_ILI9163C 128x128 lcd display
  7. * MCP_2515 - can port controller
  8. *
  9. * controls
  10. * Slave ports via:
  11. * I2C
  12. * PCA9685 - 16port 12bit PWM driver - can controll 16 eg. LEDs which can be 0....12V dimmed=]
  13. * todo 74HC595 - Serial to Parallel Shifting-Out - from 8 ports to many if connected in paraller
  14. * based on https://www.arduino.cc/en/tutorial/ShiftOut
  15. * or 16 servos
  16. * - can have addressed many of PCA9685 on I2C bus - so can controll over 64 ports
  17. * to be added another devices tested
  18. * todo Rottary Swich { GND, +, SW, DT, CLK } - to provide LCD menu access
  19. * TODO structure of port is in _Slave_Ports.h
  20. * _Slave_Port _Slave_Ports[] = {
  21. {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 },
  22. {"CAN", - Base port origin protocol
  23. 0x102, - Base port origin protocol address
  24. 0x80, - int PortTypes; 0xFF = 1111 1111
  25. 0x01 = 0000 0001 = PWM port
  26. {
  27. bit 1 - PWM port
  28. bit 2 - not allocated
  29. bit 3 - not allocated
  30. bit 4 - not allocated
  31. bit 2 - not allocated
  32. bit 5 - not allocated
  33. bit 6 - not allocated
  34. bit 7 - not allocated
  35. bit 8 - not allocated
  36. },
  37. 0x00, int Address - local address on e.g. I2C bus - 0x40 etc
  38. 0x00, - int LocalPort - local port number when used e.g. I2C extension
  39. 0x01 - Exp_state - 0xFF = 1111 1111
  40. 0x00 = 0000 0000 = OFF = 0V
  41. 0x01 = 0000 0001 = ON = PWM max
  42. {
  43. bit 1 - OFF = 0V
  44. bit 2 - ON = PWM max
  45. bit 3 - not allocated
  46. bit 4 - not allocated
  47. bit 2 - not allocated
  48. bit 5 - not allocated
  49. bit 6 - not allocated
  50. bit 7 - not allocated
  51. bit 8 - not allocated
  52. },
  53. 0x6d - testowy ttl - 10 sekund do przedawnienia stanu portu w tablicy i koniecznosci odswiezenia tablicy
  54. }
  55. };
  56. - to be optimalized for inegration
  57. - to allow of subscribe ports
  58. - to allow of concurent routing
  59. - to allow new port detectioj
  60. - to allow creating ports dependencies e.g. IF
  61. - port has value
  62. - port AND port
  63. THEN
  64. - set port
  65. - probably should be avilable creations of virtual ports which would describe
  66. - state of port AND port etc...
  67. * TO BE INTEGRATED WITH p5
  68. * - interface and WFST
  69. * TO BE INGEGRATED WITH MARLIN 3D PRINTERS - machine and automation purpose
  70. * - controll remote ports via g-code
  71. * - allows interrupts
  72. * TO ALLOW OF NEXT PACKETS TYPES WITH DIFFERENT MEANINGS
  73. * - dimming
  74. * logic
  75. */
  76. #include "Queue.h"
  77. Queue<int> _Slave_Ports_queue = Queue<int>(10);
  78. #define _ControllerID 0x101
  79. #define _Slave1_ID 0x103
  80. #define Active_ControllerID _ControllerID
  81. //#define Active_ControllerID _Slave1_ID
  82. #include "Slave_Ports_Status.h"
  83. #include "Slave_Ports_QUEUE.h"
  84. #include "_Slave_Ports_queue_task_type_LIST.h"
  85. #include "_Slave_Ports_Protocol.h"
  86. #if Active_ControllerID == _ControllerID
  87. String lcdPattern = "s" ;
  88. // #define HAS_LCD1602
  89. // #define HAS_TFT_ILI9163C
  90. #define HAS_ROTTARY_SW
  91. #endif
  92. #if Active_ControllerID == _Slave1_ID
  93. String lcdPattern = "." ;
  94. #define HAS_TFT_ILI9163C
  95. // #define HAS_LCD1602
  96. #endif
  97. //BEGIN 1602
  98. #if defined(HAS_LCD1602)
  99. #include <LiquidCrystal.h>
  100. // initialize the library with the numbers of the interface pins
  101. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  102. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  103. const int LCD1602rs=12, LCD1602en=11, LCD1602db4=5, LCD1602db5=4, LCD1602db6=3, LCD1602db7=2; // lcd keypad shield pins
  104. LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
  105. int lcdPos = 0 ;
  106. #include "LCD1602.h"
  107. #endif
  108. #if defined(HAS_TFT_ILI9163C)
  109. #include <SPI.h>
  110. #include <Adafruit_GFX.h>
  111. #include <TFT_ILI9163C.h>
  112. #include "TFT_ILI9163C.h"
  113. #endif
  114. //BEGIN CAN
  115. #include <mcp_can.h>
  116. #include <SPI.h>
  117. long unsigned int CANrxId;
  118. unsigned char CANlen = 0;
  119. unsigned char CANrxBuf[8];
  120. const int CAN_CSpin = 53 , CAN_INTpin = 10 ;
  121. MCP_CAN CAN0(CAN_CSpin); // Set CS to pin 10
  122. //EOF CAN
  123. //REMOTE PORTS DEFINITIONS
  124. #include "_Slave_Ports.h"
  125. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  126. //#define _CAN_P_0x102_PWMServoDriver0x40
  127. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  128. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  129. // #define __CSpin 53 // 10 // chip select
  130. int LCDtim = 250; //the value of delay time
  131. #if defined(HAS_ROTTARY_SW)
  132. #include "_ROTTARY_SW.h"
  133. #endif
  134. // include the library code
  135. /**********************************************************/
  136. //EOF BEGIN 1602
  137. #include "_PCINT.h"
  138. void setup() {
  139. Serial.begin(115200);
  140. Slave_Ports_Status Slave_Ports_Status_C;
  141. Slave_Ports_QUEUE Slave_Ports_QUEUE_C;
  142. #include "_Slave_Ports_LOCAL_LIST.h"
  143. #if defined(HAS_LCD1602)
  144. lcd.begin(16, 2);
  145. #endif
  146. //Slave_Ports_Status_T _add[] = { 1, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01 };
  147. Serial.println("#200 adding ports");
  148. Slave_Ports_Status_C.add_port(
  149. 1, // int Port_ID,
  150. Active_ControllerID, // int _Slave_ID,
  151. 0x01, // int PortTypes,
  152. 0x00, // int Address,
  153. 1, // int LocalPort,
  154. 0x01, // int Exp_state,
  155. 0x01, // int Exp_state_ttl,
  156. 0x01, // int Broadcast_state,
  157. 0x01, // int I2C_port,
  158. I2C_driver_NOT_USED_ ); // int I2C_driver
  159. Slave_Ports_Status_C.add_port( 2, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01, 0x01, I2C_driver_NOT_USED_); // Slave_Ports_Status_C.I2C_driver_F(Adafruit_PWMServoDriver)
  160. Slave_Ports_Status_C.add_port( 3, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01, 0x01, I2C_driver_Adafruit_PWMServoDriver ); // Slave_Ports_Status_C.I2C_driver_F(Adafruit_PWMServoDriver)
  161. Slave_Ports_Status_C.add_port( 4, _Slave1_ID, 0x01, 0x00, 1, 0x01, 0x01, 0x01, 0x01, I2C_driver_Adafruit_PWMServoDriver ); // Slave_Ports_Status_C.I2C_driver_F(Adafruit_PWMServoDriver)
  162. //Slave_Ports_Status_C.list_ports();
  163. //Slave_Ports_Status_C.add_empty();
  164. //Queue<int> _Slave_Ports_queue = Queue<int>(10);
  165. // LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
  166. //Slave_Ports_Status_T
  167. //struct Slave_Ports_Status_S _add;
  168. // _LCD_print_queue.push(1);
  169. // _LCD_print_queue.push(2);
  170. // _LCD_print_queue.push(3);
  171. //String character = "Geas";
  172. //char b = abc.charAt(0);
  173. //int b_ascii_value = b;
  174. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  175. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  176. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  177. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  178. Serial.println("#221");
  179. //todo PCINT service
  180. #include "_PCINT_setup.h"
  181. #if defined(HAS_ROTTARY_SW)
  182. #include "_ROTTARY_SW_setup.h"
  183. #endif
  184. Serial.println("#231");
  185. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  186. String LCDarray2="Testing id "; //the string to print onthe LCD
  187. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  188. delay(1000);
  189. // put your setup code here, to run once:
  190. #if defined(HAS_LCD1602)
  191. LCD_print(LCDarray1, LCDarray2, LCDtim/5);
  192. #endif
  193. #if defined(HAS_TFT_ILI9163C)
  194. TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin);
  195. float p = 3.1415926;
  196. display.begin();
  197. display.clearScreen();
  198. display.setCursor(0,0);
  199. display.print(LCDarray1);
  200. display.setCursor(0,10);
  201. display.print(LCDarray2);
  202. delay(LCDtim);
  203. #endif
  204. //BEGIN CAN
  205. //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  206. if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  207. //LCDarray1="MCP2515 Init Okay";
  208. //LCDarray2="500KBPS 16MHZ";
  209. Serial.println("#303 MCP2515 Init Okay");
  210. /*
  211. #if defined(HAS_LCD1602)
  212. LCD_print(LCDarray1, LCDarray2, LCDtim/5);
  213. #endif
  214. #if defined(HAS_TFT_ILI9163C)
  215. delay(LCDtim);
  216. display.setCursor(0,20);
  217. display.print("MCP2515 Init Okay!!\r\n");
  218. delay(LCDtim);
  219. #endif
  220. */
  221. } else {
  222. /* LCDarray1 = "MCP2515 Init Failed";
  223. LCDarray2 = "CAN_CSpin: ";
  224. LCDarray2.concat(CAN_CSpin);
  225. LCDarray2.concat("CAN_INTpin: ");
  226. LCDarray2.concat(CAN_INTpin);
  227. */
  228. Serial.println("#322 MCP2515 Init Failed");
  229. /*
  230. #if defined(HAS_LCD1602)
  231. LCD_print(LCDarray1, LCDarray2, LCDtim / 2 ) ;
  232. #endif
  233. #if defined(HAS_TFT_ILI9163C)
  234. delay(LCDtim);
  235. display.setCursor(0,20);
  236. display.print(LCDarray1);
  237. delay(LCDtim);
  238. display.setCursor(0,50);
  239. display.print(LCDarray2);
  240. delay(LCDtim);
  241. #endif
  242. */
  243. }
  244. /*AAA
  245. #if Active_ControllerID == _Slave1_ID
  246. #include "_SlaveID_setup.h"
  247. #elif Active_ControllerID == _ControllerID
  248. LCDarray1 = "Master Module ";
  249. LCDarray2 = "enabling remotes ";
  250. #endif
  251. Serial.println("");
  252. #if defined(HAS_LCD1602)
  253. LCD_print(LCDarray1, LCDarray2, LCDtim / 5 ) ;
  254. delay(LCDtim * 10);
  255. #endif
  256. #if defined(HAS_TFT_ILI9163C)
  257. delay(LCDtim);
  258. display.setCursor(0,30);
  259. display.print(LCDarray1); display.print(" ");
  260. display.print(LCDarray2);
  261. delay(LCDtim);
  262. #endif
  263. */
  264. CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
  265. pinMode(CAN_INTpin, INPUT); // Setting pin 2 for /INT input
  266. Serial.println("#362 set MCP_NORMAL");
  267. //EOF CAN
  268. /*AA
  269. #if defined(HAS_LCD1602)
  270. LCDarray1 = "#323 loop.... ";
  271. //LCDarray2 = "....";
  272. LCD_print1(LCDarray1, LCDtim/10);
  273. #endif
  274. */
  275. //_Slave_Ports_queue.push(_LCD_print_queue.peek() + 1);
  276. Serial.println("#298 _Slave_Ports_queue__list");
  277. //AA _Slave_Ports_queue__INIT(_Slave_Ports_queue_tasks_LIST);
  278. //AA _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  279. //AA int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  280. // int _Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  281. //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());
  282. //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 //,
  283. //A_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, int(0x04) ,0x00 ,100, 5000 ) ; // "_Slave_Ports_broadcast", //4
  284. int Slave_Ports_QUEUE_C_queue_id_current_broadcast = Slave_Ports_QUEUE_C.queue__add( TASK_TYPE_T_Slave_Ports_broadcast____,100, 5000 ) ; // "_Slave_Ports_broadcast", //4
  285. //_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x05 ,0x00 ,100, 5000 ) ; //"_Slave_Ports_set_I2C" //5 - to set ports associated on PW
  286. int Slave_Ports_QUEUE_C_queue_id_current_set_I2C = Slave_Ports_QUEUE_C.queue__add( TASK_TYPE_T_Slave_Ports_set_I2C______,100, 5000 ) ; // "_Slave_Ports_broadcast", //4
  287. delay(10000);
  288. // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  289. //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  290. //_Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ;
  291. //_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());
  292. // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  293. //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  294. // _Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ;
  295. //_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000, _Slave_Ports_queue_peek,
  296. //_Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1),
  297. //_Slave_Ports_queue.peek());
  298. Serial.println("#301 _Slave_Ports_queue__list");
  299. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  300. Serial.print("#304 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count());
  301. // _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000 ) ;
  302. //Serial.println("#323 _Slave_Ports_queue__list"); _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  303. // Serial.print("#324 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count());
  304. }
  305. //#if Active_ControllerID == _ControllerID
  306. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  307. //#endif
  308. //String LCDarray1 ;
  309. //String LCDarray2 ;
  310. unsigned long time;
  311. void loop() {
  312. #include "_PCINT_loop.h"
  313. time = millis();
  314. Serial.print("Time: " );
  315. Serial.println(time);
  316. // put your main code here, to run repeatedly:
  317. //BEGIN CAN
  318. #if Active_ControllerID == _Slave1_ID
  319. #include "_SlaveID_loop1.h"
  320. #elif Active_ControllerID == _ControllerID
  321. #include "_ControllerID_loop1.h"
  322. #endif
  323. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST) ;
  324. int _Slave_Ports_queue_count = _Slave_Ports_queue.count();
  325. int _Slave_Ports_queue_front = _Slave_Ports_queue.front();
  326. int _Slave_Ports_queue_back = _Slave_Ports_queue.back();
  327. int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek();
  328. int _Slave_Ports_queue_pop = _Slave_Ports_queue.pop();
  329. _Slave_Ports_queue__POP( _Slave_Ports_queue_tasks_LIST,
  330. _Slave_Ports_queue_count,
  331. _Slave_Ports_queue_pop,
  332. _Slave_Ports_queue_front,
  333. _Slave_Ports_queue_back,
  334. _Slave_Ports_queue_peek,
  335. LCDtim, _Slave_Ports);
  336. delay(5000);
  337. //_LCD_print_buffors_list(_LCD_print_buffors) ;
  338. //_LCD_print_screens_list(_LCD_print_screens);
  339. }