summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWaleed El-Geresy <waleed.el-geresy15@imperial.ac.uk>2017-05-08 12:06:25 +0100
committerVasil Zlatanov <v@skozl.com>2017-05-10 16:39:44 +0100
commitae77622c84de56d1183a98b85b60ac9411b4bebf (patch)
tree1636c9bdd24c91c2eebeea46ea5ec20ed4064733
parentada3557c0a579f89f20ee3d1da7e1cceb24685db (diff)
downloade2-switch-ae77622c84de56d1183a98b85b60ac9411b4bebf.tar.gz
e2-switch-ae77622c84de56d1183a98b85b60ac9411b4bebf.tar.bz2
e2-switch-ae77622c84de56d1183a98b85b60ac9411b4bebf.zip
Add lookup table
-rw-r--r--src/main.cpp21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 1406b74..9cc47fb 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,6 +6,7 @@
#define SW_PINS p24, p23, p22, p21
#define SW_PERIOD 20000 // 20ms
+#define ARRAY_SIZE 360
void tout(void);
void square(void);
@@ -30,16 +31,24 @@ volatile uint16_t update = 0;
volatile uint16_t amp = 0;
const double pi = 3.141592653589793238462;
-const double offset = 65535/2;
+const double offset = 65535/2; //Offset is 1/2 the total bits
double rads = 0.0;
uint16_t sample = 0;
+uint16_t sineArray[ARRAY_SIZE];
+
// 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)
{
+ for(int i=0; i<ARRAY_SIZE; i++)
+ {
+ int rads = (pi * i) / (ARRAY_SIZE/2.0f);
+ sineArray[i] = (uint16_t)(0.5f * (offset * (cos(rads + pi))) + offset);
+ }
+
uint16_t frequency = 0;
uint16_t old_frequency = 0;
@@ -123,11 +132,11 @@ void triangle(void){
}
void sine(void){
- rads = (pi * amp) / 180.0f;
- sample = (uint16_t)(0.5f * (offset * (cos(rads + pi))) + offset);
- out_wave.write_u16(sample);
+ if(amp == ARRAY_SIZE)
+ amp = 0;
+
+ out_wave.write_u16(sineArray[amp]);
amp++;
- if(amp == 360)
- amp = 0;
}
+