P5_Automation_can-dev.ino 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #include "Slave_Ports_Status.h"
  79. #define _ControllerID 0x101
  80. #define _Slave1_ID 0x103
  81. #define Active_ControllerID _ControllerID
  82. //#define Active_ControllerID _Slave1_ID
  83. #if Active_ControllerID == _ControllerID
  84. String lcdPattern = "s" ;
  85. #define HAS_LCD1602
  86. // #define HAS_TFT_ILI9163C
  87. #define HAS_ROTTARY_SW
  88. #endif
  89. #if Active_ControllerID == _Slave1_ID
  90. String lcdPattern = "." ;
  91. #define HAS_TFT_ILI9163C
  92. // #define HAS_LCD1602
  93. #endif
  94. //BEGIN 1602
  95. #if defined(HAS_LCD1602)
  96. #include <LiquidCrystal.h>
  97. // initialize the library with the numbers of the interface pins
  98. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  99. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  100. const int LCD1602rs=12, LCD1602en=11, LCD1602db4=5, LCD1602db5=4, LCD1602db6=3, LCD1602db7=2; // lcd keypad shield pins
  101. LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
  102. int lcdPos = 0 ;
  103. #include "LCD1602.h"
  104. #endif
  105. #if defined(HAS_TFT_ILI9163C)
  106. #include <SPI.h>
  107. #include <Adafruit_GFX.h>
  108. #include <TFT_ILI9163C.h>
  109. #include "TFT_ILI9163C.h"
  110. #endif
  111. //BEGIN CAN
  112. #include <mcp_can.h>
  113. #include <SPI.h>
  114. long unsigned int CANrxId;
  115. unsigned char CANlen = 0;
  116. unsigned char CANrxBuf[8];
  117. const int CAN_CSpin = 53 , CAN_INTpin = 10 ;
  118. MCP_CAN CAN0(CAN_CSpin); // Set CS to pin 10
  119. //EOF CAN
  120. //REMOTE PORTS DEFINITIONS
  121. #include "_Slave_Ports.h"
  122. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  123. //#define _CAN_P_0x102_PWMServoDriver0x40
  124. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  125. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  126. // #define __CSpin 53 // 10 // chip select
  127. int LCDtim = 250; //the value of delay time
  128. #if defined(HAS_ROTTARY_SW)
  129. #include "_ROTTARY_SW.h"
  130. #endif
  131. // include the library code
  132. /**********************************************************/
  133. //EOF BEGIN 1602
  134. #include "_PCINT.h"
  135. void setup() {
  136. Serial.begin(115200);
  137. #include "_Slave_Ports_LOCAL_LIST.h"
  138. Slave_Ports_Status_T _add[] = { 1, Active_ControllerID, 0x01, 0x00, 1, 0x01, 0x01, 0x01 };
  139. Slave_Ports_Status_C.list_ports();
  140. //Slave_Ports_Status_C.add_empty();
  141. delay(10000);
  142. //Queue<int> _Slave_Ports_queue = Queue<int>(10);
  143. // LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
  144. //Slave_Ports_Status_T
  145. //struct Slave_Ports_Status_S _add;
  146. // _LCD_print_queue.push(1);
  147. // _LCD_print_queue.push(2);
  148. // _LCD_print_queue.push(3);
  149. //String character = "Geas";
  150. //char b = abc.charAt(0);
  151. //int b_ascii_value = b;
  152. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  153. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  154. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  155. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  156. //todo PCINT service
  157. #include "_PCINT_setup.h"
  158. #if defined(HAS_ROTTARY_SW)
  159. #include "_ROTTARY_SW_setup.h"
  160. #endif
  161. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  162. String LCDarray2="Testing id "; //the string to print onthe LCD
  163. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  164. // put your setup code here, to run once:
  165. #if defined(HAS_LCD1602)
  166. lcd.begin(16, 2);
  167. LCD_print(LCDarray1, LCDarray2, LCDtim/5);
  168. #endif
  169. #if defined(HAS_TFT_ILI9163C)
  170. TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin);
  171. float p = 3.1415926;
  172. display.begin();
  173. display.clearScreen();
  174. display.setCursor(0,0);
  175. display.print(LCDarray1);
  176. display.setCursor(0,10);
  177. display.print(LCDarray2);
  178. delay(LCDtim);
  179. #endif
  180. //BEGIN CAN
  181. //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  182. if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  183. LCDarray1="MCP2515 Init Okay";
  184. LCDarray2="500KBPS 16MHZ";
  185. Serial.print(LCDarray1 + LCDarray2);
  186. #if defined(HAS_LCD1602)
  187. LCD_print(LCDarray1, LCDarray2, LCDtim/5);
  188. #endif
  189. #if defined(HAS_TFT_ILI9163C)
  190. delay(LCDtim);
  191. display.setCursor(0,20);
  192. display.print("MCP2515 Init Okay!!\r\n");
  193. delay(LCDtim);
  194. #endif
  195. } else {
  196. LCDarray1 = "MCP2515 Init Failed";
  197. LCDarray2 = "CAN_CSpin: ";
  198. LCDarray2.concat(CAN_CSpin);
  199. LCDarray2.concat("CAN_INTpin: ");
  200. LCDarray2.concat(CAN_INTpin);
  201. Serial.print(LCDarray1);
  202. #if defined(HAS_LCD1602)
  203. LCD_print(LCDarray1, LCDarray2, LCDtim / 2 ) ;
  204. #endif
  205. #if defined(HAS_TFT_ILI9163C)
  206. delay(LCDtim);
  207. display.setCursor(0,20);
  208. display.print(LCDarray1);
  209. delay(LCDtim);
  210. display.setCursor(0,50);
  211. display.print(LCDarray2);
  212. delay(LCDtim);
  213. #endif
  214. }
  215. #if Active_ControllerID == _Slave1_ID
  216. #include "_SlaveID_setup.h"
  217. #elif Active_ControllerID == _ControllerID
  218. LCDarray1 = "Master Module ";
  219. LCDarray2 = "enabling remotes ";
  220. #endif
  221. Serial.println("");
  222. #if defined(HAS_LCD1602)
  223. LCD_print(LCDarray1, LCDarray2, LCDtim / 5 ) ;
  224. delay(LCDtim * 10);
  225. #endif
  226. #if defined(HAS_TFT_ILI9163C)
  227. delay(LCDtim);
  228. display.setCursor(0,30);
  229. display.print(LCDarray1); display.print(" ");
  230. display.print(LCDarray2);
  231. delay(LCDtim);
  232. #endif
  233. CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
  234. pinMode(CAN_INTpin, INPUT); // Setting pin 2 for /INT input
  235. //EOF CAN
  236. #if defined(HAS_LCD1602)
  237. LCDarray1 = "loop.... ";
  238. LCDarray2 = "....";
  239. LCD_print(LCDarray1, LCDarray2, LCDtim/10);
  240. #endif
  241. //_Slave_Ports_queue.push(_LCD_print_queue.peek() + 1);
  242. Serial.println("#298 _Slave_Ports_queue__list");
  243. _Slave_Ports_queue__INIT(_Slave_Ports_queue_tasks_LIST);
  244. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  245. int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  246. // int _Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  247. //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());
  248. //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 //,
  249. _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, int(0x04) ,0x00 ,100, 5000 ) ; // "_Slave_Ports_broadcast", //4
  250. _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x05 ,0x00 ,100, 5000 ) ; //"_Slave_Ports_set_I2C" //5 - to set ports associated on PW
  251. // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  252. //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  253. //_Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ;
  254. //_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());
  255. // _Slave_Ports_queue_peek = _Slave_Ports_queue.peek() ;
  256. //_Slave_Ports_queue_push = _Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1) ;
  257. // _Slave_Ports_queue_peek_new = _Slave_Ports_queue.peek() ;
  258. //_Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000, _Slave_Ports_queue_peek,
  259. //_Slave_Ports_queue.push(_Slave_Ports_queue.peek() + 1),
  260. //_Slave_Ports_queue.peek());
  261. Serial.println("#301 _Slave_Ports_queue__list");
  262. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  263. Serial.print("#304 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count());
  264. // _Slave_Ports_queue__add( _Slave_Ports_queue_tasks_LIST, 0x03 ,0x00 ,100, 5000 ) ;
  265. //Serial.println("#323 _Slave_Ports_queue__list"); _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST);
  266. // Serial.print("#324 _Slave_Ports_queue.count(), "); Serial.println(_Slave_Ports_queue.count());
  267. }
  268. //#if Active_ControllerID == _ControllerID
  269. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  270. //#endif
  271. //String LCDarray1 ;
  272. //String LCDarray2 ;
  273. unsigned long time;
  274. void loop() {
  275. #include "_PCINT_loop.h"
  276. time = millis();
  277. Serial.print("Time: " );
  278. Serial.println(time);
  279. // put your main code here, to run repeatedly:
  280. //BEGIN CAN
  281. #if Active_ControllerID == _Slave1_ID
  282. #include "_SlaveID_loop1.h"
  283. #elif Active_ControllerID == _ControllerID
  284. #include "_ControllerID_loop1.h"
  285. #endif
  286. _Slave_Ports_queue__list(_Slave_Ports_queue_tasks_LIST) ;
  287. int _Slave_Ports_queue_count = _Slave_Ports_queue.count();
  288. int _Slave_Ports_queue_front = _Slave_Ports_queue.front();
  289. int _Slave_Ports_queue_back = _Slave_Ports_queue.back();
  290. int _Slave_Ports_queue_peek = _Slave_Ports_queue.peek();
  291. int _Slave_Ports_queue_pop = _Slave_Ports_queue.pop();
  292. _Slave_Ports_queue__POP( _Slave_Ports_queue_tasks_LIST,
  293. _Slave_Ports_queue_count,
  294. _Slave_Ports_queue_pop,
  295. _Slave_Ports_queue_front,
  296. _Slave_Ports_queue_back,
  297. _Slave_Ports_queue_peek,
  298. LCDtim, _Slave_Ports);
  299. //_LCD_print_buffors_list(_LCD_print_buffors) ;
  300. //_LCD_print_screens_list(_LCD_print_screens);
  301. }
  302. #include "_PCINT_func.h"
  303. #include "_Slave_Ports_func.h"