aboutsummaryrefslogtreecommitdiff
path: root/host/usb.c
blob: 18b38a8677d2851df67f656ecdd0052c75e9077f (plain)
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <libusb-1.0/libusb.h>

#include "color.h"
#include "usb.h"

int matelight_usb_init(){
	int rc = 0;
	rc = libusb_init(NULL);
	if(rc){
		fprintf(stderr, "LIBML: Cannot initialize libusb\n");
		return 1;
	}
	return 0;
}

void matelight_usb_destroy(){
	libusb_exit(NULL);
}

static int matelight_cmp_str_desc(libusb_device_handle *dev, uint8_t index, char *value){
	char data[256];
	if(libusb_get_string_descriptor_ascii(dev, index, (unsigned char*)data, sizeof(data)) < 0){
		fprintf(stderr, "LIBML: Cannot read device string descriptor\n");
		return 0;
	}
	return strcmp(data, value) == 0;
}

matelight_handle *matelight_open(char *match_serial){
	matelight_handle *out = NULL;
    libusb_device_handle *handle = NULL;
	libusb_device** device_list;
	struct libusb_device_descriptor desc;

	ssize_t res = libusb_get_device_list(NULL, &device_list);
	if(res == 0){
		fprintf(stderr, "LIBML: Cannot find any connected matelight\n");
		goto error;
	}else if(res < 0){
		fprintf(stderr, "LIBML: Error enumerating connected USB devices\n");
		goto error;
	}else{
		out = calloc(1, sizeof(matelight_handle));
		if(!out){
			fprintf(stderr, "LIBML: Cannot allocate memory\n");
			goto error;
		}
		for(ssize_t i=0; i<res; i++){
			libusb_get_device_descriptor(device_list[i], &desc);
			if(desc.idVendor == MATELIGHT_VID && desc.idProduct == MATELIGHT_PID){
				if(libusb_open(device_list[i], &handle)){
					fprintf(stderr, "LIBML: Cannot open Mate Light USB device\n");
					goto error;
				}
				out->handle = handle;
				if(matelight_cmp_str_desc(handle, desc.iManufacturer, "Gold & Apple"))
				if(matelight_cmp_str_desc(handle, desc.iProduct, "Mate Light"))
				if(!match_serial || matelight_cmp_str_desc(handle, desc.iSerialNumber, match_serial)){
#define BUF_SIZE 256
					out->serial = malloc(BUF_SIZE);
					if(!out->serial){ fprintf(stderr, "LIBML: Cannot allocate memory\n");
						goto error;
					}
					if(libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char*)&out->serial, BUF_SIZE) < 0){
						fprintf(stderr, "LIBML: Cannot read device string descriptor\n");
						goto error;
					}
#undef BUF_SIZE
                    return out;
				}
			}
		}
	}
	libusb_free_device_list(device_list, 1);
	return NULL;
error:
	if(out){
        if(out->serial)
            free(out->serial);
        free(out);
    }
    if(handle)
        libusb_close(handle);
	libusb_free_device_list(device_list, 1);
	return NULL;
}

typedef struct{
	uint8_t opcode;
	uint8_t x, y;
	rgb_t buf[CRATE_WIDTH*CRATE_HEIGHT];
} crate_frame_t;

int matelight_send_frame(matelight_handle *ml, void *buf, size_t w, size_t h, float brightness, int alpha){
//	fprintf(stderr, "\nFRAME START\n");
	for(size_t cy=0; cy<h; cy++){
//		fprintf(stderr, "##### NEXT ROW\n");
		for(size_t cx=0; cx<w; cx++){
			crate_frame_t frame;
			frame.opcode = 0;
			frame.x = cx;
			frame.y = cy;
//			fprintf(stderr, "CRATE %d %d\n", cx, cy);
			for(size_t x=0; x<CRATE_WIDTH; x++){
				for(size_t y=0; y<CRATE_HEIGHT; y++){
					size_t dpos = y*CRATE_WIDTH + x;
					size_t spos = w*CRATE_WIDTH*(cy*CRATE_HEIGHT+y) + cx*CRATE_WIDTH+x;
					color_t *src;
					if(alpha){
						src = (((color_t*)buf)+spos);
					}else{
						src = (color_t*)(((rgb_t*)buf)+spos);
					}
					rgb_t *dst = frame.buf+dpos;
					/* Gamma correction */
#define GAMMA_APPLY(c) ((uint8_t)roundf(powf((c/255.0F), GAMMA) * brightness * 255))
					dst->r = GAMMA_APPLY(src->r);
					dst->g = GAMMA_APPLY(src->g);
					dst->b = GAMMA_APPLY(src->b);
//					fprintf(stderr, "%c", ((dst->r > 1) ? '#' : '.'));
#undef GAMMA_APPLY
				}
//				fprintf(stderr, "\n");
			}
			int transferred = 0;
			int rc = libusb_bulk_transfer(ml->handle, MATELIGHT_FRAMEDATA_ENDPOINT, (unsigned char*)&frame, sizeof(frame), &transferred, MATELIGHT_TIMEOUT);
			if(rc){
				fprintf(stderr, "LIBML: Cannot write frame data\n");
				return 1;
			}
			if(transferred != sizeof(frame)){
				fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %zu bytes.\n", transferred, sizeof(frame));
				return 1;
			}
		}
	}
	uint8_t payload = 0x01;
	int transferred = 0;
	int rc = libusb_bulk_transfer(ml->handle, MATELIGHT_FRAMEDATA_ENDPOINT, &payload, sizeof(uint8_t), &transferred, MATELIGHT_TIMEOUT);
	if(rc){
		fprintf(stderr, "LIBML: Cannot write frame data\n");
		return 1;
	}
	if(transferred != sizeof(uint8_t)){
		fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %zu bytes.\n", transferred, sizeof(uint8_t));
		return 1;
	}
	return 0;
}