forked from triffid/FiveD_on_Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.c
105 lines (84 loc) · 1.62 KB
/
simulation.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "simulation.h"
uint8_t ACSR;
uint8_t TIMSK1;
/* -- debugging ------------------------------------------------------------ */
void sim_info(const char fmt[], ...)
{
va_list ap;
fputs("\033[0;32m" , stdout);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fputs("\033[m\n", stdout);
}
void sim_error(const char msg[])
{
printf("\033[0;31mERROR: %s\033[m\n", msg);
exit(-1);
}
void sim_assert(bool cond, const char msg[])
{
if (!cond)
{
sim_error(msg);
}
}
/* -- interrupts ----------------------------------------------------------- */
volatile bool sim_interrupts = false;
void sei(void)
{
sim_interrupts = true;
}
/* -- PIN I/O ------------------------------------------------------------ */
#define out true
#define in false
static int x = 0, y = 0, z = 0, e = 0;
static bool direction[PIN_NB];
static bool state[PIN_NB];
static void print_pos(void)
{
sim_info("x:%5d y:%5d z:%5d e:%5d", x, y, z, e);
}
void WRITE(pin_t pin, bool s)
{
bool old_state = state[pin];
if (direction[pin] == out)
{
state[pin] = s;
}
if (s && !old_state) /* rising edge */
{
switch (pin)
{
case X_STEP_PIN:
x += state[X_DIR_PIN] ? 1 : -1;
print_pos();
break;
case Y_STEP_PIN:
y += state[Y_DIR_PIN] ? 1 : -1;
print_pos();
break;
case Z_STEP_PIN:
z += state[Z_DIR_PIN] ? 1 : -1;
print_pos();
break;
case E_STEP_PIN:
e += state[E_DIR_PIN] ? 1 : -1;
print_pos();
break;
default:
break;
}
}
}
void SET_OUTPUT(pin_t pin)
{
direction[pin] = out;
}
void SET_INPUT(pin_t pin)
{
direction[pin] = in;
}