Initial commit

This commit is contained in:
Cynopolis
2021-10-10 11:39:32 -05:00
commit 01d85538bb
14 changed files with 2691 additions and 0 deletions

163
src/main.cpp Normal file
View File

@@ -0,0 +1,163 @@
/*
* Code Written by Quinn Henthorne: quinn.henthorne@gmail.com
* 10/7/2021
*
* Pinout List
* Dac0 - Waveform output
* Dac1 - Amplitude output
* D2 - Synch Pin Output (For debugging purposes)
* D22 - Ampltidue Encoder Input
* D23 - Amplitude Encoder Input
* D24 - Period Encoder Input
* D25 - Period Encoder Input
* D26 - Waveform Select Encoder
* D27 - Waveform Select Encoder
*/
#include <Arduino.h>
#include <Encoder.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#include <LiquidTWI2.h>
//uncomment to enable serial output for debugging
#define enable_serial_debug
#define waveform_pin DAC0
#define amplitude_pin DAC1
#define sync_pin 2
// Number of waveforms available
#define waveform_num 4
// Array of waveform arrays. Currently holds four waveforms with 600 samples each.
int waveforms[waveform_num][600] = {
{2083,2075,2067,2059,2071,2086,2100,2115,2129,2135,2141,2147,2153,2176,2176,2176,2176,2188,2201,2215,2228,2242,2255,2269,2282,2294,2305,2340,2352,2363,2376,2389,2401,2414,2427,2440,2453,2466,2478,2491,2504,2522,2539,2557,2574,2586,2597,2609,2621,2633,2644,2656,2668,2679,2691,2703,2714,2726,2738,2749,2761,2777,2792,2808,2820,2831,2843,2855,2867,2878,2890,2902,2960,2972,2984,2996,3007,3019,3030,3042,3053,3065,3078,3092,3105,3118,3131,3145,3158,3171,3181,3192,3202,3212,3223,3233,3243,3254,3264,3288,3294,3299,3314,3329,3343,3358,3364,3370,3393,3405,3417,3428,3440,3440,3452,3463,3463,3463,3463,3472,3481,3489,3498,3498,3498,3498,3498,3498,3510,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3516,3510,3510,3498,3493,3487,3475,3471,3467,3463,3451,3440,3428,3416,3408,3401,3358,3346,3323,3288,3276,3253,3229,3218,3195,3171,3148,3124,3083,3042,3025,3007,2978,2948,2931,2913,2872,2831,2750,2668,2645,2621,2586,2551,2480,2445,2328,2270,2247,2223,2184,2145,1989,1954,1931,1872,1866,1860,1849,1837,1802,1785,1767,1747,1728,1708,1673,1650,1626,1609,1591,1568,1533,1521,1474,1462,1451,1439,1381,1346,1310,1264,1229,1205,1176,1147,1135,1112,1088,1077,1065,1048,1030,983,966,948,928,909,889,866,849,831,819,808,796,784,761,749,714,702,691,679,667,655,650,644,626,608,597,591,585,574,562,550,539,503,492,480,468,456,445,433,433,433,433,433,421,421,418,416,413,410,410,410,410,407,404,401,398,398,398,398,398,398,398,398,398,398,398,404,410,415,421,425,429,433,439,445,451,456,458,461,463,466,468,476,483,491,496,501,505,510,515,523,530,538,544,550,556,562,574,585,587,590,592,595,597,609,620,623,626,629,632,638,644,644,655,661,667,673,679,687,694,702,720,737,755,772,796,819,843,866,892,919,945,971,1030,1053,1123,1147,1178,1209,1240,1310,1345,1381,1416,1445,1474,1509,1544,1580,1603,1619,1634,1650,1685,1720,1767,1872,1884,1895,1907,1942,1954,1966,2012,2030,2048,2129,2188,2258,2293,2352,2434,2463,2492,2609,2656,2726,2734,2742,2750,2820,2837,2854,2871,2888,2904,2921,2938,2955,2972,2996,3019,3048,3077,3083,3089,3101,3147,3159,3171,3183,3218,3230,3241,3288,3311,3319,3327,3335,3381,3399,3416,3440,3452,3463,3463,3463,3475,3475,3475,3487,3491,3494,3498,3498,3498,3498,3498,3498,3498,3498,3502,3506,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3487,3487,3485,3482,3480,3477,3475,3463,3459,3456,3452,3446,3440,3432,3424,3416,3411,3405,3399,3393,3370,3346,3342,3339,3335,3323,3311,3299,3299,3299,3264,3259,3253,3247,3241,3235,3229,3218,3206,3200,3194,3188,3182,3177,3171,3163,3155,3147,3136,3112,3101,3089,3074,3060,3045,3030,2995,2990,2984,2972,2960,2948,2925,2919,2913,2890,2885,2881,2876,2872,2867,2861,2855,2831,2820,2808,2802,2796,2787,2779,2770,2761,2745,2730,2691,2679,2656,2650,2597,2574,2551,2528,2399,2393,2387,2375,2363,2352,2340,2328,2305,2282,2278,2274,2270,2258,2247,2235,2223,2212,2200,
},
{2083,2090,2097,2104,2111,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2118,2120,2122,2125,2127,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2129,2131,2134,2136,2139,2141,2146,2151,2155,2160,2165,2168,2171,2173,2176,2185,2194,2202,2211,2211,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,2831,281,234,12,12,12,12,12,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,5,6,8,9,11,12,14,16,19,21,23,25,28,30,33,35,35,35,35,35,35,35,35,35,35,29,23,23,23,23,23,23,23,23,23,23,23,18,14,9,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,59,94,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,2083,2083,2083,2083,2083,2083,2083,2083,2083,2083,
},
{1637,1637,1637,4095,4086,4078,4069,4061,4052,4044,4035,4027,4018,4010,4001,3993,3985,3977,3968,3960,3952,3944,3936,3928,3920,3912,3903,3896,3888,3880,3872,3864,3856,3848,3840,3833,3825,3817,3810,3802,3794,3787,3779,3772,3764,3757,3749,3742,3734,3727,3720,3712,3705,3698,3690,3683,3676,3669,3662,3655,3647,3640,3633,3626,3619,3612,3605,3598,3591,3585,3578,3571,3564,3557,3551,3544,3537,3530,3524,3517,3510,3504,3497,3491,3484,3478,3471,3465,3458,3452,3446,3439,3433,3427,3420,3414,3408,3401,3395,3389,3383,3377,3371,3365,3358,3352,3346,3340,3334,3328,3322,3316,3311,3305,3299,3293,3287,3281,3276,3270,3264,3258,3253,3247,3241,3236,3230,3224,3219,3213,3208,3202,3197,3191,3186,3180,3175,3169,3164,3159,3153,3148,3143,3137,3132,3127,3121,3116,3111,3106,3101,3096,3090,3085,3080,3075,3070,3065,3060,3055,3050,3045,3040,3035,3030,3025,3020,3016,3011,3006,3001,2996,2991,2987,2982,2977,2972,2968,2963,2958,2954,2949,2945,2940,2935,2931,2926,2922,2917,2913,2908,2904,2899,2895,2890,2886,2882,2877,2873,2868,2864,2860,2855,2851,2847,2843,2838,2834,2830,2826,2822,2817,2813,2809,2805,2801,2797,2793,2789,2785,2781,2777,2773,2769,2765,2761,2757,2753,2749,2745,2741,2737,2733,2729,2725,2722,2718,2714,2710,2706,2703,2699,2695,2691,2688,2684,2680,2677,2673,2669,2666,2662,2659,2655,2651,2648,2644,2641,2637,2634,2630,2627,2623,2620,2616,2613,2609,2606,2603,2599,2596,2592,2589,2586,2582,2579,2576,2572,2569,2566,2563,2559,2556,2553,2550,2546,2543,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,0,6,12,17,23,29,34,40,46,51,57,62,68,73,79,84,90,95,101,106,112,117,122,128,133,138,143,149,154,159,164,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,274,279,284,289,294,298,303,308,312,317,322,326,331,336,340,345,349,354,358,363,367,372,376,381,385,389,394,398,403,407,411,416,420,424,428,433,437,441,445,450,454,458,462,466,470,474,479,483,487,491,495,499,503,507,511,515,519,523,527,531,534,538,542,546,550,554,558,561,565,569,573,576,580,584,588,591,595,599,602,606,610,613,617,620,624,628,631,635,638,642,645,649,652,656,659,663,666,669,673,676,680,683,686,690,693,696,700,703,706,709,713,716,719,722,726,729,732,735,738,742,745,748,751,754,757,760,764,767,770,773,776,779,782,785,788,791,794,797,800,803,806,809,811,814,817,820,823,826,829,832,834,837,840,843,846,848,851,854,857,860,862,865,868,870,873,876,879,881,884,887,889,892,894,897,900,902,905,907,910,913,915,918,920,923,925,928,930,933,935,938,940,943,945,947,950,952,955,957,960,962,964,967,969,971,974,976,978,981,983,985,988,990,992,994,997,999,1001,1003,1006,1008,1010,1012,1015,1017,1019,1021,1023,1025,1028,1030,1032,1034,1637,
},
{1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,2875,4095,4079,4062,4046,4030,4014,3999,3983,3967,3952,3937,3921,3906,3891,3876,3861,3846,3832,3817,3803,3788,3774,3760,3746,3732,3718,3704,3690,3677,3663,3650,3636,3623,3610,3597,3584,3571,3558,3545,3532,3520,3507,3495,3483,3470,3458,3446,3434,3422,3410,3399,3387,3375,3364,3352,3341,3329,3318,3307,3296,3285,3274,3263,3252,3242,3231,3220,3210,3199,3189,3179,3169,3158,3148,3138,3128,3118,3109,3099,3089,3079,3070,3060,3051,3041,3032,3023,3014,3005,2995,2986,2977,2969,2960,2951,2942,2934,2925,2916,2908,2899,2891,2883,2874,2866,2858,2850,2842,2834,2826,2818,2810,2802,2795,2787,2779,2772,2764,2757,2749,2742,2735,2727,2720,2713,2706,2699,2692,2685,2678,2671,2664,2657,2650,2644,2637,2630,2624,2617,2611,2604,2598,2591,2585,2579,2573,2566,2560,2554,2093,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,814,0,11,22,32,43,54,64,75,85,95,106,116,126,136,146,156,166,176,185,195,204,214,223,233,242,251,261,270,279,288,297,306,315,323,332,341,350,358,367,375,383,392,400,408,416,425,433,441,449,456,464,472,480,488,495,503,510,518,525,533,540,547,555,562,569,576,583,590,597,604,611,618,624,631,638,645,651,658,664,671,677,683,690,696,702,709,715,721,727,733,739,745,751,757,763,769,774,780,786,791,797,803,808,814,819,825,830,835,841,846,851,856,862,867,872,877,882,887,892,897,902,907,912,917,921,926,931,936,940,945,949,954,959,963,968,972,976,981,985,990,994,998,1002,1007,1011,1015,1019,1023,1027,1335,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,
}
};
//Setup encoders and their corresponding variables
Encoder amp_encoder(22, 23);
long new_amp = 64;
long old_amp = 64;
Encoder period_encoder(24, 25);
long new_period = 10;
long old_period = 10;
Encoder waveform_encoder(26, 27);
long new_waveform = 2;
long old_waveform = 2;
int waveform_select = 2;
//Setup LCD Display
LiquidCrystal_I2C lcd(0x27,16,2);
// Timer to regulate the step width
int timer = 0;
unsigned int millis_period = 13;
void setup() {
//The sync pin can be run into an oscilliscope's trig channel to easiy find the waveform
pinMode(sync_pin, OUTPUT);
//Change the resolution of the analog ourput to its maximum (12 bit res)
analogWriteResolution(12);
//If serial debugging is enabled set up serial
#ifdef enable_serial_debug
Serial.begin(115200);
#endif
// Initialize timers and encoders
timer = micros();
amp_encoder.write(new_amp*4);
period_encoder.write(new_period*4);
waveform_encoder.write(new_waveform*4);
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Hello");
// Read in encoder values
new_amp = amp_encoder.read()/4;
new_period = period_encoder.read()/4;
new_waveform = waveform_encoder.read()/4;
// Check to see if any of the encoder values have changed. If they have, update their values.
//Handles ampltidue encoder
if(new_amp != old_amp){
// Make sure the value is within a valid range
if(new_amp > 127){
//128 ampltidue steps should be enough granularity
new_amp = 127;
amp_encoder.write(new_amp*4);
}
if(new_amp < 0){
new_amp = 0;
amp_encoder.write(new_amp*4);
}
old_amp = new_amp;
}
//Handles period encoder
if(new_period != old_period){
// Make sure the value is within a valid range
if(new_period > 100){
new_period = 100;
period_encoder.write(new_period*4);
}
//Anything less than three will cause errors
if(new_period < 3){
new_period = 3;
period_encoder.write(new_period*4);
}
old_period = new_period;
}
//Handles waveform encoder
if(new_waveform != old_waveform){
// Make sure the value is within a valid range
if(new_waveform > waveform_num-1){
new_waveform = waveform_num-1;
waveform_encoder.write(new_waveform*4);
}
if(new_waveform < 0){
new_waveform = 0;
waveform_encoder.write(new_waveform*4);
}
old_waveform = new_waveform;
}
// Creates a synch signal so an oscilliscope can more easily read irregular pulses
digitalWrite(sync_pin, !digitalRead(sync_pin));
// Output a signal to control the amplitude
analogWrite(amplitude_pin, map(new_amp, 0, 127, 0, 4095));
// The period of time each sample in the array should take in microseconds
// The 1000 converts from milliseconds to microseconds, and the 600 deivides by the number of samples in the array
// The -3 offset compensates for a 3 millisecond overhead created by the time it takes to do all of the calculations
unsigned int sample_period = (new_period-3)*1000/600;
#ifdef enable_serial_debug
Serial.print("Ampltidue: ");
Serial.println(map(new_amp, 0, 127, 0, 4095));
Serial.print("Period: ");
Serial.println(new_period);
Serial.print("Waveform #: ");
Serial.println(new_waveform);
delay(500);
#endif
// Cycle through the waveform samples over a given period of time
for(int i = 0; i < 600; i++){
while(micros()-timer < sample_period);
analogWrite(waveform_pin, waveforms[new_waveform][i]);
timer = micros();
}
}