Minor Makefile cleanup
[libam7xxx.git] / picoproj.c
1 /* picoproj - communication with AM7xxx based USB pico projectors
2  *
3  * Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <endian.h>
24 #include <errno.h>
25
26 typedef enum {
27         AM7x01_PACKET_TYPE_INIT    = 0x01,
28         AM7x01_PACKET_TYPE_IMAGE   = 0x02,
29         AM7x01_PACKET_TYPE_POWER   = 0x04,
30         AM7x01_PACKET_TYPE_UNKNOWN = 0x05,
31 } am7x01_packet_type;
32
33 typedef enum {
34         AM7x01_IMAGE_FORMAT_JPEG = 1,
35 } am7x01_image_format;
36
37 typedef enum {
38         AM7x01_POWER_OFF  = 0,
39         AM7x01_POWER_LOW  = 1,
40         AM7x01_POWER_MID  = 2,
41         AM7x01_POWER_HIGH = 3,
42 } am7x01_power_mode;
43
44 struct image_header {
45         uint32_t format;
46         uint32_t width;
47         uint32_t height;
48         uint32_t image_size;
49 };
50
51 struct power_header {
52         uint32_t power_low;
53         uint32_t power_mid;
54         uint32_t power_high;
55 };
56
57 /*
58  * Examples of packet headers:
59  *
60  * Image widget:
61  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62  * +02|00|00|00|00|10|3e|10|01|00|00|00|20|03|00|00|e0|01|00|00|53|E8|00|00+
63  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
64  *
65  * Brightness widget:
66  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
67  * +04|00|00|00|00|0c|ff|ff|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00+
68  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
69  */
70
71 static uint8_t reference_image_header[] = {
72         0x02, 0x00, 0x00, 0x00,
73         0x00,
74         0x10,
75         0x3e,
76         0x10,
77         0x01, 0x00, 0x00, 0x00,
78         0x20, 0x03, 0x00, 0x00,
79         0xe0, 0x01, 0x00, 0x00,
80         0x53, 0xE8, 0x00, 0x00
81 };
82
83 struct header {
84         uint32_t packet_type;
85         uint8_t unknown0;
86         uint8_t header_len;
87         uint8_t unknown2;
88         uint8_t unknown3;
89         union {
90                 struct image_header image;
91                 struct power_header power;
92         } header_data;
93 };
94
95
96 static void dump_image_header(struct image_header *i)
97 {
98         if (i == NULL)
99                 return;
100
101         printf("Image header:\n");
102         printf("format:      0x%08x (%u)\n", i->format, i->format);
103         printf("width:       0x%08x (%u)\n", i->width, i->width);
104         printf("height:      0x%08x (%u)\n", i->height, i->height);
105         printf("image size:  0x%08x (%u)\n", i->image_size, i->image_size);
106 }
107
108 static void dump_header(struct header *h)
109 {
110         if (h == NULL)
111                 return;
112
113         printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
114         printf("unknown0:    0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
115         printf("header_len:  0x%02hhx (%hhu)\n", h->header_len, h->header_len);
116         printf("unknown2:    0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
117         printf("unknown3:    0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
118
119         switch(h->packet_type) {
120         case AM7x01_PACKET_TYPE_IMAGE:
121                 dump_image_header(&(h->header_data.image));
122                 break;
123
124         default:
125                 printf("Packet type not supported!\n");
126                 break;
127         }
128
129         fflush(stdout);
130 }
131
132 static void dump_buffer(uint8_t *buffer, unsigned int len)
133 {
134         unsigned int i;
135
136         if (buffer == NULL || len == 0)
137                 return;
138
139         for (i = 0; i < len; i++) {
140                 printf("%02hhX%c", buffer[i], (i < len - 1) ? ' ' : '\n');
141         }
142         fflush(stdout);
143 }
144
145 static int send_data(uint8_t *buffer, unsigned int len)
146 {
147         dump_buffer(buffer, len);
148         return 0;
149 }
150
151 static int send_header(struct header *h)
152 {
153         union {
154                 struct header header;
155                 uint8_t buffer[sizeof (struct header)];
156         } data;
157
158         data.header = *h;
159
160         return send_data(data.buffer, sizeof (struct header));
161 }
162
163 static int send_image(am7x01_image_format format,
164                       unsigned int width,
165                       unsigned int height,
166                       uint8_t *image,
167                       unsigned int size)
168 {
169         int ret;
170         struct header h = {
171                 .packet_type = htole32(AM7x01_PACKET_TYPE_IMAGE),
172                 .unknown0    = 0x00,
173                 .header_len  = sizeof(struct image_header),
174                 .unknown2    = 0x3e,
175                 .unknown3    = 0x10,
176                 .header_data = {
177                         .image = {
178                                 .format     = htole32(format),
179                                 .width      = htole32(width),
180                                 .height     = htole32(height),
181                                 .image_size = htole32(size),
182                         },
183                 },
184         };
185
186         dump_header(&h);
187         printf("\n");
188
189         printf("Dump Buffers\n");
190         dump_buffer(reference_image_header, sizeof(struct header));
191
192         ret = send_header(&h);
193         if (ret < 0)
194                 return ret;
195
196         if (image == NULL || size == 0)
197                 return 0;
198
199         return send_data(image, size);
200 }
201
202
203 int main(int argc, char *argv[])
204 {
205         int ret;
206
207         ret = send_image(AM7x01_IMAGE_FORMAT_JPEG, 800, 480, 59475);
208         if (ret < 0) {
209                 perror("send_image");
210                 exit(EXIT_FAILURE);
211         }
212         exit(EXIT_SUCCESS);
213 }