Commits

Anonymous committed fc73c58

Pulling the tone generation code.
STM32 chips have multiple timers, so the tone generation code should be in its own Tone library.

Comments (0)

Files changed (1)

 
 #include "TVout.h"
 
+HardwareTimer timer1(1);
 
 /* Call this to start video output with the default resolution.
  * 
 
 /* Stop video render and free the used memory.
  */
- void TVout::end() {
-	TIMSK1 = 0;
+void TVout::end() {
+  // Register for timer mask.
+  // TODO Disable timer interrupts?
+  timer1.setMode(TIMER_CH1, TIMER_DISABLED);
 	free(screen);
 }
 
  */
 void TVout::force_outstart(uint8_t time) {
 	delay_frame(1);
+  // Recalculate _CYCLES_PER_US for Maple.
 	display.output_delay = ((time * _CYCLES_PER_US) - 1);
 }
 
 
 /* Simple tone generation
  *
- * Arguments:
- *	frequency:
- *		the frequency of the tone
- *	duration_ms:
- *		The duration to play the tone in ms
- * courtesy of adamwwolf
+ * TODO: either link this to another library
+ * or warn users that this is deprecated and needs for them
+ * to use a real Tone.cpp library for Maple.
  */
 void TVout::tone(unsigned int frequency, unsigned long duration_ms) {
-
-	if (frequency == 0)
-		return;
-
-#define TIMER 2
-	//this is init code
-	TCCR2A = 0;
-	TCCR2B = 0;
-	TCCR2A |= _BV(WGM21);
-	TCCR2B |= _BV(CS20);
-	//end init code
-
-	//most of this is taken from Tone.cpp from Arduino
-	uint8_t prescalarbits = 0b001;
-	uint32_t ocr = 0;
-  
-
-    DDR_SND |= _BV(SND_PIN); //set pb3 (digital pin 11) to output
-
-    //we are using an 8 bit timer, scan through prescalars to find the best fit
-	ocr = F_CPU / frequency / 2 - 1;
-    prescalarbits = 0b001;  // ck/1: same for both timers
-    if (ocr > 255) {
-        ocr = F_CPU / frequency / 2 / 8 - 1;
-        prescalarbits = 0b010;  // ck/8: same for both timers
-
-        if (ocr > 255) {
-			ocr = F_CPU / frequency / 2 / 32 - 1;
-			prescalarbits = 0b011;
-        }
-
-        if (ocr > 255) {
-			ocr = F_CPU / frequency / 2 / 64 - 1;
-			prescalarbits = TIMER == 0 ? 0b011 : 0b100;
-			if (ocr > 255) {
-				ocr = F_CPU / frequency / 2 / 128 - 1;
-				prescalarbits = 0b101;
-			}
-
-			if (ocr > 255) {
-				ocr = F_CPU / frequency / 2 / 256 - 1;
-				prescalarbits = TIMER == 0 ? 0b100 : 0b110;
-				if (ocr > 255) {
-					// can't do any better than /1024
-					ocr = F_CPU / frequency / 2 / 1024 - 1;
-					prescalarbits = TIMER == 0 ? 0b101 : 0b111;
-				}
-			}
-        }
-    }
-    TCCR2B = prescalarbits;
-
-	if (duration_ms > 0)
-		remainingToneVsyncs = duration_ms*60/1000; //60 here represents the framerate
-	else
-		remainingToneVsyncs = -1;
- 
-    // Set the OCR for the given timer,
-    OCR2A = ocr;
-    //set it to toggle the pin by itself
-    TCCR2A &= ~(_BV(COM2A1)); //set COM2A1 to 0
-    TCCR2A |= _BV(COM2A0);
-} // end of tone
+  return;
+} 
 
 /* Stops tone generation
  */
 void TVout::noTone() {
-	TCCR2B = 0;
-	PORT_SND &= ~(_BV(SND_PIN)); //set pin 11 to 0
-} // end of noTone
+  return;
+}