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

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