P5_Automation_can-dev-res.ino 13 KB

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