@@ -257,13 +257,16 @@ static void * avr_run_thread(void * param)
257
257
258
258
vcd_trace_enable (vcd_enabled );
259
259
260
- // Phase overlaps 16 needles/solenoids
261
- unsigned encoder_phase = 0 ;
262
260
// {v1, v2} encoding over 4 phases
263
261
unsigned phase_map [4 ] = {0 , 1 , 3 , 2 };
264
262
265
- encoder_phase = (machine .carriage .position +
266
- (machine .belt_phase == REGULAR ) ? 4 : 12 ) % 16 ;
263
+ // Phase overlaps 16 needles/solenoids (16*4 v1/v2 states)
264
+ unsigned encoder_phase ;
265
+ if (machine .belt_phase == REGULAR ) {
266
+ encoder_phase = (unsigned )(machine .carriage .position * 4 + 16 ) % 64 ;
267
+ } else {
268
+ encoder_phase = (unsigned )(machine .carriage .position * 4 + 48 ) % 64 ;
269
+ }
267
270
268
271
char needles [machine .num_needles ];
269
272
memset (needles , ' ' , machine .num_needles );
@@ -282,7 +285,7 @@ static void * avr_run_thread(void * param)
282
285
switch (event )
283
286
{
284
287
case CARRIAGE_LEFT :
285
- new_phase = (encoder_phase - 1 )%16 ;
288
+ new_phase = (encoder_phase - 1 )%64 ;
286
289
if ((new_phase %4 ) == 0 ) {
287
290
if (machine .carriage .position > -24 ) {
288
291
machine .carriage .position -- ;
@@ -293,7 +296,7 @@ static void * avr_run_thread(void * param)
293
296
}
294
297
break ;
295
298
case CARRIAGE_RIGHT :
296
- new_phase = (encoder_phase + 1 )%16 ;
299
+ new_phase = (encoder_phase + 1 )%64 ;
297
300
if ((new_phase %4 ) == 0 ) {
298
301
if (machine .carriage .position < (machine .num_needles + 24 )) {
299
302
machine .carriage .position ++ ;
@@ -312,7 +315,6 @@ static void * avr_run_thread(void * param)
312
315
fprintf (stderr , "Unexpect event from graphic thread\n" );
313
316
break ;
314
317
}
315
-
316
318
machine .hall_left = 1650 ;
317
319
machine .hall_right = 1650 ;
318
320
uint16_t solenoid_states = (shield .mcp23008 [1 ].reg [MCP23008_REG_OLAT ] << 8 ) + shield .mcp23008 [0 ].reg [MCP23008_REG_OLAT ];
@@ -407,15 +409,35 @@ static void * avr_run_thread(void * param)
407
409
408
410
selected_needle = machine .carriage .position + select_offset ;
409
411
if ((selected_needle < machine .num_needles ) && (selected_needle >= 0 )) {
410
- int solenoid_offset = (machine .type == KH270 ) ? 3 : 0 ;
411
- needles [selected_needle ] = solenoid_states & (1 << (solenoid_offset + selected_needle % machine .num_solenoids )) ? '.' : '|' ;
412
+ int solenoid_index ;
413
+ switch (machine .type ) {
414
+ case KH270 :
415
+ solenoid_index = selected_needle + 4 ;
416
+ // On KH270 solenoid to needle mapping is direction-dependent
417
+ if (event == CARRIAGE_LEFT ) {
418
+ solenoid_index += machine .num_solenoids >> 1 ;
419
+ }
420
+ // 12 solenoids mapped to position [3-14]
421
+ solenoid_index = 3 + solenoid_index % machine .num_solenoids ;
422
+ break ;
423
+ default :
424
+ solenoid_index = selected_needle ;
425
+ // Solenoid to needle mapping is belt phase-dependent
426
+ if (machine .belt_phase == SHIFTED ) {
427
+ solenoid_index += machine .num_solenoids >> 1 ;
428
+ }
429
+ solenoid_index = solenoid_index % machine .num_solenoids ;
430
+ break ;
431
+ }
432
+
433
+ needles [selected_needle ] = solenoid_states & (1 << solenoid_index ) ? '.' : '|' ;
412
434
solenoid_update = 1 ;
413
435
}
414
436
415
437
encoder_phase = new_phase ;
416
438
avr_raise_irq (encoder_v2 .irq + IRQ_BUTTON_OUT , (phase_map [encoder_phase % 4 ] & 1 ) ? 1 : 0 );
417
439
avr_raise_irq (encoder_v1 .irq + IRQ_BUTTON_OUT , (phase_map [encoder_phase % 4 ] & 2 ) ? 1 : 0 );
418
- avr_raise_irq (encoder_beltPhase .irq + IRQ_BUTTON_OUT , (encoder_phase & 8 ) ? 1 : 0 );
440
+ avr_raise_irq (encoder_beltPhase .irq + IRQ_BUTTON_OUT , (encoder_phase & 32 ) ? 1 : 0 );
419
441
420
442
if ((encoder_phase % 4 ) == 0 ) {
421
443
int half_num_needles = machine .num_needles / 2 ;
0 commit comments