114 lines
3.7 KiB
C
Executable File
114 lines
3.7 KiB
C
Executable File
#include <ch32v00x.h>
|
|
#include <debug.h>
|
|
#include "neopixel_driver.h"
|
|
// #include "bit_bang_neopixel.h"
|
|
#include "uart_driver.h"
|
|
#include "hall_driver.h"
|
|
#include "i2c_slave.h"
|
|
|
|
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
|
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
|
void Delay_Init(void);
|
|
void Delay_Ms(uint32_t n);
|
|
void init_button(void);
|
|
|
|
int main(void)
|
|
{
|
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
|
|
SystemCoreClockUpdate();
|
|
Delay_Init();
|
|
|
|
// allow some time to enter flash mode, in case something gets screwed up
|
|
Delay_Ms(1000);
|
|
|
|
USARTx_CFG();
|
|
UartStringSend("Hello World!\r\n");
|
|
|
|
SPINeoPixelDriverInit();
|
|
// BBNeoPixelDriverInit();
|
|
|
|
i2c_init();
|
|
|
|
init_button();
|
|
ADCConfig();
|
|
|
|
uint16_t adc_readings[4] = {0};
|
|
uint16_t adc_calibration_values[4] = {512, 512, 512, 512};
|
|
while (1) {
|
|
// button will calibrate
|
|
if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0) {
|
|
adc_calibration_values[0] = adc_readings[0];
|
|
adc_calibration_values[1] = adc_readings[1];
|
|
adc_calibration_values[2] = adc_readings[2];
|
|
adc_calibration_values[3] = adc_readings[3];
|
|
}
|
|
|
|
// start an adc convertion
|
|
ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);
|
|
Delay_Ms(10);
|
|
|
|
// wait until the conversion is done
|
|
while (adcFlag != 1) {
|
|
Delay_Us(100);
|
|
}
|
|
|
|
adc_readings[0] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1);
|
|
adc_readings[1] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_2);
|
|
adc_readings[2] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_3);
|
|
adc_readings[3] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_4);
|
|
|
|
adcFlag = 0;
|
|
|
|
char buffer[64] = {0};
|
|
// sprintf(buffer, "%d\t%d\t%d\t%d\r\n", adc_readings[0], adc_readings[1], adc_readings[2], adc_readings[3]);
|
|
// UartStringSend(buffer);
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
if (adc_readings[i] > adc_calibration_values[i] + 5) {
|
|
float value = (float)(adc_readings[i] - adc_calibration_values[i]) / 512.0;
|
|
if (value > 1.0) value = 1.0;
|
|
if (value < 0.0) value = 0.0;
|
|
uint8_t color = 255 * value;
|
|
|
|
SendNeoPixelColor(color, 0, 0);
|
|
SendNeoPixelColor(color, 0, 0);
|
|
SendNeoPixelColor(color, 0, 0);
|
|
SendNeoPixelColor(color, 0, 0);
|
|
}
|
|
else if (adc_readings[i] < adc_calibration_values[i] - 5) {
|
|
float value = (float)(adc_calibration_values[i] - adc_readings[i]) / 512.0;
|
|
if (value > 1.0) value = 1.0;
|
|
if (value < 0.0) value = 0.0;
|
|
uint8_t color = 255 * value;
|
|
|
|
SendNeoPixelColor(0, 0, color);
|
|
SendNeoPixelColor(0, 0, color);
|
|
SendNeoPixelColor(0, 0, color);
|
|
SendNeoPixelColor(0, 0, color);
|
|
} else {
|
|
SendNeoPixelColor(0, 0, 0);
|
|
SendNeoPixelColor(0, 0, 0);
|
|
SendNeoPixelColor(0, 0, 0);
|
|
SendNeoPixelColor(0, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void init_button(void) {
|
|
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
}
|
|
|
|
void NMI_Handler(void) {
|
|
SendNeoPixelColor(0, 255, 0);
|
|
}
|
|
void HardFault_Handler(void)
|
|
{
|
|
SendNeoPixelColor(0, 255, 0);
|
|
while (1);
|
|
}
|