swap between rising and falling edge detection

This commit is contained in:
Andy Killorin 2025-04-03 12:26:03 -04:00
parent 808d474ec3
commit b091f216e8
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

26
main.c
View file

@ -51,7 +51,7 @@ const float gVoltageScale[VOLTAGE_SCALES] = {
uint32_t cputime_unloaded; uint32_t cputime_unloaded;
int RisingTrigger(void); int Trigger(bool rising);
// start a pwm test signal // start a pwm test signal
void start_signal() { void start_signal() {
@ -129,6 +129,7 @@ int main(void) {
IntMasterEnable(); IntMasterEnable();
uint8_t voltage_scale = 0; uint8_t voltage_scale = 0;
uint8_t rising = 1;
while (true) { while (true) {
// calculate cpu usage // calculate cpu usage
@ -141,6 +142,9 @@ int main(void) {
Button button = (Button) 0; Button button = (Button) 0;
while (fifo_get(&button)) { while (fifo_get(&button)) {
switch (button) { switch (button) {
case S1: // toggle edge
rising = !rising;
break;
case S2: // draw case S2: // draw
refresh = !refresh; refresh = !refresh;
break; break;
@ -175,13 +179,19 @@ int main(void) {
snprintf(str, sizeof(str), "CPU Load %.1f%%", usage_percent); snprintf(str, sizeof(str), "CPU Load %.1f%%", usage_percent);
GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 10, /*opaque*/ false); GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 10, /*opaque*/ false);
if (rising) {
GrStringDraw(&sContext, "^", /*length*/ -1, /*x*/ WIDTH - 10, /*y*/ 0, /*opaque*/ false);
} else {
GrStringDraw(&sContext, "v", /*length*/ -1, /*x*/ WIDTH - 10, /*y*/ 0, /*opaque*/ false);
}
// display graph // display graph
#define LOCAL_BUF_LEN 128 #define LOCAL_BUF_LEN 128
uint16_t local_adc_buffer[LOCAL_BUF_LEN]; // copy of adc buffer uint16_t local_adc_buffer[LOCAL_BUF_LEN]; // copy of adc buffer
int32_t adc_current_index; int32_t adc_current_index;
int32_t j; int32_t j;
if (refresh) { if (refresh) {
int trigger = RisingTrigger(); int trigger = Trigger(rising);
adc_current_index = trigger - (WIDTH / 2); adc_current_index = trigger - (WIDTH / 2);
for (j=0; j<LOCAL_BUF_LEN; j++) { for (j=0; j<LOCAL_BUF_LEN; j++) {
@ -251,15 +261,23 @@ int main(void) {
} }
} }
int RisingTrigger(void) // search for rising edge trigger int Trigger(bool rising) // search for edge trigger
{ {
int x = gADCBufferIndex - (WIDTH / 2); // half screen width int x = gADCBufferIndex - (WIDTH / 2); // half screen width
int x_stop = x - ADC_BUFFER_SIZE/2; int x_stop = x - ADC_BUFFER_SIZE/2;
for (; x > x_stop; x--) { for (; x > x_stop; x--) {
if (rising) {
if ( gADCBuffer[ADC_BUFFER_WRAP(x)] >= ADC_OFFSET && if ( gADCBuffer[ADC_BUFFER_WRAP(x)] >= ADC_OFFSET &&
gADCBuffer[ADC_BUFFER_WRAP(x-1)] < ADC_OFFSET) gADCBuffer[ADC_BUFFER_WRAP(x-1)] < ADC_OFFSET) {
break; break;
} }
} else { // falling edge trigger
if ( gADCBuffer[ADC_BUFFER_WRAP(x)] <= ADC_OFFSET &&
gADCBuffer[ADC_BUFFER_WRAP(x-1)] > ADC_OFFSET) {
break;
}
}
}
if (x == x_stop) // for loop ran to the end if (x == x_stop) // for loop ran to the end
x = x = gADCBufferIndex - (WIDTH / 2); // reset x back to how it was initialized x = x = gADCBufferIndex - (WIDTH / 2); // reset x back to how it was initialized
return x; return x;