#include "Pico_SevenSeg.h"
#include "Pico_SimpleMultimeter.h"
#include <stdio.h>
#include <stdlib.h>
#include "hardware/gpio.h"
#define AMPMETER_RESISTANCE 10
#define DISPLAY_SCALER 100 // The display library only takes integer values. Multiply by 100 to get 2 decimal places
#define MODE_CHANGE_BTN_GPIO 20
#define BTN_DEBOUNCE_TIME_MS 200
/*----------Declarations-------------*/
int onMeasureCallback(float volts, float amps);
typedef enum MeasureMode_t
{
MEASURE_MODE_VOLTS,
MEASURE_MODE_AMPS
}MeasureMode_t;
MeasureMode_t measureMode = MEASURE_MODE_VOLTS;
SevenSeg4Handle_t myDisplay = NULL;
SevenSeg4Params_t displayParams = {
.segGpios = {2,3,4,5,6,7,8},
.digitGpios = {19,18,17,16},
.isCommonAnode = false,
.updateType = SEVEN_SEG_UPDATE_INTERRUPT,
.blinkPeriod = 100, // 100 * 5ms = 500 ms
.decimalPointGpio = 9
};
SimpleMultimeterHandle_t myMultimeter = NULL;
SimpleMultimeterParams_t multimeterParams = {
.ampmeterCahnnel = 2,
.voltimeterChannel = 1,
.measurePeriodMs = 100,
.measureType = MEASURE_TYPE_PERIODIC,
.resistance = AMPMETER_RESISTANCE,
.onMeasure = onMeasureCallback
};
int onMeasureCallback(float volts, float amps)
{
uint32_t value = 0;
switch (measureMode)
{
case MEASURE_MODE_VOLTS:
SevenSeg4_SetDecimalPlace(myDisplay,2);
value = volts * DISPLAY_SCALER;
break;
case MEASURE_MODE_AMPS:
SevenSeg4_SetDecimalPlace(myDisplay,1);
value = amps * DISPLAY_SCALER * 100; // *100 to measure in milliamps and shift the decimal place
break;
default:
break;
}
SevenSeg4_SetValue(myDisplay,value);
return 0;
}
uint64_t lastRisingEdgeTime; // In micro seconds
static void modeChangeBtnISR(unsigned int gpio, uint32_t evenMask)
{
uint64_t time = get_absolute_time();
if(time <= lastRisingEdgeTime + (BTN_DEBOUNCE_TIME_MS * 1000) || gpio != MODE_CHANGE_BTN_GPIO)
return;
lastRisingEdgeTime = time;
if(measureMode == MEASURE_MODE_VOLTS)
{
printf("Changed mode to AMPS\n");
measureMode = MEASURE_MODE_AMPS;
} else if(measureMode == MEASURE_MODE_AMPS)
{
printf("Changed mode to VOLTS\n");
measureMode = MEASURE_MODE_VOLTS;
}
return;
}
void main()
{
stdio_init_all();
sleep_ms(3000);
gpio_init(MODE_CHANGE_BTN_GPIO);
gpio_set_irq_enabled_with_callback(MODE_CHANGE_BTN_GPIO,GPIO_IRQ_EDGE_RISE,true,&modeChangeBtnISR);
SevenSeg4_Init(&displayParams,&myDisplay);
SevenSeg4_SetDecimalPlace(myDisplay,2);
SimpleMultimeter_Init(&multimeterParams,&myMultimeter);
while(1)
{
sleep_ms(1);
}
}Loading
pi-pico-w
pi-pico-w