Luigi Santivetti | 69972f9 | 2019-11-12 22:55:40 +0000 | [diff] [blame^] | 1 | /* |
| 2 | coolant_control.c - coolant control methods |
| 3 | Part of Grbl |
| 4 | |
| 5 | Copyright (c) 2012-2016 Sungeun K. Jeon for Gnea Research LLC |
| 6 | |
| 7 | Grbl is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | Grbl is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Grbl. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "grbl.h" |
| 22 | |
| 23 | |
| 24 | void coolant_init() |
| 25 | { |
| 26 | COOLANT_FLOOD_DDR |= (1 << COOLANT_FLOOD_BIT); // Configure as output pin |
| 27 | #ifdef ENABLE_M7 |
| 28 | COOLANT_MIST_DDR |= (1 << COOLANT_MIST_BIT); |
| 29 | #endif |
| 30 | coolant_stop(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | // Returns current coolant output state. Overrides may alter it from programmed state. |
| 35 | uint8_t coolant_get_state() |
| 36 | { |
| 37 | uint8_t cl_state = COOLANT_STATE_DISABLE; |
| 38 | #ifdef INVERT_COOLANT_FLOOD_PIN |
| 39 | if (bit_isfalse(COOLANT_FLOOD_PORT,(1 << COOLANT_FLOOD_BIT))) { |
| 40 | #else |
| 41 | if (bit_istrue(COOLANT_FLOOD_PORT,(1 << COOLANT_FLOOD_BIT))) { |
| 42 | #endif |
| 43 | cl_state |= COOLANT_STATE_FLOOD; |
| 44 | } |
| 45 | #ifdef ENABLE_M7 |
| 46 | #ifdef INVERT_COOLANT_MIST_PIN |
| 47 | if (bit_isfalse(COOLANT_MIST_PORT,(1 << COOLANT_MIST_BIT))) { |
| 48 | #else |
| 49 | if (bit_istrue(COOLANT_MIST_PORT,(1 << COOLANT_MIST_BIT))) { |
| 50 | #endif |
| 51 | cl_state |= COOLANT_STATE_MIST; |
| 52 | } |
| 53 | #endif |
| 54 | return(cl_state); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // Directly called by coolant_init(), coolant_set_state(), and mc_reset(), which can be at |
| 59 | // an interrupt-level. No report flag set, but only called by routines that don't need it. |
| 60 | void coolant_stop() |
| 61 | { |
| 62 | #ifdef INVERT_COOLANT_FLOOD_PIN |
| 63 | COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT); |
| 64 | #else |
| 65 | COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT); |
| 66 | #endif |
| 67 | #ifdef ENABLE_M7 |
| 68 | #ifdef INVERT_COOLANT_MIST_PIN |
| 69 | COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT); |
| 70 | #else |
| 71 | COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT); |
| 72 | #endif |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // Main program only. Immediately sets flood coolant running state and also mist coolant, |
| 78 | // if enabled. Also sets a flag to report an update to a coolant state. |
| 79 | // Called by coolant toggle override, parking restore, parking retract, sleep mode, g-code |
| 80 | // parser program end, and g-code parser coolant_sync(). |
| 81 | void coolant_set_state(uint8_t mode) |
| 82 | { |
| 83 | if (sys.abort) { return; } // Block during abort. |
| 84 | |
| 85 | if (mode & COOLANT_FLOOD_ENABLE) { |
| 86 | #ifdef INVERT_COOLANT_FLOOD_PIN |
| 87 | COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT); |
| 88 | #else |
| 89 | COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT); |
| 90 | #endif |
| 91 | } else { |
| 92 | #ifdef INVERT_COOLANT_FLOOD_PIN |
| 93 | COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT); |
| 94 | #else |
| 95 | COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT); |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | #ifdef ENABLE_M7 |
| 100 | if (mode & COOLANT_MIST_ENABLE) { |
| 101 | #ifdef INVERT_COOLANT_MIST_PIN |
| 102 | COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT); |
| 103 | #else |
| 104 | COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT); |
| 105 | #endif |
| 106 | } else { |
| 107 | #ifdef INVERT_COOLANT_MIST_PIN |
| 108 | COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT); |
| 109 | #else |
| 110 | COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT); |
| 111 | #endif |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | sys.report_ovr_counter = 0; // Set to report change immediately |
| 116 | } |
| 117 | |
| 118 | |
| 119 | // G-code parser entry-point for setting coolant state. Forces a planner buffer sync and bails |
| 120 | // if an abort or check-mode is active. |
| 121 | void coolant_sync(uint8_t mode) |
| 122 | { |
| 123 | if (sys.state == STATE_CHECK_MODE) { return; } |
| 124 | protocol_buffer_synchronize(); // Ensure coolant turns on when specified in program. |
| 125 | coolant_set_state(mode); |
| 126 | } |