P5_Automation_can-dev-res-working-1930.ino 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 DEBUG_CAN
  83. //#define DEBUG_QUEUE
  84. //#define DEBUG_QUEUE_ASSERT
  85. //#define DEBUG_QUEUE_WAIT 500
  86. //#define DEBUG_STRUCT_INIT
  87. //#define DEBUG_STORAGE_QUEUE_TEST
  88. //#define DEBUG_ARDUINO_ATTR_ARRAY_S__commit_added_element_QUEUE
  89. //#define DEBUG_ARDUINO_ARRAY_INDEX_NEXT_LOCK
  90. //#define HAS__PCINT_setup //to allow interrupts
  91. //#define DEBUG_TASK_TYPE_T_Slave_Ports_broadcast____
  92. //#define DEBUG_TASK_TYPE_T_DISPLAY_queue_pop________
  93. //#define DEBUG_TASK_TYPE_T_Slave_Ports_set_I2C______
  94. //#define DEBUG_QUEUE_ASSERT todo not working
  95. //#define DEBUG_LAST_ACTION_TRIG_ENABLE
  96. #define DISABLE_DISPLAY__add_record
  97. #define DISPLAY_array16_T_is_DISPLAY_string_T
  98. #define ENABLE_DISPLAY_MENU
  99. #define USE_DISPLAY_string_T
  100. //#define USE_DISPLAY_char_PA //dev testing pointer to display
  101. //#define DISABLE_ANY_PRINT //when uno ?
  102. //#define DEBUG 3
  103. //#define DISABLE_LOOP
  104. #define DISABLE_QUEUE_REASSIGN
  105. //#define DEBUG_LATENCY_MARKER
  106. //ports assign config
  107. #define PortA8 A8 //for rottary sw
  108. #define PortA9 A9 //for rottary sw dt
  109. #define PortA10 A10 //for rottary sw clk
  110. #define Port7 7 //for TFT_ILI9163C_CSpin 7
  111. #define Port8 8 //for TFT_ILI9163C_DCpin 8
  112. #define Port9 9 //for TFT_ILI9163C__RSTpin 9
  113. #define Port10 10 // CAN_INTpin 10
  114. #define Port12 12 // todo change CAN to 12 from 53
  115. #define Port53 53 // #define CAN_CSpin 53 //todo change can be 12?
  116. #define Sprintln(a) (Serial.println(a))
  117. /*
  118. Then instead of
  119. Serial.println(F("Hello world!"));
  120. write
  121. Sprintln(F("Hello world!"));
  122. etc. To deactivate the Serial printing, define the macro empty:
  123. #define Sprintln(a)
  124. */
  125. #include "___CONFIG/config_Active_ControllerID.h" //there kleep id of device
  126. #define HAS__jm_Scheduler //to allow planned actions blink etc
  127. #if defined(HAS__jm_Scheduler)
  128. #include "jm_Scheduler.h"
  129. #endif
  130. //Debug level
  131. //#define DEBUG 5
  132. //#define DISABLE_LOOP //To debug and disable any output
  133. #include "___main/__main_struct.h"
  134. #include "___main/__main_trigger_struct.h"
  135. #include "___main/__main_struct_funct.h"
  136. #include "___main/__main_struct_limits.h"
  137. #include "_time_struct.h"
  138. #include "_DRIVER_STATUS/_DRIVER_STATUS_struct.h"
  139. #include "___main/__main_I2C_struc.h"
  140. #include "___main/__main_static.h"
  141. #include "___main/__main_auto.h"
  142. #include "___main/__main_register.h"
  143. #include "___main/__main_extern.h"
  144. #include "___template/__STORAGE_ARRAY_func.h" //testing
  145. #define _ControllerID 0x11
  146. #define _Slave1_ID 0x13
  147. //#include "Active_ControllerID_0x103.h"
  148. //#define Active_ControllerID _ControllerID
  149. //#define Active_ControllerID _Slave1_ID
  150. //#include "config_Slave_Ports_LOCAL_0x101_init.h" //there kleep config of this id device
  151. #define print_SEP_RT "]"
  152. #if Active_ControllerID == 0x11
  153. const int Active_ControllerID_HEX = 0x11 ;
  154. #include "___CONFIG/0x11/config_Active_ControllerID_0x11.h"
  155. #endif
  156. #if Active_ControllerID == 0x13
  157. const int Active_ControllerID_HEX = 0x13 ;
  158. #include "___CONFIG/0x13/config_Active_ControllerID_0x13.h" //there kleep config of this id device
  159. #endif
  160. #if Active_ControllerID == 0x14
  161. const int Active_ControllerID_HEX = 0x14 ;
  162. #include "___CONFIG/0x14/config_Active_ControllerID_0x14.h" //there kleep config of this id device
  163. #endif
  164. #if Active_ControllerID == 0x15
  165. const int Active_ControllerID_HEX = 0x15 ;
  166. #include "___CONFIG/0x15/config_Active_ControllerID_0x15.h" //atmega bez wyswietlacza druga i bez portow na razie
  167. #endif
  168. #if defined(HAS_TFT_ILI9163C)
  169. #include "_TFT_ILI9163C_config.h"
  170. #endif
  171. #include "_DISPLAY_struct.h"
  172. #include "_Port_ID_sruct.h"
  173. #include "_TASK_TYPES_struct.h"
  174. #include "_Slave_Ports_Status_QUEUE_struct.h"
  175. //#include "Slave_Ports_Status_QUEUE.h"
  176. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_struct.h"
  177. #include "_CAN/_CAN_config.h"
  178. #if defined(HAS_ROTTARY_SW)
  179. #include "_ROTTARY_SW/_ROTTARY_SW_config.h"
  180. #endif
  181. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  182. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_config.h"
  183. #endif
  184. #if defined(HAS_ROTTARY_SW)
  185. #include "_ROTTARY_SW/_ROTTARY_SW_struct.h"
  186. #endif
  187. #include "Queue.h"
  188. //#include "TPQ.h"
  189. Queue<int> _Slave_Ports_queue = Queue<int>(ARDUINO_ARRAY_INDEX_LIMIT_QUEUE);
  190. Queue<int> TEST_QUEUE = Queue<int>(ARDUINO_ARRAY_INDEX_LIMIT_QUEUE);
  191. #if Active_ControllerID == _ControllerID
  192. String lcdPattern = "s" ;
  193. #endif
  194. #if Active_ControllerID == _Slave1_ID
  195. String lcdPattern = "." ;
  196. #endif
  197. //BEGIN 1602
  198. #if defined(HAS_LCD1602)
  199. #include <LiquidCrystal.h>
  200. // initialize the library with the numbers of the interface pins
  201. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  202. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  203. #include "_LCD1602_struct.h"
  204. // #include "_LCD1602_init.h"
  205. #endif
  206. //#if defined(HAS_TFT_ILI9163C)
  207. #include "_TFT_ILI9163C_struct.h"
  208. //#endif
  209. #if defined(HAS__PCINT_setup)
  210. #include "_PCINT/_PCINT_struct.h" //pins MYPIN1 MYPIN2 MYPIN3
  211. #endif
  212. //BEGIN CAN
  213. #include "_CAN/_CAN_init.h"
  214. //EOF CAN
  215. //REMOTE PORTS DEFINITIONS
  216. #include "_PortTypes_struct.h"
  217. #include "_PortStates_struct.h"
  218. #include "_Port_Exp_state_ttl_struct.h"
  219. #include "_Port_Broadcast_state_struct.h"
  220. #include "_Port_I2C_driver_struct.h"
  221. #include "_Slave_Ports_Status_struct.h"
  222. #include "_CAN/_Slave_Ports_Protocol_CAN_struct.h"
  223. #include "__icon/__icon.h"
  224. #if defined(HAS_TFT_ILI9163C)
  225. #include "_TFT_ILI9163C_func.h"
  226. #endif
  227. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  228. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_func.h"
  229. #endif
  230. #include "_TASK_TYPES_func.h"
  231. #include "_Slave_Ports_Status_QUEUE_add_func.h"
  232. #include "_DISPLAY_add_record_func.h"
  233. #include "_DISPLAY_func.h"
  234. #if defined(HAS_LCD1602)
  235. #include "_LCD1602_func.h"
  236. //#include "_LCD1602_init.h"
  237. #endif
  238. #if defined(HAS_ROTTARY_SW)
  239. #include "_ROTTARY_SW/_ROTTARY_SW_func.h"
  240. #endif
  241. #include "_time_func.h"
  242. #include "_Slave_Ports_ttl_decrement_func.h"
  243. #include "_Slave_Ports_broadcast_func.h"
  244. #include "_Slave_Ports_I2C_func.h"
  245. #include "_Slave_Ports_action_func.h"
  246. #include "_DISPLAY_queue_pop_func.h"
  247. #include "_Slave_Ports_Status_QUEUE_func.h"
  248. #include "_Slave_Ports_Protocol_CAN_func.h"
  249. #include "_Slave_Ports_Status_func.h"
  250. #include "_CAN/_CAN_func.h"
  251. #include "_Slave_Ports.h"
  252. // #include "_Slave_Ports_ttl_decrement_POP.h"
  253. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  254. //#define _CAN_P_0x102_PWMServoDriver0x40
  255. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  256. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  257. // #define __CSpin 53 // 10 // chip select
  258. #if defined(HAS_LCD1602)
  259. #include "_LCD1602_init.h"
  260. #endif
  261. #if defined(HAS_TFT_ILI9163C)
  262. #include "_TFT_ILI9163C_init.h"
  263. #endif
  264. #include "_DISPLAY_init.h"
  265. #if defined(HAS_ROTTARY_SW)
  266. #include "_ROTTARY_SW/_ROTTARY_SW_init.h" //empty
  267. #endif
  268. #if defined(HAS__PCINT_setup)
  269. #include "_PCINT/_PCINT_init.h"
  270. #endif
  271. #include "___CONFIG/config_Slave_Ports_LOCAL_init.h"
  272. #if Active_ControllerID == 0x11
  273. #include "___CONFIG/0x11/config_Slave_Ports_LOCAL_0x11_init.h"
  274. #endif
  275. #if Active_ControllerID == 0x13
  276. #include "___CONFIG/0x13/config_Slave_Ports_LOCAL_0x13_init.h"
  277. #endif
  278. #if Active_ControllerID == 0x14
  279. #include "___CONFIG/0x14/config_Slave_Ports_LOCAL_0x14_init.h"
  280. #endif
  281. #if Active_ControllerID == 0x15
  282. #include "___CONFIG/0x15/config_Slave_Ports_LOCAL_0x15_init.h"
  283. #endif
  284. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  285. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_init.h"
  286. #endif
  287. #include "___main/__main_init.h"
  288. #ifdef DISABLE_LOOP
  289. #include "___DEBUG/___DEBUG_DISABLE_INIT.h"
  290. #endif
  291. void setup() { //REMEMBER NOT INITIALIZE ANY VARIABLES OR ARRAYS
  292. Serial.begin(115200);
  293. #if defined(DEBUG) || defined(DEBUG_QUEUE)
  294. Serial.println("#329 Serial.begin(115200)");
  295. #endif
  296. // Serial.begin(9600);
  297. // Serial.println("#233 setup" );
  298. // _LCD_print_queue.push(1);
  299. // _LCD_print_queue.push(2);
  300. // _LCD_print_queue.push(3);
  301. //String character = "Geas";
  302. //char b = abc.charAt(0);
  303. //int b_ascii_value = b;
  304. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  305. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  306. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  307. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  308. //todo PCINT service
  309. #if defined(DISABLE_LOOP) || defined(DEBUG)
  310. Serial.println("#375 will include ___main/__main_I2C_setup.h");
  311. #endif
  312. #include "___main/__main_I2C_setup.h"
  313. #if defined(HAS__PCINT_setup)
  314. #include "_PCINT/_PCINT_setup.h" //empty
  315. #endif
  316. #if defined(HAS_ROTTARY_SW)
  317. #include "_ROTTARY_SW/_ROTTARY_SW_setup.h" //Attach interrupts
  318. #endif
  319. /* TODO
  320. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  321. String LCDarray2="Testing id "; //the string to print onthe LCD
  322. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  323. */
  324. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  325. #if defined(DISABLE_LOOP) || defined(DEBUG) || defined(DEBUG_I2C_driver_Adafruit_PWMServoDriver)
  326. Serial.println("#401 will include /_I2C_driver_Adafruit_PWMServoDriver_setup.h");
  327. #endif
  328. #include "_I2C_driver_Adafruit_PWMServoDriver/_I2C_driver_Adafruit_PWMServoDriver_setup.h"
  329. #endif
  330. // put your setup code here, to run once:
  331. #if defined(HAS_LCD1602)
  332. #include "_LCD1602_setup.h"
  333. #endif
  334. #if defined(HAS_TFT_ILI9163C)
  335. #include "_TFT_ILI9163C_setup.h"
  336. #endif
  337. //BEGIN CAN
  338. #include "_CAN/_CAN_setup.h"
  339. #if defined(HAS_LCD1602)
  340. /* todo
  341. LCDarray1 = "loop.... ";
  342. LCDarray2 = "....";
  343. LCD_print(LCDarray1, LCDarray2, LCDtim/10);
  344. */
  345. #endif
  346. #ifndef DISABLE_LOOP
  347. #include "___main/__main_setup.h"
  348. #endif
  349. #ifdef DISABLE_LOOP
  350. #include "___DEBUG/___DEBUG_DISABLE_SETUP.h"
  351. #endif
  352. }
  353. //#if Active_ControllerID == _ControllerID
  354. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  355. //#endif
  356. //String LCDarray1 ;
  357. //String LCDarray2 ;
  358. unsigned long time;
  359. #include "___loop/_LOOP_LATENCY_COUNTER.h"
  360. void loop() {
  361. register long time;
  362. time = millis();
  363. #include "___loop/___loop_init.h"
  364. #if defined(HAS__PCINT_setup)
  365. #include "_PCINT/_PCINT_loop.h"
  366. #endif
  367. #if defined(HAS_ROTTARY_SW)
  368. #include "_ROTTARY_SW/_ROTTARY_SW_loop.h"
  369. #endif
  370. #ifdef DISABLE_LOOP
  371. #include "___DEBUG/___DEBUG_DISABLE_LOOP.h"
  372. #endif
  373. #include "___loop/__main_loop_top.h"
  374. // Serial.println(time);
  375. #include "___loop/_SlaveID_loop1.h" //todo PCINT
  376. #include "___loop/_ControllerID_loop1.h" //todo make default
  377. #include "___loop/__main_loop_end.h"
  378. // DISPLAY_DEV.setCursor(10,10);
  379. // DISPLAY_DEV.print(" Test307");
  380. /*
  381. DISPLAY__print_ANY(
  382. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  383. DISPLAY_PHYSICAL_CHAR_A,
  384. DISPLAY_pos_seq_A_NEXT,
  385. "123456789012345678901234567890"
  386. ); */
  387. //delay(10);
  388. if(DEBUG_ACTION_TRIG != DEBUG_LAST_ACTION_TRIG) {
  389. #if defined(DEBUG_LAST_ACTION_TRIG_ENABLE)
  390. 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("] ");
  391. #endif
  392. // Serial.print("] COUNTER__MAIN_A["); Serial.print(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_A);
  393. // Serial.print("] COUNTER__MAIN_END_A["); Serial.print(ARDUINO_LOOP_LATENCY_COUNTER__MAIN_END_A);
  394. }
  395. DEBUG_LAST_ACTION_TRIG = DEBUG_ACTION_TRIG ;
  396. }
  397. #include "_PCINT/_PCINT_func_loop.h" //todo empty implement display etc
  398. #include "_Slave_Ports_func_loop.h" //todo implement