-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrbl_hmi.pde
126 lines (106 loc) · 2.87 KB
/
grbl_hmi.pde
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
long target[3];
long position[3];
float mm_per_step[3];
#include <Wire.h>
#include "lc_display.h"
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent); // register event
Serial.begin(115200); // start serial for output
Serial.print("\nHello World\n");
lcd_init();
ps2_setup();
}
extern char ps2_data[5]; // 3 sliders linearized 0-7: x, y, z, followed by 8 buttons in one byte
// last byte is zero as end marker
#define XVAL 0
#define YVAL 1
#define ZVAL 2
#define BUTTONS 3
void loop()
{
int i;
delay(50);
lcd_report_position();
ps2_read();
// For serial debugging enable the following lines:
if (ps2_data[0]|ps2_data[1]|ps2_data[2]|ps2_data[3]){
Serial.print("Stick Values:");
Serial.print(ps2_data[XVAL], DEC);
Serial.print(",");
Serial.print(ps2_data[YVAL], DEC);
Serial.print(",");
Serial.print(ps2_data[ZVAL], DEC);
Serial.print(",");
Serial.print(ps2_data[BUTTONS], DEC);
Serial.print(",");
Serial.println(ps2_data[4], DEC);
}
}
void format_measurement(long value)
{
long whole;
long fraction;
float answer;
answer = float(value)*DEFAULT_UM_PER_STEP;
whole = round(answer/10);
fraction = abs(whole % 100);
whole = whole/100;
if (whole>=0) Serial.print(" ");
if (abs(whole)<100) Serial.print(" ");
if (abs(whole)<10){
if ((whole==0) & (answer<0)){
Serial.print("-");
} else {
Serial.print(" ");
}
}
Serial.print(whole);
Serial.print(".");
if (fraction<10) Serial.print("0");
Serial.print(fraction);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
byte channel;
long Value;
channel = Wire.receive();
//Serial.print((int) channel);
//Serial.print(": ");
Value = Wire.receive();
Value = (Value << 8) | Wire.receive();
Value = (Value << 8) | Wire.receive();
Value = (Value << 8) | Wire.receive();
if (channel<128){
if (channel<3){
mm_per_step[channel]=Value;
}
} else {
channel -= 128;
if (channel<3){
position[channel]=Value;
} else if (channel<6) {
target[channel-3]=Value;
}
}
// Serial.println(" Rx"); // print the integer
}
void requestEvent()
{
Wire.send((uint8_t *) ps2_data, 4); // Must use this .send, because the one
// that takes char* won't transmit a data
// byte of zero.
// respond with message of 4 bytes
// as expected by master
/*
Wire.send(1);
Wire.send(2);
Wire.send(3);
Wire.send(4);
*/
Serial.println("Tx");
}