P5_Automation_can-dev-res.ino 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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_DEBUG_ACTION_TRIG
  77. //#define DEBUG_I2C //to debug _Slave_Ports_I2C_POP etc
  78. //#define HAS_TFT_ILI9163C
  79. //#define HAS_LCD1602
  80. //#define HAS_ROTTARY_SW
  81. //#define DEBUG_I2C_driver_Adafruit_PWMServoDriver
  82. //#define HAS__PCINT_setup //to allow interrupts
  83. #define HAS__jm_Scheduler //to allow planned actions blink etc
  84. #if defined(HAS__jm_Scheduler)
  85. #include "jm_Scheduler.h"
  86. #endif
  87. //Debug level
  88. //#define DEBUG 5
  89. //#define DISABLE_LOOP //To debug and disable any output
  90. #include "___main/__main_struct.h"
  91. #include "___main/__main_trigger_struct.h"
  92. #include "___main/__main_struct_funct.h"
  93. #include "___main/__main_struct_limits.h"
  94. #include "_time_struct.h"
  95. #include "_DRIVER_STATUS/_DRIVER_STATUS_struct.h"
  96. #include "___main/__main_I2C_struc.h"
  97. #include "___main/__main_static.h"
  98. #include "___main/__main_auto.h"
  99. #include "___main/__main_register.h"
  100. #include "___main/__main_extern.h"
  101. #include "___template/__STORAGE_ARRAY_func.h" //testing
  102. #define _ControllerID 0x101
  103. #define _Slave1_ID 0x103
  104. //#include "Active_ControllerID_0x103.h"
  105. //#define Active_ControllerID _ControllerID
  106. //#define Active_ControllerID _Slave1_ID
  107. #include "___CONFIG/config_Active_ControllerID.h" //there kleep id of device
  108. //#include "config_Slave_Ports_LOCAL_0x101_init.h" //there kleep config of this id device
  109. #ifdef DEBUG
  110. #define DPRINT(...) Serial.print(__VA_ARGS__)
  111. #define DPRINTLN(...) Serial.println(__VA_ARGS__)
  112. #define DRINTF(...) Serial.print(F(__VA_ARGS__))
  113. #define DPRINTLNF(...) Serial.println(F(__VA_ARGS__))
  114. DPRINTLN("#125 [DEBUG ENABLED]");
  115. //DPRINTLN(0xC0FFEEul,DEC);
  116. //DPRINTLN(12648430ul,HEX);
  117. #else
  118. #define DPRINT(...) //blank line
  119. #define DPRINTLN(...) //blank line
  120. #define DPRINTF(...) //blank line
  121. #define DPRINTLNF(...) //blank line
  122. #endif
  123. #if Active_ControllerID == 0x101
  124. #include "___CONFIG/0x101/config_Active_ControllerID_0x101.h"
  125. #endif
  126. #if Active_ControllerID == 0x103
  127. #include "___CONFIG/0x103/config_Active_ControllerID_0x103.h" //there kleep config of this id device
  128. #endif
  129. #if defined(HAS_TFT_ILI9163C)
  130. #include "_TFT_ILI9163C_config.h"
  131. #endif
  132. #include "_DISPLAY_struct.h"
  133. #include "_Port_ID_sruct.h"
  134. #include "_TASK_TYPES_struct.h"
  135. #include "_Slave_Ports_Status_QUEUE_struct.h"
  136. //#include "Slave_Ports_Status_QUEUE.h"
  137. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_struct.h"
  138. #if defined(HAS_ROTTARY_SW)
  139. #include "_ROTTARY_SW/_ROTTARY_SW_config.h"
  140. #endif
  141. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  142. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_config.h"
  143. #endif
  144. #if defined(HAS_ROTTARY_SW)
  145. #include "_ROTTARY_SW/_ROTTARY_SW_struct.h"
  146. #endif
  147. #include "Queue.h"
  148. //#include "TPQ.h"
  149. Queue<int> _Slave_Ports_queue = Queue<int>(10);
  150. #if Active_ControllerID == _ControllerID
  151. String lcdPattern = "s" ;
  152. #endif
  153. #if Active_ControllerID == _Slave1_ID
  154. String lcdPattern = "." ;
  155. #endif
  156. //BEGIN 1602
  157. #if defined(HAS_LCD1602)
  158. #include <LiquidCrystal.h>
  159. // initialize the library with the numbers of the interface pins
  160. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  161. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  162. #include "_LCD1602_struct.h"
  163. // #include "_LCD1602_init.h"
  164. #endif
  165. //#if defined(HAS_TFT_ILI9163C)
  166. #include "_TFT_ILI9163C_struct.h"
  167. //#endif
  168. #if defined(HAS__PCINT_setup)
  169. #include "_PCINT/_PCINT_struct.h"
  170. #endif
  171. //BEGIN CAN
  172. #include "_CAN_init.h"
  173. //EOF CAN
  174. //REMOTE PORTS DEFINITIONS
  175. #include "_PortTypes_struct.h"
  176. #include "_PortStates_struct.h"
  177. #include "_Port_Exp_state_ttl_struct.h"
  178. #include "_Port_Broadcast_state_struct.h"
  179. #include "_Port_I2C_driver_struct.h"
  180. #include "_Slave_Ports_Status_struct.h"
  181. #include "_Slave_Ports_Protocol_CAN_struct.h"
  182. #if defined(HAS_TFT_ILI9163C)
  183. #include "_TFT_ILI9163C_func.h"
  184. #endif
  185. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  186. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_func.h"
  187. #endif
  188. #include "_TASK_TYPES_func.h"
  189. #include "_Slave_Ports_Status_QUEUE_add_func.h"
  190. #include "_DISPLAY_add_record_func.h"
  191. #include "_DISPLAY_func.h"
  192. #if defined(HAS_LCD1602)
  193. #include "_LCD1602_func.h"
  194. //#include "_LCD1602_init.h"
  195. #endif
  196. #if defined(HAS_ROTTARY_SW)
  197. #include "_ROTTARY_SW/_ROTTARY_SW_func.h"
  198. #endif
  199. #include "_time_func.h"
  200. #include "_Slave_Ports_ttl_decrement_func.h"
  201. #include "_Slave_Ports_broadcast_func.h"
  202. #include "_Slave_Ports_I2C_func.h"
  203. #include "_DISPLAY_queue_pop_func.h"
  204. #include "_Slave_Ports_Status_QUEUE_func.h"
  205. #include "_Slave_Ports_Protocol_CAN_func.h"
  206. #include "_Slave_Ports_Status_func.h"
  207. #include "_Slave_Ports.h"
  208. // #include "_Slave_Ports_ttl_decrement_POP.h"
  209. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  210. //#define _CAN_P_0x102_PWMServoDriver0x40
  211. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  212. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  213. // #define __CSpin 53 // 10 // chip select
  214. #if defined(HAS_LCD1602)
  215. #include "_LCD1602_init.h"
  216. #endif
  217. #if defined(HAS_TFT_ILI9163C)
  218. #include "_TFT_ILI9163C_init.h"
  219. #endif
  220. #include "_DISPLAY_init.h"
  221. #if defined(HAS_ROTTARY_SW)
  222. #include "_ROTTARY_SW/_ROTTARY_SW_init.h"
  223. #endif
  224. #if defined(HAS__PCINT_setup)
  225. #include "_PCINT/_PCINT_init.h"
  226. #endif
  227. #include "___CONFIG/config_Slave_Ports_LOCAL_init.h"
  228. #if Active_ControllerID == 0x101
  229. #include "___CONFIG/0x101/config_Slave_Ports_LOCAL_0x101_init.h"
  230. #endif
  231. #if Active_ControllerID == 0x103
  232. #include "___CONFIG/0x103/config_Slave_Ports_LOCAL_0x103_init.h"
  233. #endif
  234. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  235. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_init.h"
  236. #endif
  237. #include "___main/__main_init.h"
  238. #ifdef DISABLE_LOOP
  239. #include "___DEBUG/___DEBUG_DISABLE_INIT.h"
  240. #endif
  241. void setup() { //REMEMBER NOT INITIALIZE ANY VARIABLES OR ARRAYS
  242. Serial.begin(115200);
  243. #ifdef DISABLE_LOOP || DEBUG
  244. Serial.println("#329 Serial.begin(115200)");
  245. #endif
  246. // Serial.begin(9600);
  247. // Serial.println("#233 setup" );
  248. // _LCD_print_queue.push(1);
  249. // _LCD_print_queue.push(2);
  250. // _LCD_print_queue.push(3);
  251. //String character = "Geas";
  252. //char b = abc.charAt(0);
  253. //int b_ascii_value = b;
  254. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  255. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  256. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  257. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  258. //todo PCINT service
  259. #ifdef DISABLE_LOOP || DEBUG
  260. Serial.println("#375 will include ___main/__main_I2C_setup.h");
  261. #endif
  262. #include "___main/__main_I2C_setup.h"
  263. #if defined(HAS__PCINT_setup)
  264. #include "_PCINT/_PCINT_setup.h" //empty
  265. #endif
  266. #if defined(HAS_ROTTARY_SW)
  267. #include "_ROTTARY_SW/_ROTTARY_SW_setup.h" //Attach interrupts
  268. #endif
  269. /* TODO
  270. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  271. String LCDarray2="Testing id "; //the string to print onthe LCD
  272. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  273. */
  274. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  275. #ifdef DISABLE_LOOP || DEBUG || DEBUG_I2C_driver_Adafruit_PWMServoDriver
  276. Serial.println("#401 will include /_I2C_driver_Adafruit_PWMServoDriver_setup.h");
  277. #endif
  278. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_setup.h"
  279. #endif
  280. // put your setup code here, to run once:
  281. #if defined(HAS_LCD1602)
  282. #include "_LCD1602_setup.h"
  283. #endif
  284. #if defined(HAS_TFT_ILI9163C)
  285. #include "_TFT_ILI9163C_setup.h"
  286. #endif
  287. //BEGIN CAN
  288. #include "_CAN_setup.h"
  289. #if defined(HAS_LCD1602)
  290. /* todo
  291. LCDarray1 = "loop.... ";
  292. LCDarray2 = "....";
  293. LCD_print(LCDarray1, LCDarray2, LCDtim/10);
  294. */
  295. #endif
  296. #ifndef DISABLE_LOOP
  297. #include "___main/__main_setup.h"
  298. #endif
  299. #ifdef DISABLE_LOOP
  300. #include "___DEBUG/___DEBUG_DISABLE_SETUP.h"
  301. #endif
  302. }
  303. //#if Active_ControllerID == _ControllerID
  304. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  305. //#endif
  306. //String LCDarray1 ;
  307. //String LCDarray2 ;
  308. unsigned long time;
  309. ARDUINO_LOOP_LATENCY_T ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END( ARDUINO_LOOP_LATENCY_T
  310. ARDUINO_LOOP_LATENCY__MAIN_END_A) {
  311. static ARDUINO_LOOP_LATENCY_COUNTER_T ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A = ARDUINO_LOOP_LATENCY__MAIN_END_A;
  312. if(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A == 0 ) {
  313. ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A = ARDUINO_LOOP_LATENCY__MAIN_END_A;
  314. #ifdef DEBUG
  315. Serial.print("] COUNTER__MAIN_END_A["); Serial.print(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A); Serial.print("] C["); Serial.print(ARDUINO_LOOP_LATENCY__MAIN_END_A); Serial.println("] ");
  316. #endif
  317. } else {
  318. return ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A-- ;
  319. }
  320. }
  321. ARDUINO_LOOP_LATENCY_T ARDUINO_LOOP_LATENCY_COUNTER__MAIN( ARDUINO_LOOP_LATENCY_T
  322. ARDUINO_LOOP_LATENCY__MAIN_A) {
  323. static ARDUINO_LOOP_LATENCY_COUNTER_T ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A = ARDUINO_LOOP_LATENCY__MAIN_A;
  324. if(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A == 0 ) {
  325. #ifdef DEBUG
  326. Serial.print("] COUNTER__MAIN_A["); Serial.print(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A); Serial.print("] C["); Serial.print(ARDUINO_LOOP_LATENCY__MAIN_A); Serial.println("] ");
  327. #endif
  328. ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A = ARDUINO_LOOP_LATENCY__MAIN_A;
  329. } else {
  330. return ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A-- ;
  331. }
  332. }
  333. void loop() {
  334. #include "___loop/___loop_init.h"
  335. #if defined(HAS__PCINT_setup)
  336. #include "_PCINT/_PCINT_loop.h"
  337. #endif
  338. #if defined(HAS_ROTTARY_SW)
  339. #include "_ROTTARY_SW/_ROTTARY_SW_loop.h"
  340. #endif
  341. time = millis();
  342. #ifdef DISABLE_LOOP
  343. #include "___DEBUG/___DEBUG_DISABLE_LOOP.h"
  344. #endif
  345. #include "___loop/__main_loop_top.h"
  346. if(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END(ARDUINO_LOOP_LATENCY__MAIN_END_A) <= ARDUINO_LOOP_LATENCY_COUNTER_INIT ) {
  347. #ifdef DEBUG
  348. #ifndef DISABLE_LOOP
  349. if(DEBUG_ACTION_TRIG == 1) Serial.print("] Time: " ); Serial.println(time);
  350. #endif
  351. #endif
  352. if(DEBUG_ACTION_TRIG == 1 or (time % 100000 == 0)) {
  353. Serial.print("Time: " ); Serial.println(time);
  354. }
  355. #ifndef DISABLE_LOOP
  356. if(DEBUG_ACTION_TRIG == 1) {
  357. #ifdef DEBUG
  358. Serial.print("#331_L1 main loop top ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init ");
  359. Serial.println(ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init);
  360. Serial.print("#331_L2 main loop top ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init=[");
  361. Serial.println(ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init);
  362. Serial.print(" true["); Serial.print(true); Serial.print("] false["); Serial.print(false);
  363. Serial.println("] ");
  364. #endif
  365. }
  366. Slave_Ports_Status__DISPLAY_TARGET(
  367. Slave_Ports_Status_A ,
  368. Slave_Ports_Status_T_ARDUINO_ATTR_ARRAY_A,
  369. 0 ,
  370. DISPLAY_A ,
  371. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  372. DISPLAY_PHYSICAL_CHAR_A,
  373. DISPLAY_pos_seq_A_NEXT + DISPLAY_pos_seq_A_IMMEDIATE ,
  374. "PORTS:",
  375. DISPLAY_TARGET_DISPLAY128,
  376. millis(),
  377. millis() + ARDUINO_time_seq_id_A_INCREMENT
  378. //queue
  379. ,Slave_Ports_Status_QUEUE_A,
  380. TASK_TYPE_T__EMPTY__JUST_PASSED_ARG__,
  381. TASK_PORT_ASSOC_EMPTY
  382. );
  383. #else
  384. #include "___DEBUG/___DEBUG_DISABLE_LOOP_MAIN_A.h"
  385. #endif
  386. }
  387. // Serial.println(time);
  388. #include "_SlaveID_loop1.h" //todo PCINT
  389. #include "_ControllerID_loop1.h" //todo make default
  390. #include "___loop/__main_loop_end.h"
  391. // DISPLAY_DEV.setCursor(10,10);
  392. // DISPLAY_DEV.print(" Test307");
  393. /*
  394. DISPLAY__print_ANY(
  395. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  396. DISPLAY_PHYSICAL_CHAR_A,
  397. DISPLAY_pos_seq_A_NEXT,
  398. "123456789012345678901234567890"
  399. ); */
  400. delay(10);
  401. if(DEBUG_ACTION_TRIG != DEBUG_LAST_ACTION_TRIG) {
  402. Serial.print("#534 DEBUG_ACTION_TRIG[");Serial.print(DEBUG_ACTION_TRIG);Serial.print("] "); Serial.print("] LAST_DEBUG_ACTION_TRIG[");Serial.print(DEBUG_LAST_ACTION_TRIG);Serial.println("] ");
  403. // Serial.print("] COUNTER__MAIN_A["); Serial.print(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A);
  404. // Serial.print("] COUNTER__MAIN_END_A["); Serial.print(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A);
  405. }
  406. DEBUG_LAST_ACTION_TRIG = DEBUG_ACTION_TRIG ;
  407. }
  408. #include "_PCINT/_PCINT_func_loop.h" //todo empty implement display etc
  409. #include "_Slave_Ports_func_loop.h" //todo implement