Commits

Trammell Hudson  committed 5a0e60a Draft

Repeat mouse scroll events based on pedal speed

  • Participants
  • Parent commits 3b1c642

Comments (0)

Files changed (1)

 }
 
 
+static inline int
+midi_ready(void)
+{
+	return bit_is_set(UCSR1A, RXC1);
+}
+
 static uint8_t
-getc(void)
+midi_getc(void)
 {
-	while (!(UCSR1A & (1 << RXC1)))
+	while (!midi_ready())
 		continue;
 	return UDR1;
 }
 
 	UCSR1B = (1 << RXEN1);
 
+	// Configure timer 1 to overflow every 10 Hz for repeated
+	// mouse motion.  This uses CTC mode; 
+	OCR1A = 2500;
+	TCCR1A = 0; // WGM11 and WGM10 = 0
+	TCCR1B = 0
+		| (0 << WGM13) // CTC mode
+		| (1 << WGM12)
+		| (0 << CS12)
+		| (1 << CS11)
+		| (1 << CS10) // clock / 64 == 16 Mhz / 64 / 25000 = 10 Hz
+		;
+
 	// Wait an extra second for the PC's operating system to load drivers
 	// and do whatever it does to actually be ready for input
 	_delay_ms(1000);
 	print("MIDI decoder\r\n");
 
-#if 0
-	while(1)
-	{
-		print("move\r\n");
-		usb_mouse_move(100,0,0);
-		// usb_keyboard_press(KEY_SPACE, 0);
-		_delay_ms(1000);
-	}
-#endif
+	int8_t pedal_value = 0;
 
 	while (1)
 	{
-		uint8_t c = getc();
+		if (!midi_ready())
+		{
+			if (pedal_value == 0)
+				continue;
+
+			if (bit_is_clear(TIFR1, OCF1A))
+				continue;
+
+			TIFR1 |= 1 << OCF1A;
+			usb_mouse_move(0, 0, pedal_value);
+			continue;
+		}
+
+		uint8_t c = midi_getc();
 
 		if (c == 0x40)
 		{
-			uint8_t program = getc();
+			uint8_t program = midi_getc();
 			uint8_t button = program % 10;
 			program /= 10;
 
 
 		if (c == 0x30)
 		{
-			uint8_t chan = getc();
-			uint8_t val = getc();
+			uint8_t chan = midi_getc();
+			uint8_t val = midi_getc();
 			print("pedal ");
 			phex(chan);
 			print(" ");
 			phex(val);
 			print("\r\n");
 
-			usb_mouse_move(0,0, val);
+			if (val < 2)
+			{
+				pedal_value = 0;
+				continue;
+			}
+
+			// Faster speeds also increase the
+			// repeat rate
+			pedal_value = -(int8_t)(val/4);
+			if (val < 64)
+				OCR1A = 25000;
+			else
+				OCR1A = 16000;
 			continue;
 		}
 
 		if (c == 0x10)
 		{
 			// Not sure?  Momentary?
-			uint8_t chan = getc();
-			uint8_t val = getc();
+			uint8_t chan = midi_getc();
+			uint8_t val = midi_getc();
 			print("momentary ");
 			phex(chan);
 			print(" ");