P5_Automation_can-dev.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. Slave_Ports_Status Slave_Ports_Status_C;
  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. #include "_Slave_Ports_LOCAL_LIST.h"
  141. #if defined(HAS_LCD1602)
  142. lcd.begin(16, 2);
  143. #endif
  144. Slave_Ports_Status_T _add[] = { 1, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01 };
  145. /*Slave_Ports_Status_C.add_port(
  146. 1, // int Port_ID,
  147. Active_ControllerID, // int _Slave_ID,
  148. 0x01, // int PortTypes,
  149. 0x00, // int Address,
  150. 1, // int LocalPort,
  151. 0x01, // int Exp_state,
  152. 0x01, // int Exp_state_ttl,
  153. 0x01, // int Broadcast_state,
  154. 0x01, // int I2C_port,
  155. _NOT_USED_ ); // int I2C_driver
  156. */
  157. Slave_Ports_Status_C.add_port( 2, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01, 0x01, _NOT_USED_); // Slave_Ports_Status_C.I2C_driver_F(Adafruit_PWMServoDriver)
  158. Slave_Ports_Status_C.add_port( 3, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01, 0x01, Adafruit_PWMServoDriver ); // Slave_Ports_Status_C.I2C_driver_F(Adafruit_PWMServoDriver)
  159. //Slave_Ports_Status_C.add_port( 4, _Slave1_ID, 0x01, 0x00, 1, 0x01, 0x01, 0x01, 0x01, Adafruit_PWMServoDriver ); // Slave_Ports_Status_C.I2C_driver_F(Adafruit_PWMServoDriver)
  160. //Slave_Ports_Status_C.list_ports();
  161. //Slave_Ports_Status_C.add_empty();
  162. //Queue<int> _Slave_Ports_queue = Queue<int>(10);
  163. // LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
  164. //Slave_Ports_Status_T
  165. //struct Slave_Ports_Status_S _add;
  166. // _LCD_print_queue.push(1);
  167. // _LCD_print_queue.push(2);
  168. // _LCD_print_queue.push(3);
  169. //String character = "Geas";
  170. //char b = abc.charAt(0);
  171. //int b_ascii_value = b;
  172. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  173. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  174. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  175. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  176. Serial.println("#221");
  177. //todo PCINT service
  178. #include "_PCINT_setup.h"
  179. #if defined(HAS_ROTTARY_SW)
  180. #include "_ROTTARY_SW_setup.h"
  181. #endif
  182. Serial.println("#231");
  183. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  184. String LCDarray2="Testing id "; //the string to print onthe LCD
  185. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  186. delay(1000);
  187. // put your setup code here, to run once:
  188. #if defined(HAS_LCD1602)
  189. LCD_print(LCDarray1, LCDarray2, LCDtim/5);
  190. #endif
  191. #if defined(HAS_TFT_ILI9163C)
  192. TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin);
  193. float p = 3.1415926;
  194. display.begin();
  195. display.clearScreen();
  196. display.setCursor(0,0);
  197. display.print(LCDarray1);
  198. display.setCursor(0,10);
  199. display.print(LCDarray2);
  200. delay(LCDtim);
  201. #endif
  202. //BEGIN CAN
  203. //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  204. if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  205. LCDarray1="MCP2515 Init Okay";
  206. LCDarray2="500KBPS 16MHZ";
  207. Serial.print(LCDarray1 + LCDarray2);
  208. #if defined(HAS_LCD1602)
  209. LCD_print(LCDarray1, LCDarray2, LCDtim/5);
  210. #endif
  211. #if defined(HAS_TFT_ILI9163C)
  212. delay(LCDtim);
  213. display.setCursor(0,20);
  214. display.print("MCP2515 Init Okay!!\r\n");
  215. delay(LCDtim);
  216. #endif
  217. } else {
  218. LCDarray1 = "MCP2515 Init Failed";
  219. LCDarray2 = "CAN_CSpin: ";
  220. LCDarray2.concat(CAN_CSpin);
  221. LCDarray2.concat("CAN_INTpin: ");
  222. LCDarray2.concat(CAN_INTpin);
  223. Serial.print(LCDarray1);
  224. #if defined(HAS_LCD1602)
  225. LCD_print(LCDarray1, LCDarray2, LCDtim / 2 ) ;
  226. #endif
  227. #if defined(HAS_TFT_ILI9163C)
  228. delay(LCDtim);
  229. display.setCursor(0,20);
  230. display.print(LCDarray1);
  231. delay(LCDtim);
  232. display.setCursor(0,50);
  233. display.print(LCDarray2);
  234. delay(LCDtim);
  235. #endif
  236. }
  237. #if Active_ControllerID == _Slave1_ID
  238. #include "_SlaveID_setup.h"
  239. #elif Active_ControllerID == _ControllerID
  240. LCDarray1 = "Master Module ";
  241. LCDarray2 = "enabling remotes ";
  242. #endif
  243. Serial.println("");
  244. #if defined(HAS_LCD1602)
  245. LCD_print(LCDarray1, LCDarray2, LCDtim / 5 ) ;
  246. delay(LCDtim * 10);
  247. #endif
  248. #if defined(HAS_TFT_ILI9163C)
  249. delay(LCDtim);
  250. display.setCursor(0,30);
  251. display.print(LCDarray1); display.print(" ");
  252. display.print(LCDarray2);
  253. delay(LCDtim);
  254. #endif
  255. CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
  256. pinMode(CAN_INTpin, INPUT); // Setting pin 2 for /INT input
  257. //EOF CAN
  258. #if defined(HAS_LCD1602)
  259. LCDarray1 = "#323 loop.... ";
  260. //LCDarray2 = "....";
  261. LCD_print1(LCDarray1, LCDtim/10);
  262. #endif
  263. //_Slave_Ports_queue.push(_LCD_print_queue.peek() + 1);
  264. Serial.println("#298 _Slave_Ports_queue__list");
  265. _Slave_Ports_queue__INIT(_Slave_Ports_queue_tasks_LIST);
  266. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  267. int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  268. // int _Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  269. //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());
  270. //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 //,
  271. _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, int(0x04) ,0x00 ,100, 5000 ) ; // "_Slave_Ports_broadcast", //4
  272. _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x05 ,0x00 ,100, 5000 ) ; //"_Slave_Ports_set_I2C" //5 - to set ports associated on PW
  273. // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  274. //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  275. //_Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ;
  276. //_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());
  277. // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  278. //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  279. // _Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ;
  280. //_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000, _Slave_Ports_queue_peek,
  281. //_Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1),
  282. //_Slave_Ports_queue.peek());
  283. Serial.println("#301 _Slave_Ports_queue__list");
  284. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  285. Serial.print("#304 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count());
  286. // _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000 ) ;
  287. //Serial.println("#323 _Slave_Ports_queue__list"); _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  288. // Serial.print("#324 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count());
  289. }
  290. //#if Active_ControllerID == _ControllerID
  291. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  292. //#endif
  293. //String LCDarray1 ;
  294. //String LCDarray2 ;
  295. unsigned long time;
  296. void loop() {
  297. #include "_PCINT_loop.h"
  298. time = millis();
  299. Serial.print("Time: " );
  300. Serial.println(time);
  301. // put your main code here, to run repeatedly:
  302. //BEGIN CAN
  303. #if Active_ControllerID == _Slave1_ID
  304. #include "_SlaveID_loop1.h"
  305. #elif Active_ControllerID == _ControllerID
  306. #include "_ControllerID_loop1.h"
  307. #endif
  308. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST) ;
  309. int _Slave_Ports_queue_count = _Slave_Ports_queue.count();
  310. int _Slave_Ports_queue_front = _Slave_Ports_queue.front();
  311. int _Slave_Ports_queue_back = _Slave_Ports_queue.back();
  312. int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek();
  313. int _Slave_Ports_queue_pop = _Slave_Ports_queue.pop();
  314. _Slave_Ports_queue__POP( _Slave_Ports_queue_tasks_LIST,
  315. _Slave_Ports_queue_count,
  316. _Slave_Ports_queue_pop,
  317. _Slave_Ports_queue_front,
  318. _Slave_Ports_queue_back,
  319. _Slave_Ports_queue_peek,
  320. LCDtim, _Slave_Ports);
  321. delay(5000);
  322. //_LCD_print_buffors_list(_LCD_print_buffors) ;
  323. //_LCD_print_screens_list(_LCD_print_screens);
  324. }
  325. #include "_PCINT_func.h"
  326. #include "_Slave_Ports_func.h"