source: thomson/elec/CrO2/software/cro2.cpp@ 96bc8fa

main
Last change on this file since 96bc8fa was 96bc8fa, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

C++ refactoring : move USB communication to device class.

git-svn-id: svn://localhost/thomson@11 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/* CrO2 datassette emulator
2 * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
3 *
4 * Distributed under the terms of the MIT licence.
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <stdint.h>
11#include <unistd.h>
12
13#include <iup.h>
14#include <iupcontrols.h>
15
16#include "device.h"
17
18/* UI */
19int menu_open(Ihandle* that)
20{
21 IupPopup(IupFileDlg(), IUP_CENTER, IUP_CENTER);
22 return IUP_DEFAULT;
23}
24
25int menu_exit(Ihandle* that)
26{
27 return IUP_CLOSE;
28}
29
30
31void GUI_open(int* argc, char*** argv)
32{
33 IupOpen(argc, argv);
34// IupControlsOpen();
35
36 IupSetFunction("OPEN", menu_open);
37 IupSetFunction("EXIT", menu_exit);
38
39 Ihandle* menu = IupMenu(
40 IupSubmenu("File",
41 IupMenu(
42 IupItem("Open", "OPEN"),
43 IupItem("Exit", "EXIT"),
44 NULL
45 )
46 ),
47 NULL
48 );
49
50 Ihandle* platformlist = IupList(NULL);
51 IupSetAttribute(platformlist, "EXPAND", "HORIZONTAL");
52 IupSetAttribute(platformlist, "DROPDOWN", "YES");
53 IupSetAttribute(platformlist, "1", "MO5");
54 IupSetAttribute(platformlist, "VALUE", "1");
55
56 Ihandle* blocklist = IupTree();
57 IupSetAttribute(blocklist, "EXPAND", "VERTICAL");
58
59 Ihandle* tabs = IupTabs(
60 IupVbox(
61 IupLabel("Hello World"),
62 NULL
63 ),
64 IupVbox(
65 IupHbox(
66 IupLabel("Format:"),
67 platformlist,
68 NULL
69 ),
70 IupHbox(
71 blocklist,
72 IupVbox(
73// IupMatrix(NULL),
74 IupLabel("Checksum:"),
75 NULL
76 ),
77 NULL
78 )
79 ),
80 NULL
81 );
82
83 IupSetAttribute(tabs,"TABTITLE0", "Control");
84 IupSetAttribute(tabs,"TABTITLE1", "Explore");
85
86 Ihandle* dialog = IupDialog(tabs);
87 IupSetAttribute(dialog, "TITLE", "CrO2 tape emulator");
88 IupSetAttributeHandle(dialog, "MENU", menu);
89 IupShow(dialog);
90
91 IupMainLoop();
92
93 IupClose();
94}
95
96
97int main(int argc, char **argv)
98{
99 //usb_dev_handle *handle = NULL;
100 unsigned char buffer[275];
101 int nBytes = 0;
102
103 if(argc < 2){
104 GUI_open(&argc, &argv);
105 exit(0);
106 }
107
108 Device dev; // Constructor inits communication.
109 // TODO handle thrown exceptions.
110
111 if(strcmp(argv[1], "get") == 0){
112 memset(buffer, 0, 275);
113 nBytes = dev.read(buffer, sizeof(buffer));
114 }else if(strcmp(argv[1], "put") == 0){
115
116 while (dev.getStatus() & 8)
117 usleep(1000000);
118
119 FILE* fptr = fopen(argv[2], "rb");
120 int blockid;
121 uint8_t blktype, blksize;
122 sscanf(argv[3], "%d", &blockid);
123
124 do
125 {
126 do
127 {
128 fread(&blktype, 1, 1, fptr);
129 if (feof(fptr))
130 {
131 fprintf(stderr, "end of file.\n");
132 fclose(fptr);
133 exit(0);
134 }
135 }
136 while(blktype != 0x5A); // skip sync header
137
138 fread(&blktype, 1, 1, fptr);
139 fread(&blksize, 1, 1, fptr);
140 blksize -= 2;
141 fread(buffer, 1, blksize + 1, fptr);
142 if (blktype == 0)
143 {
144 // new file
145 printf("%.11s\n",buffer);
146 }
147 }
148 while (blockid --);
149
150 fclose(fptr);
151
152 nBytes = dev.write(buffer, blksize, blktype);
153 }else{
154 GUI_open(&argc, &argv);
155 }
156
157 if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
158 return 0;
159}
160
Note: See TracBrowser for help on using the repository browser.