Saturday, August 27, 2011

By request: Frankenkindle demo code

Mike at Hackaday suggested that I submit the code used to generate the keystroke sequences that control the Kindle.  He correctly noticed in the prototype video that the pages don't turn very quickly.  I believe that this issue is caused by the custom script running on the Kindle itself.  You can see that the Up and Down buttons used on the menu selection screen operate very quickly, but the page turn operation takes more time.  At this point it's something I think I can live with but I'm always looking for ways to optimize my design.  This project will be completely open source anyway, so there's no harm in publishing it a bit early.

The basic operation is as follows:  The microcontroller waits until a button is pressed, at which point it calls the appropriate subroutine.  These subroutines manipulate control signals going to two multiplexer chips, which effectively act as Single Pole, Quadruple Pole switches.  When activated, they short specific pairs of wires that are connected to the Kindle, mimicking actual button press events.  Note that turning individual pages is not possible from the main Kindle keypad, so the software had to be hacked to allow custom control through specific key press sequences.  Turning a page to the right is accomplished by pressing the Shift key followed by the 5-way Right key.  There is specific timing required to make this work.

TL;DR: Microcontroller waits for you to press a button, then presses a button on the Kindle for you.

Note that the controller itself is a Teensy++ v1.0 (PJRC has since updated the design and currently sells v2.0 boards).  It runs in a mode Paul at PJRC calls Teensyduino, which allows it to use Arduino code.  A full design package (code, schematics, BOM, etc) will be published once I feel the design is complete.  For now, here is the code you saw running in the demo video:


Arduino code

01
02
03
04
05
06
07
08
09
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
///////////////////////////////////////////////////////////////////////
//
// Frankenkindle v0.1 code
//
// Glenn Johnson
// 8/2011
//
// Monitor simple pushbuttons from V.Reader device and translate
// into Kindle-compatible keypad control signals.  
//
// Note: Chip select signals not used - both multiplexers
//       are always powered.
//
////////////////////////////////////////////////////////////////////// 



void setup() {              

  //Set up serial for debug messages
  Serial.begin(9600);
  
  //set up the digital inputs from the V.Reader
  pinMode(PIN_B0, INPUT_PULLUP);  //Down, marked "Replay"
  pinMode(PIN_B1, INPUT_PULLUP);  //Home, marked as "Exit"
  pinMode(PIN_B2, INPUT_PULLUP);  //Up, marked as "Play"
  pinMode(PIN_B3, INPUT_PULLUP);  //(Not connected)
  pinMode(PIN_B4, INPUT_PULLUP);  //Right
  pinMode(PIN_B5, INPUT_PULLUP);  //Select (Enter), marked as "Bookmark"
  pinMode(PIN_B6, INPUT_PULLUP);  //Left
  
  //set up digital outputs to control the mux
  pinMode(PIN_C0, OUTPUT);    //IN1_1
  pinMode(PIN_C1, OUTPUT);    //IN2_1
  pinMode(PIN_C2, OUTPUT);    //IN1_2
  pinMode(PIN_C3, OUTPUT);    //IN2_2
  //pinMode(PIN_C4, OUTPUT);    //chip select 1
  //pinMode(PIN_C5, OUTPUT);    //chip select 2
  
  pinMode(PIN_D6, OUTPUT);    //LED, for testing
  
  //Set control signals to unused states
  digitalWrite(PIN_C0, HIGH);
  digitalWrite(PIN_C1, HIGH);
  digitalWrite(PIN_C2, HIGH);
  digitalWrite(PIN_C3, HIGH);  
  //digitalWrite(PIN_C4, LOW); 
  //digitalWrite(PIN_C5, LOW);
  
}

void shift()
{
  
  //press SHIFT
  digitalWrite(PIN_C0, LOW);   //IN1_1
  digitalWrite(PIN_C1, HIGH);  //IN2_1
  
  delay(100);

  //release SHIFT
  digitalWrite(PIN_C0, HIGH);
  digitalWrite(PIN_C1, HIGH);
  
}

void left()
{
  
  //press DPAD_LEFT
  digitalWrite(PIN_C0, HIGH);   //IN1_1
  digitalWrite(PIN_C1, LOW);    //IN2_1
  
  delay(100);
  
  //release DPAD_LEFT
  digitalWrite(PIN_C0, HIGH);   //IN1_1
  digitalWrite(PIN_C1, HIGH);   //IN2_1
  
}

void right()
{
  
  //press DPAD_RIGHT
  digitalWrite(PIN_C0, LOW);    //IN1_1
  digitalWrite(PIN_C1, LOW);    //IN2_1
  
  delay(100);
  
  //release DPAD_RIGHT
  digitalWrite(PIN_C0, HIGH);   //IN1_1
  digitalWrite(PIN_C1, HIGH);   //IN2_1
  
}

void up()
{
  
  //press DPAD_UP
  digitalWrite(PIN_C2, LOW);    //IN1_2
  digitalWrite(PIN_C3, LOW);    //IN2_2
  
  delay(100);
  
  //release DPAD_UP
  digitalWrite(PIN_C2, HIGH);   //IN1_2
  digitalWrite(PIN_C3, HIGH);   //IN2_2
  
}

void down()
{
  
  //press DPAD_DOWN
  digitalWrite(PIN_C2, HIGH);    //IN1_2
  digitalWrite(PIN_C3, LOW);    //IN2_2
  
  delay(100);
  
  //release DPAD_DOWN
  digitalWrite(PIN_C2, HIGH);   //IN1_2
  digitalWrite(PIN_C3, HIGH);   //IN2_2
  
}

void enter()
{
  
  //press DPAD_CENTER
  digitalWrite(PIN_C2, LOW);     //IN1_2
  digitalWrite(PIN_C3, HIGH);    //IN2_2
  
  delay(100);
  
  //release DPAD_CENTER
  digitalWrite(PIN_C2, HIGH);   //IN1_2
  digitalWrite(PIN_C3, HIGH);   //IN2_2
  
}


void shiftLeft()
{

  //send SHIFT keystroke
  shift();
    
  delay(200);
   
  //send DPAD_LEFT keystroke
  left();
  
  
  //Two second delay, ensure that the script has time to execute.
  delay(2000);
  
}



void shiftRight()
{
  
  //send SHIFT keystroke
  shift();
  
  delay(200);
  
  //send DPAD_RIGHT keystroke
  right();
  
  //Two second delay, ensure that the script has time to execute.
  delay(2000);
    
}

void shiftUp()
{
  
  //send SHIFT keystroke
  shift();
  
  delay(200);
  
  //send DPAD_UP keystroke
  up();
  
  //Two second delay, ensure that the script has time to execute.
  delay(2000);
    
}



void loop() {
  
  //if a button is pressed, it goes low
 
  if (!digitalRead(PIN_B0))  
  {
    
    //Down
    
    //Kindle command
    down();
    
    delay(1000);
    
  }
  
  if (!digitalRead(PIN_B1))
  {
    
    //Home

    //Kindle command
    shiftUp();
    
    delay(1000);
    
  }
  
  if (!digitalRead(PIN_B2))
  {
    
    //Up

    //Kindle command (Home = Shift + Up)
    up();
    
    delay(1000);
  }
  
  if (!digitalRead(PIN_B3))
  {

    //Not connected
    
  }
  
  if (!digitalRead(PIN_B4))
  {
    //Turn page right

    //Kindle command 
    shiftRight();
    
    delay(1000);
    
  }
  
  if (!digitalRead(PIN_B5))
  {

    //Select

    //Kindle command 
    enter();
    
    delay(1000);
    
  }
  
  if (!digitalRead(PIN_B6))
  {

    //Turn page left

    //Kindle command (page turn left = Shift + left)
    shiftLeft();
       
    delay(1000);
    
  }  
   
}

No comments:

Post a Comment