SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / SCClassLibrary / Common / Streams / PgeneralHid.sc
bloba3d3655714ec642955cb860989a7a9073c7ca76c
1 //human device interface pattern. pulls values from devices like gamepads etc.
2 // based on GeneralHID
4 PhidSlot : Pattern {
5         var <>slot,<>type,<>device;
6         var <>repeats;
7         var <>pSlot;
9         // the Device list must have been built.
10         *new { arg slot, type, device, repeats=inf;
11                 ^super.new.init(slot, type, device, repeats)
12         }
14         init{|sl,tp,dev,rep|
15                 device = dev;
16                 slot = sl;
17                 type = tp;
18                 repeats = rep;
19                 if ( device.isKindOf( GeneralHIDDevice ).not,
20                         {
21                                 try { device = GeneralHID.open( device ); }{ "device argument is not a GeneralHIDDevice".error; ^nil }
22                         });
23         }
25         storeArgs { ^[slot, type, device, repeats] }
27         embedInStream { arg inval;
29                 // slot and type streams
30                 var slotStr = slot.asStream;
31                 var typeStr = type.asStream;
32                 var slotVal, typeVal, slottypes;
34                 repeats.value.do({
36                         slotVal = slotStr.next(inval);
37                         typeVal = typeStr.next(inval);
39                         slottypes = [slotVal, typeVal].flop;
41                         inval = slottypes.collect{ |it|
42                                 var ret;
43                                 if ( device.slots[ it[1] ].isNil,
44                                         { "slot type not found".warn;
45                                                 ret = inval;
46                                         },{
47                                         if ( device.slots[ it[1] ][ it[0] ].isNil,
48                                                 { "slot not found".warn;
49                                                         ret = inval;
50                                                 },{
51                                                         ret = device.slots[ it[1] ][ it[0] ].value;
52                                                 });
53                                         });
54                                 ret;
55                         }.unbubble.yield;
57                 });
59                 ^inval;
60         }
64 PhidKey : Pattern {
65         var <>key,<>device;
66         var <>repeats;
67         var <>pSlot;
69         // the Device list must have been built.
70         *new { arg key, device, repeats=inf;
71                 ^super.new.init(key, device, repeats)
72         }
74         init{|ky,dev,rep|
75                 key = ky;
76                 device = dev;
77                 repeats = rep;
78                 if ( device.isKindOf( GeneralHIDDevice ).not,
79                         {
80                                 try { device = GeneralHID.open( device ); }{ "device argument is not a GeneralHIDDevice".error; ^nil }
81                         });
82         }
84         storeArgs { ^[key, device, repeats] }
86         embedInStream { arg inval;
87                 // key stream
88                 var keyStr = key.asStream;
89                 var keyVal;
91                 repeats.value.do({
93                         keyVal = keyStr.next(inval);
94                         keyVal = keyVal.asArray;
96                         inval = keyVal.collect{ |it|
97                                 var ret;
98                                 if ( device.at(it).isNil,
99                                         { "slot not found".warn;
100                                                 ret = inval;
101                                         },{
102                                                 ret = device.at( it ).value;
103                                         });
104                                 ret;
105                         }.unbubble.yield;
107                 });
109                 ^inval;
110         }