forked from grblHAL/Plugins_spindle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onoff.c
184 lines (147 loc) · 5.51 KB
/
onoff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
onoff.c - on/off + optional direction spindle driver
Part of grblHAL
Copyright (c) 2023-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "shared.h"
#if SPINDLE_ENABLE & ((1<<SPINDLE_ONOFF1)|(1<<SPINDLE_ONOFF1_DIR))
#ifdef SPINDLE1_ENABLE_PIN
#define ON_OFF_N_PORTS 0
#elif SPINDLE_ENABLE & (1<<SPINDLE_ONOFF1_DIR)
#define ON_OFF_N_PORTS 2
#else
#define ON_OFF_N_PORTS 1
#endif
#if ON_OFF_N_PORTS
#include "grbl/protocol.h"
#include "grbl/nvs_buffer.h"
typedef struct {
uint8_t on_port;
uint8_t dir_port;
} onoff_spindle_settings_t;
static onoff_spindle_settings_t spindle_config, run;
static spindle_state_t spindle_state = {0};
static nvs_address_t nvs_address;
static uint8_t n_dout;
static char max_dport[4];
// Start or stop spindle
static void spindleSetState (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
{
UNUSED(spindle);
spindle_state = state;
#if SPINDLE_ENABLE & (1<<SPINDLE_ONOFF1_DIR)
hal.port.digital_out(run.dir_port, state.ccw);
#endif
hal.port.digital_out(run.on_port, state.on);
}
// Returns spindle state in a spindle_state_t variable
static spindle_state_t spindleGetState (spindle_ptrs_t *spindle)
{
UNUSED(spindle);
return spindle_state;
}
static void onoff_spindle_register (void)
{
static const spindle_ptrs_t spindle = {
.type = SpindleType_Basic,
.ref_id = SPINDLE_ONOFF1,
.cap = {
#if ON_OFF_N_PORTS == 2
.direction = On,
#endif
.gpio_controlled = On
},
.set_state = spindleSetState,
.get_state = spindleGetState
};
if((spindle_register(&spindle, "On/off spindle")) != -1)
spindleSetState(NULL, spindle_state, 0.0f);
else
protocol_enqueue_foreground_task(report_warning, "On/off spindle failed to initialize!");
}
static const setting_detail_t vfd_settings[] = {
{ Setting_Spindle_OnPort, Group_AuxPorts, "Spindle on port", NULL, Format_Int8, "#0", "0", max_dport, Setting_NonCore, &spindle_config.on_port, NULL, NULL, { .reboot_required = On } },
#if ON_OFF_N_PORTS == 2
{ Setting_Spindle_DirPort, Group_AuxPorts, "Spindle dir port", NULL, Format_Int8, "#0", "0", max_dport, Setting_NonCore, &spindle_config.dir_port, NULL, NULL, { .reboot_required = On } }
#endif
};
#ifndef NO_SETTINGS_DESCRIPTIONS
static const setting_descr_t spindle_settings_descr[] = {
{ Setting_Spindle_OnPort, "Spindle 0 (default spindle) VFD ModBus address" },
#if ON_OFF_N_PORTS == 2
{ Setting_Spindle_DirPort, "Spindle 1 VFD ModBus address" }
#endif
};
#endif
static void spindle_settings_save (void)
{
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&spindle_config, sizeof(onoff_spindle_settings_t), true);
}
static void spindle_settings_restore (void)
{
spindle_config.on_port = n_dout - 1;
spindle_config.dir_port = n_dout > 1 ? n_dout - 2 : 0;
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&spindle_config, sizeof(onoff_spindle_settings_t), true);
}
static void spindle_settings_load (void)
{
bool ok;
if((hal.nvs.memcpy_from_nvs((uint8_t *)&spindle_config, nvs_address, sizeof(onoff_spindle_settings_t), true) != NVS_TransferResult_OK))
spindle_settings_restore();
run.on_port = spindle_config.on_port;
run.dir_port = spindle_config.dir_port;
strcpy(max_dport, uitoa(max(n_dout, ioports_available(Port_Digital, Port_Output)) - 1));
ok = ioport_claim(Port_Digital, Port_Output, &run.on_port, "Spindle on");
#if ON_OFF_N_PORTS == 2
ok = ok && ioport_claim(Port_Digital, Port_Output, &run.dir_port, "Spindle direction");
#endif
if(ok)
onoff_spindle_register();
else
protocol_enqueue_foreground_task(report_warning, "On/off spindle failed to initialize!");
}
static setting_details_t vfd_setting_details = {
.settings = vfd_settings,
.n_settings = sizeof(vfd_settings) / sizeof(setting_detail_t),
#ifndef NO_SETTINGS_DESCRIPTIONS
.descriptions = spindle_settings_descr,
.n_descriptions = sizeof(spindle_settings_descr) / sizeof(setting_descr_t),
#endif
.load = spindle_settings_load,
.restore = spindle_settings_restore,
.save = spindle_settings_save
};
void onoff_spindle_init (void)
{
if((n_dout = hal.port.num_digital_out) < ON_OFF_N_PORTS)
protocol_enqueue_foreground_task(report_warning, "On/off spindle failed to initialize!");
else if(!ioport_can_claim_explicit()) {
run.on_port = --hal.port.num_digital_out;
#if ON_OFF_N_PORTS == 2
run.dir_port = --hal.port.num_digital_out;
#endif
onoff_spindle_register();
} else if((nvs_address = nvs_alloc(sizeof(onoff_spindle_settings_t)))) {
hal.port.num_digital_out -= ON_OFF_N_PORTS;
settings_register(&vfd_setting_details);
} else
protocol_enqueue_foreground_task(report_warning, "On/off spindle failed to initialize!");
}
#else // ON_OFF_N_PORTS == 0
// Dummy init for when the base driver provides the spindle implementation
void onoff_spindle_init (void)
{
}
#endif
#endif // SPINDLE_ENABLE