Repository navigation
Expand file tree
/
Copy pathgcode-stacks.c
More file actions
85 lines (64 loc) · 2.01 KB
/
Copy pathgcode-stacks.c
File metadata and controls
85 lines (64 loc) · 2.01 KB
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
/*
============================================================================
Name : gcode-stacks.c
Author : Radu - Eosif Mihailescu
Version : 1.0 (2012-09-23)
Copyright : (C) 2012 Radu - Eosif Mihailescu <radu.mihailescu@linux360.ro>
Description : G-Code Subroutine Parameter Stack Handling Code
============================================================================
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "gcode-commons.h"
#include "gcode-stacks.h"
#include "gcode-debugcon.h"
#include "gcode-parameters.h"
static double *parametersStack[GCODE_MACRO_COUNT];
static TProgramPointer *programStack[GCODE_SUBPROGRAM_COUNT];
static uint8_t paSP, prSP;
bool init_stacks(void *data) {
paSP = prSP = 0;
GCODE_DEBUG("Stacks initialized, %d nested calls (%d of which macro-capable) supported",
GCODE_SUBPROGRAM_COUNT, GCODE_MACRO_COUNT);
return true;
}
bool stacks_push_parameters(void) {
if(paSP < GCODE_MACRO_COUNT - 1) {
double *params = (double *)malloc(sizeof(double) * 33);
uint8_t i;
for(i = 0; i < 33; i++) params[i] = fetch_parameter(i + 1);
parametersStack[++paSP] = params;
return true;
} else return false;
}
bool stacks_push_program(const TProgramPointer *state) {
if(prSP < GCODE_SUBPROGRAM_COUNT - 1) {
TProgramPointer *pptr = (TProgramPointer *)malloc(sizeof(TProgramPointer));
*pptr = *state;
programStack[++prSP] = pptr;
return true;
} else return false;
}
bool stacks_pop_parameters(void) {
if(paSP) {
double *params = parametersStack[paSP--];
uint8_t i;
for(i = 0; i < 33; i++) update_parameter(i + 1, params[i]);
free(params);
commit_parameters();
return true;
} else return false;
}
bool stacks_pop_program(TProgramPointer *state) {
if(prSP && state) {
*state = *(programStack[prSP--]);
free(programStack[prSP + 1]);
return true;
} else return false;
}
bool done_stacks(void) {
GCODE_DEBUG("Stacks done");
return true;
}