summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 19f280dc00bbdd8060a7c96064d0bc1a47c34271 (plain)
1
2
3
4
5
6
7
8
9
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
#include "mbed.h"
#include "spi_init.h"
#include "counter.h"
#include "Adafruit_SSD1306.h"

#define SW_PINS p24, p23, p22, p21
#define SW_PERIOD 20000 // 20ms

void tout(void);
void pwm_invert(void);

// Onboard LED 
DigitalOut out_wave(LED1);
PinName switch_pin[] = { SW_PINS };

Counter *switch_position[4];
Ticker timer;
Ticker pwm;

volatile uint16_t switch_count[4] = { 0, 0, 0, 0 };
volatile uint16_t switch_pressed[4] = { 0, 0, 0, 0 };
volatile uint16_t last_pressed[4] = { 0, 0, 0, 0 };

uint16_t current_f[4] = { 0, 0, 0, 0 };

volatile uint16_t update = 0;

// Initialise display
SPInit gSpi(D_MOSI_PIN, NC, D_CLK_PIN);	
Adafruit_SSD1306_Spi oled(gSpi, D_DC_PIN, D_RST_PIN, D_CS_PIN, 64, 128);

int main(void)
{
	oled.setRotation(2);
	wait(0.5);

	// Enable ISR for the switch rising edge
	for (int i = 0; i < 4; ++i) {
		switch_position[i] = new Counter(switch_pin[i]);	
	}

	//Attach switch sampling timer ISR to the timer instance with the required period
	timer.attach_us(&tout, SW_PERIOD);

	oled.clearDisplay();
	oled.printf("%ux%u Group Ay08-04\r\n", oled.width(), oled.height());

	for (;;) {
		if (update) {
			update = 0;

			oled.setTextCursor(0, 0);

			//Write the latest switch osciallor count
			for (int i = 3; i >= 0; --i) {
				current_f[i] += (switch_pressed[i] && !last_pressed[i]);
				if (current_f[i] > 9)
					current_f[i] = 0;
				oled.printf("\nS:%u C:%05u N:%u", switch_pressed[i], switch_count[i], current_f[i]);
			}

			uint16_t frequency = 1000*current_f[3] + 100*current_f[2] + 10*current_f[1] + current_f[0];

			oled.printf("\nF:%u   ", frequency);

			if (frequency < 25)
				pwm.attach_ms(&pwm_invert, 500/frequency);
			else
				pwm.attach_us(&pwm_invert, 500000/frequency);

			//Copy the display buffer to the display
			oled.display();
		}

	}
}


//Interrupt Service Routine for the switch sampling timer
void tout(void)
{
	// Query how many times switch triggered
	for (int i = 0; i < 4; ++i) {
		switch_count[i] = switch_position[i]->read();
		switch_position[i]->write(0);
		
		last_pressed[i] = switch_pressed[i];

		if (switch_count[i] < 600)
			switch_pressed[i] = 1;
		else if (switch_count[i] > 700)
			switch_pressed[i] = 0;
	}
	// Update display
	update = 1;
}

void pwm_invert(void){
	out_wave = !out_wave;
}