Skip to content

Instantly share code, notes, and snippets.

@jackhumbert
Created July 30, 2015 23:56
Show Gist options
  • Save jackhumbert/795c680e7dad8cec81a5 to your computer and use it in GitHub Desktop.
Save jackhumbert/795c680e7dad8cec81a5 to your computer and use it in GitHub Desktop.

beeps.h

#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>

void note(int x, float length);
void beeps();
void true_note(float x, float y, float length);

beeps.c

#include "beeps.h"
#include <math.h>

#define PI 3.14159265

void delay_us(uint16_t count) {
  while(count--) {
    _delay_us(1);
  }
}

void beeps() {
    DDRB |= (1<<1);
	for (int r = 0; r < 2; r++) { // repeat
	    for (int x = 0; x < 3; x++) { // arpeggio
	    int t = 500*pow(2,-x); // starting note
	        for (int y = 0; y < 12000/t; y++) { // note length
	            PORTB |= (1<<1);
	            delay_us(t);
	            PORTB &= ~(1<<1);
	            delay_us(t);
	        }
	    }
	}
}

void note(int x, float length) {
    DDRB |= (1<<1);
	int t = (int)(440*pow(2,-x/12.0)); // starting note
    for (int y = 0; y < length*1000/t; y++) { // note length
        PORTB |= (1<<1);
        delay_us(t);
        PORTB &= ~(1<<1);
        delay_us(t);
    }
	PORTB &= ~(1<<1);
}

void true_note(float x, float y, float length) {
	for (uint32_t i = 0; i < length * 50; i++) {
		uint32_t v = (uint32_t) (round(sin(PI*2*i*640000*pow(2, x/12.0))*.5+1 + sin(PI*2*i*640000*pow(2, y/12.0))*.5+1) / 2 * pow(2, 8)); 
		for (int u = 0; u < 8; u++) {
			if (v & (1 << u) && !(PORTB&(1<<1)))
		        PORTB |= (1<<1);
		    else if (PORTB&(1<<1))
	        	PORTB &= ~(1<<1);
		}
	}
	PORTB &= ~(1<<1);
}

Add beeps.c to Makefile:

SRC = extended_keymap_common.c \
	analog.c \
	led.c \
	backlight.c \
	beeps.c

Add beeps.h to the header of matrix_pcb.c:

#include "beeps.h"

Add start-up beeps to matrix_init() in matrix_pcb.c:

beeps();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment