P5_Automation_can-dev-res.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. * //#define HAS_I2C_driver_Adafruit_PWMServoDriver
  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. //#define HAS_TFT_ILI9163C
  77. //#define HAS_LCD1602
  78. //#define HAS_ROTTARY_SW
  79. #define HAS__PCINT_setup //to allow interrupts
  80. #define DEBUG 5 //Debug level
  81. #include "__main_struct.h"
  82. #include "__main_struct_funct.h"
  83. #include "__main_struct_limits.h"
  84. #include "_time_struct.h"
  85. #include "__main_I2C_struc.h"
  86. #define _ControllerID 0x101
  87. #define _Slave1_ID 0x103
  88. #include "config_Active_ControllerID.h"
  89. #if defined(HAS_TFT_ILI9163C)
  90. #include "_TFT_ILI9163C_config.h"
  91. #endif
  92. //#include "Active_ControllerID_0x103.h"
  93. //#define Active_ControllerID _ControllerID
  94. //#define Active_ControllerID _Slave1_ID
  95. #include "_DISPLAY_struct.h"
  96. #include "_Port_ID_sruct.h"
  97. #include "_TASK_TYPES_struct.h"
  98. #include "_Slave_Ports_Status_QUEUE_struct.h"
  99. //#include "Slave_Ports_Status_QUEUE.h"
  100. #include "_I2C_driver_Adafruit_PWMServoDriver_struct.h"
  101. #if defined(HAS_ROTTARY_SW)
  102. #include "_ROTTARY_SW_config.h"
  103. #endif
  104. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  105. #include "_I2C_driver_Adafruit_PWMServoDriver_config.h"
  106. #endif
  107. #if defined(HAS_ROTTARY_SW)
  108. #include "_ROTTARY_SW_struct.h"
  109. #endif
  110. #include "Queue.h"
  111. Queue<int> _Slave_Ports_queue = Queue<int>(10);
  112. #if Active_ControllerID == _ControllerID
  113. String lcdPattern = "s" ;
  114. #endif
  115. #if Active_ControllerID == _Slave1_ID
  116. String lcdPattern = "." ;
  117. #endif
  118. //BEGIN 1602
  119. #if defined(HAS_LCD1602)
  120. #include <LiquidCrystal.h>
  121. // initialize the library with the numbers of the interface pins
  122. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  123. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  124. #include "_LCD1602_struct.h"
  125. // #include "_LCD1602_init.h"
  126. #endif
  127. #if defined(HAS_TFT_ILI9163C)
  128. #include "_TFT_ILI9163C_struct.h"
  129. #endif
  130. #if defined(HAS__PCINT_setup)
  131. #include "_PCINT_struct.h"
  132. #endif
  133. //BEGIN CAN
  134. #include "_CAN_init.h"
  135. //EOF CAN
  136. //REMOTE PORTS DEFINITIONS
  137. #include "_PortTypes_struct.h"
  138. #include "_PortStates_struct.h"
  139. #include "_Port_Exp_state_ttl_struct.h"
  140. #include "_Port_Broadcast_state_struct.h"
  141. #include "_Port_I2C_driver_struct.h"
  142. #include "_Slave_Ports_Status_struct.h"
  143. #include "_Slave_Ports_Protocol_CAN_struct.h"
  144. #if defined(HAS_TFT_ILI9163C)
  145. #include "_TFT_ILI9163C_func.h"
  146. #endif
  147. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  148. #include "_I2C_driver_Adafruit_PWMServoDriver_func.h"
  149. #endif
  150. #include "_TASK_TYPES_func.h"
  151. #include "_Slave_Ports_Status_QUEUE_add_func.h"
  152. #include "_DISPLAY_add_record_func.h"
  153. #include "_DISPLAY_func.h"
  154. #if defined(HAS_LCD1602)
  155. #include "_LCD1602_func.h"
  156. //#include "_LCD1602_init.h"
  157. #endif
  158. #if defined(HAS_ROTTARY_SW)
  159. #include "_ROTTARY_SW_func.h"
  160. #endif
  161. #include "_time_func.h"
  162. #include "_Slave_Ports_ttl_decrement_func.h"
  163. #include "_Slave_Ports_broadcast_func.h"
  164. #include "_Slave_Ports_I2C_func.h"
  165. #include "_DISPLAY_queue_pop_func.h"
  166. #include "_Slave_Ports_Status_QUEUE_func.h"
  167. #include "_Slave_Ports_Protocol_CAN_func.h"
  168. #include "_Slave_Ports_Status_func.h"
  169. #include "_Slave_Ports.h"
  170. // #include "_Slave_Ports_ttl_decrement_POP.h"
  171. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  172. //#define _CAN_P_0x102_PWMServoDriver0x40
  173. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  174. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  175. // #define __CSpin 53 // 10 // chip select
  176. #if defined(HAS_LCD1602)
  177. #include "_LCD1602_init.h"
  178. #endif
  179. #if defined(HAS_TFT_ILI9163C)
  180. #include "_TFT_ILI9163C_init.h"
  181. #endif
  182. #include "_DISPLAY_init.h"
  183. #if defined(HAS_ROTTARY_SW)
  184. #include "_ROTTARY_SW_init.h"
  185. #endif
  186. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  187. #include "_I2C_driver_Adafruit_PWMServoDriver_init.h"
  188. #endif
  189. #if defined(HAS__PCINT_setup)
  190. #include "_PCINT_init.h"
  191. #endif
  192. #include "config_Slave_Ports_LOCAL_init.h"
  193. #include "__main_init.h"
  194. void setup() {
  195. Serial.begin(115200);
  196. // Serial.begin(9600);
  197. // Serial.println("#233 setup" );
  198. // _LCD_print_queue.push(1);
  199. // _LCD_print_queue.push(2);
  200. // _LCD_print_queue.push(3);
  201. //String character = "Geas";
  202. //char b = abc.charAt(0);
  203. //int b_ascii_value = b;
  204. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  205. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  206. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  207. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  208. //todo PCINT service
  209. #if defined(HAS__PCINT_setup)
  210. #include "_PCINT_setup.h"
  211. #endif
  212. #if defined(HAS_ROTTARY_SW)
  213. #include "_ROTTARY_SW_setup.h"
  214. #endif
  215. /* TODO
  216. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  217. String LCDarray2="Testing id "; //the string to print onthe LCD
  218. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  219. */
  220. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  221. #include "_I2C_driver_Adafruit_PWMServoDriver_setup.h"
  222. #endif
  223. // put your setup code here, to run once:
  224. #if defined(HAS_LCD1602)
  225. #include "_LCD1602_setup.h"
  226. #endif
  227. #if defined(HAS_TFT_ILI9163C)
  228. #include "_TFT_ILI9163C_setup.h"
  229. #endif
  230. //BEGIN CAN
  231. #include "_CAN_setup.h"
  232. #if defined(HAS_LCD1602)
  233. /* todo
  234. LCDarray1 = "loop.... ";
  235. LCDarray2 = "....";
  236. LCD_print(LCDarray1, LCDarray2, LCDtim/10);
  237. */
  238. #endif
  239. #include "__main_setup.h"
  240. }
  241. //#if Active_ControllerID == _ControllerID
  242. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  243. //#endif
  244. //String LCDarray1 ;
  245. //String LCDarray2 ;
  246. unsigned long time;
  247. void loop() {
  248. #if defined(HAS__PCINT_setup)
  249. #include "_PCINT_loop.h"
  250. #endif
  251. #if defined(HAS_ROTTARY_SW)
  252. #include "_ROTTARY_SW_loop.h"
  253. #endif
  254. time = millis();
  255. Serial.print("Time: " ); Serial.println(time);
  256. #include "__main_loop_top.h"
  257. Serial.print("#331_L1 main loop top ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init ");
  258. Serial.println(ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init);
  259. Serial.print("#331_L2 main loop top ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init=[");
  260. Serial.println(ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init);
  261. Serial.print(" true["); Serial.print(true); Serial.print("] false["); Serial.print(false);
  262. Serial.println("] ");
  263. Slave_Ports_Status__DISPLAY_TARGET(
  264. Slave_Ports_Status_A ,
  265. Slave_Ports_Status_T_ARDUINO_ATTR_ARRAY_A,
  266. 0 ,
  267. DISPLAY_A ,
  268. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  269. DISPLAY_PHYSICAL_CHAR_A,
  270. DISPLAY_pos_seq_A_NEXT + DISPLAY_pos_seq_A_IMMEDIATE,
  271. "336 PORTS:",
  272. DISPLAY_TARGET_DISPLAY128,
  273. millis(),
  274. millis() + ARDUINO_time_seq_id_A_INCREMENT
  275. //queue
  276. ,Slave_Ports_Status_QUEUE_A,
  277. TASK_TYPE_T__EMPTY__JUST_PASSED_ARG__,
  278. TASK_PORT_ASSOC_EMPTY
  279. );
  280. // Serial.println(time);
  281. #include "_SlaveID_loop1.h" //todo PCINT
  282. #include "_ControllerID_loop1.h" //todo make default
  283. #include "__main_loop_end.h"
  284. // DISPLAY_DEV.setCursor(10,10);
  285. // DISPLAY_DEV.print(" Test307");
  286. /*
  287. DISPLAY__print_ANY(
  288. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  289. DISPLAY_PHYSICAL_CHAR_A,
  290. DISPLAY_pos_seq_A_NEXT,
  291. "123456789012345678901234567890"
  292. ); */
  293. delay(10000);
  294. }
  295. #include "_PCINT_func_loop.h" //todo empty implement display etc
  296. #include "_Slave_Ports_func_loop.h" //todo implement