forked from laamaa/m8c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.c
86 lines (71 loc) · 1.92 KB
/
write.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
// Copyright 2021 Jonne Kokkonen
// Released under the MIT licence, https://opensource.org/licenses/MIT
#include <SDL_log.h>
#include <libserialport.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
int reset_display(struct sp_port *port){
SDL_Log("Reset display\n");
uint8_t buf[2];
int result;
buf[0] = 0x45;
buf[1] = 0x52;
result = sp_blocking_write(port, buf, 2, 5);
if (result != 2) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error resetting M8 display, code %d", result);
return 0;
}
return 1;
}
int enable_and_reset_display(struct sp_port *port) {
uint8_t buf[1];
int result;
SDL_Log("Enabling and resetting M8 display\n");
buf[0] = 0x44;
result = sp_blocking_write(port, buf, 1, 5);
if (result != 1) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error enabling M8 display, code %d", result);
return 0;
}
usleep(500);
if (!reset_display(port)){
return 0;
}
return 1;
}
int disconnect(struct sp_port *port) {
char buf[1] = {'D'};
int result;
SDL_Log("Disconnecting M8\n");
result = sp_blocking_write(port, buf, 1, 5);
if (result != 1) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending disconnect, code %d", result);
return -1;
}
return 1;
}
int send_msg_controller(struct sp_port *port, uint8_t input) {
char buf[2] = {'C',input};
size_t nbytes = 2;
int result;
result = sp_blocking_write(port, buf, nbytes, 5);
if (result != nbytes) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending input, code %d", result);
return -1;
}
return 1;
}
int send_msg_keyjazz(struct sp_port *port, uint8_t note, uint8_t velocity) {
if (velocity > 64)
velocity = 64;
char buf[3] = {'K',note,velocity};
size_t nbytes = 3;
int result;
result = sp_blocking_write(port, buf, nbytes,5);
if (result != nbytes) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending keyjazz, code %d", result);
return -1;
}
return 1;
}