microscope-ring-light-remot.../main.c

190 lines
5.0 KiB
C

#include <poti.h>
#include <i2c.h>
#include <stdint.h>
#include <stm32f0xx.h>
#include <stm-periph/stm32-gpio-macros.h>
#include <systick.h>
#include <dmx.h>
#define PCA9555_ADDR (0x40)
#define PCA9555_REG_IN_PORT0 (0x0)
#define PCA9555_REG_IN_PORT1 (0x1)
#define PCA9555_REG_OUT_PORT0 (0x2)
#define PCA9555_REG_OUT_PORT1 (0x3)
#define PCA9555_REG_POLARITY0 (0x4)
#define PCA9555_REG_POLARITY1 (0x5)
#define PCA9555_REG_CONFIG0 (0x6)
#define PCA9555_REG_CONFIG1 (0x7)
static const uint8_t gamma8[] = {
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, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255
};
static uint8_t dmx_universe[129];
enum color_mode {
MODE_RED,
MODE_GREEN,
MODE_BLUE,
MODE_RGB,
MODE_WHITE_DISCRETE,
};
static void setup_pins(void)
{
uint32_t tmp;
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
tmp = GPIOA->MODER;
/* Reset all used port pins */
tmp &= MODER_DELETE(0) & MODER_DELETE(1) & MODER_DELETE(2) & MODER_DELETE(3) &
MODER_DELETE(4) & MODER_DELETE(5) & MODER_DELETE(6) & MODER_DELETE(7) &
MODER_DELETE(8) & MODER_DELETE(9) & MODER_DELETE(10);
/* Analog ports for POTIs */
tmp |= ANALOG(0) | ANALOG(1) | ANALOG(4);
/* Setup USART1 RX/TX (AF1) */
tmp |= ALTFUNC(2) | ALTFUNC(3);
SETAF(GPIOA, 2, 1);
SETAF(GPIOA, 3, 1);
/* LED enables for Potentiometers */
tmp |= OUTPUT(5) | OUTPUT(6) | OUTPUT(7);
GPIOA->ODR &= ~((1 << 5) | (1 << 6) | (1 << 7));
/* Setup I2C pins (AF4) */
tmp |= ALTFUNC(9) | ALTFUNC(10);
SETAF(GPIOA, 9, 4);
SETAF(GPIOA, 10, 4);
/* Set I2C Pins to open drain. Others are push pull */
GPIOA->OTYPER = OTYP_OPENDRAIN(9) | OTYP_OPENDRAIN(10);
/* Write changes to GPIOA mode register */
GPIOA->MODER = tmp;
/* PB1 Input with pullup */
GPIOB->MODER &= MODER_DELETE(1);
GPIOB->PUPDR = PULLUP(1);
}
int main(void)
{
uint8_t i2c_command[2];
uint8_t port;
uint32_t odr;
enum color_mode mode = MODE_RED;
struct poti_values poti_vals;
setup_pins();
i2c_init();
dmx_init(dmx_universe, sizeof(dmx_universe), GPIOA, 2u, 2300u, 10u, 5u);
poti_init_adc();
/* Setup Systick for 1ms ticks */
SysTick_Config(48000000UL/1000);
GPIOA->ODR |= (1<<5) | (1<<7);
/* Setup PCA */
i2c_command[0] = PCA9555_REG_CONFIG1;
i2c_command[1] = 0x00;
i2c_write(PCA9555_ADDR, i2c_command, 2u);
i2c_command[0] = PCA9555_REG_CONFIG0;
i2c_command[1] = 0xFF;
i2c_write(PCA9555_ADDR, i2c_command, 2u);
dmx_stream_start();
dmx_universe[0] = 0x55;
dmx_universe[1] = 0x50;
/* Blink the LEDs */
while (1) {
systick_wait_ms(10);
poti_get_values(&poti_vals);
i2c_read(PCA9555_ADDR, PCA9555_REG_IN_PORT0, &port, 1u);
i2c_command[0] = PCA9555_REG_OUT_PORT1;
i2c_command[1] = ~port;
i2c_write(PCA9555_ADDR, i2c_command, 2u);
odr = GPIOA->ODR;
odr &= ~((1<<5) | (1<<6) | (1<<7));
if (port & (1<<0)) {
mode = MODE_RED;
}
if (port & (1<<1)) {
mode = MODE_GREEN;
}
if (port & (1<<2)) {
mode = MODE_BLUE;
}
if (port & (1<<3)) {
mode = MODE_RGB;
}
if (port & (1<<4)) {
mode = MODE_WHITE_DISCRETE;
}
switch (mode) {
case MODE_RED:
case MODE_GREEN:
case MODE_BLUE:
for (int i = 0; i < 32; i++) {
dmx_universe[i*4] = mode == MODE_RED ? poti_vals.pot_vals_filtered[0] : 0;
dmx_universe[i*4+1] = mode == MODE_GREEN ? poti_vals.pot_vals_filtered[0] : 0;
dmx_universe[i*4+2] = mode == MODE_BLUE ? poti_vals.pot_vals_filtered[0] : 0;
dmx_universe[i*4+3] = 0x0;
}
dmx_universe[128] = 0;
odr |= (1<<5);
break;
case MODE_RGB:
odr |= (1<<5) | (1<<6) | (1<<7);
for (int i = 0; i < 32; i++) {
dmx_universe[i*4] =poti_vals.pot_vals_filtered[0];
dmx_universe[i*4+1] = poti_vals.pot_vals_filtered[1];
dmx_universe[i*4+2] = poti_vals.pot_vals_filtered[2];
dmx_universe[i*4+3] = 0x0;
}
dmx_universe[128] = 0;
break;
case MODE_WHITE_DISCRETE:
odr |= (1<<5);
for (int i = 0; i < 32; i++) {
dmx_universe[i*4] = 0;
dmx_universe[i*4+1] = 0;
dmx_universe[i*4+2] = 0;
dmx_universe[i*4+3] = 0;
}
dmx_universe[128] = gamma8[poti_vals.pot_vals_filtered[0]];
break;
default:
break;
}
GPIOA->ODR = odr;
}
}