123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #include "mbed.h"
- #include "main.h"
- #include "SWO.h"
- #include "pca9685.h"
- #include "tusb322.h"
- #include "ds3231.h"
- #include "ioc.h"
- #include "animation.h"
- I2C i2c(PA_10, PA_9);
- // SWO_Channel swo("swo");
- typedef enum {
- Decrementing = 0,
- Incrementing = 1,
- } DigitState;
- typedef struct {
- DigitState CurrentState;
- int Value;
- bool Updated;
- } Digit;
- typedef struct {
- Digit Digits[NUM_DIGITS];
- int LastActiveDigit;
- int RefreshLastDigit;
- bool Refresh;
- int FadeDuration;
- } Tube;
- Tube Tubes[NUM_TUBES]; // Active per-tube configuration
- Digit Dot;
- Timeout DotUpdateTimeout;
- void DotUpdateCallback(void) {
- if (Dot.CurrentState == Incrementing && Dot.Value < DOT_MAX) {
- Dot.Value = (Dot.Value + DOT_FADE_STEP >= DOT_MAX) ? DOT_MAX : Dot.Value + DOT_FADE_STEP;
- } else if (Dot.CurrentState == Decrementing && Dot.Value > DOT_MIN) {
- Dot.Value = (Dot.Value - DOT_FADE_STEP <= DOT_MIN) ? DOT_MIN : Dot.Value - DOT_FADE_STEP;
- }
- Dot.Updated = true;
- if (Dot.Value != DOT_MAX && Dot.Value != DOT_MIN) {
- DotUpdateTimeout.attach_us(DotUpdateCallback, REFRESH_RATE_US);
- }
- }
- // Macro the per-tube callback function as each callback needs to be defined as
- // a unique function (callbacks cannot have arguments and are not reentrant)
- #define TUBE_CALLBACK(x) \
- void Tube##x##UpdateCallback(void) { \
- int ticks = Tubes[x].FadeDuration / REFRESH_RATE_US; \
- int step = (DOT_MAX + ticks - 1) / ticks; \
- bool activeTube = false; \
- for (int i = 0; i < NUM_DIGITS; i++) { \
- Digit *digit = &Tubes[x].Digits[i]; \
- if (digit->CurrentState == Incrementing && digit->Value <= DIGIT_MAX) { \
- digit->Value = (digit->Value + step >= DIGIT_MAX) ? DIGIT_MAX : digit->Value + step; \
- digit->Updated = true; \
- } else if (digit->CurrentState == Decrementing && digit->Value >= DOT_MIN) { \
- digit->Value = (digit->Value - step <= DIGIT_MIN) ? DIGIT_MIN : digit->Value - step; \
- digit->Updated = true; \
- } \
- activeTube |= (digit->Value != DIGIT_MAX && digit->Value != DIGIT_MIN); \
- } \
- if (activeTube) { \
- Tube##x##UpdateTimeout.attach_us(Tube##x##UpdateCallback, REFRESH_RATE_US); \
- } \
- }
- Timeout Tube0UpdateTimeout;
- TUBE_CALLBACK(0)
- Timeout Tube1UpdateTimeout;
- TUBE_CALLBACK(1)
- Timeout Tube2UpdateTimeout;
- TUBE_CALLBACK(2)
- Timeout Tube3UpdateTimeout;
- TUBE_CALLBACK(3)
- void FadeInOutDigit(int T, int D, int Duration) {
- for (int i = 0; i < NUM_DIGITS; i++) {
- Tubes[T].Digits[i].CurrentState = Decrementing;
- }
- if (D != -1) {
- Tubes[T].Digits[D].CurrentState = Incrementing;
- }
- Tubes[T].FadeDuration = Duration;
- Tubes[T].LastActiveDigit = D;
- if (T == 0) {
- Tube0UpdateTimeout.attach_us(Tube0UpdateCallback, REFRESH_RATE_US);
- } else if (T == 1) {
- Tube1UpdateTimeout.attach_us(Tube1UpdateCallback, REFRESH_RATE_US);
- } else if (T == 2) {
- Tube2UpdateTimeout.attach_us(Tube2UpdateCallback, REFRESH_RATE_US);
- } else if (T == 3) {
- Tube3UpdateTimeout.attach_us(Tube3UpdateCallback, REFRESH_RATE_US);
- }
- }
- bool RtcTick, RefreshTick, RngTick;
- Ticker RefreshTicker, RngRefreshTicker;
- // Callback from DS3231 interrupt (1Hz)
- void RtcInterruptCallback(void) {
- RtcTick = true;
- }
- // Callback from RefreshTicker (REFRESH_RATE_US)
- void RefreshTickerCallback(void) {
- RefreshTick = true;
- }
- void RngTickerCallback(void) {
- RngTick = true;
- }
- Timeout RngUpdateTimeout;
- int RngUpdateIteration;
- void RngUpdateCallback(void) {
- for (int i = 0; i < NUM_TUBES; i++) {
- if (Tubes[i].Refresh) {
- if (RngUpdateIteration) {
- int nextDigit;
- do {
- nextDigit = rand() % NUM_DIGITS;
- } while (nextDigit == Tubes[i].LastActiveDigit ||
- (RngUpdateIteration == 1 && nextDigit == Tubes[i].RefreshLastDigit));
- FadeInOutDigit(i, nextDigit, DIGIT_RNG_FADE_DURATION_US);
- } else {
- FadeInOutDigit(i, Tubes[i].RefreshLastDigit, DIGIT_RNG_FADE_DURATION_US);
- Tubes[i].Refresh = false;
- }
- }
- }
- if (RngUpdateIteration-- != 0) {
- RngUpdateTimeout.attach_us(RngUpdateCallback, DIGIT_RNG_FADE_DURATION_US);
- }
- }
- int main() {
- // Initialize pointers in global data structure
- for (int i = 0; i < NUM_TUBES; i++) {
- for (int j = 0; j < NUM_DIGITS; j++) {
- Tubes[i].Digits[j].CurrentState = Decrementing;
- Tubes[i].Digits[j].Value = 0;
- Tubes[i].Digits[j].Updated = false;
- }
- Tubes[i].LastActiveDigit = -1;
- Tubes[i].Refresh = false;
- Tubes[i].FadeDuration = 0;
- }
- Dot.CurrentState = Decrementing;
- Dot.Value = 0;
- Dot.Updated = false;
- RtcTick = false;
- RefreshTick = false;
- RngTick = false;
- int nextSecond, nextMinute, nextHour;
- int prevMinute, prevHour;
- bool startup = true;
-
- // Start I2C at 400kHz for DS3231
- i2c.frequency(400000);
-
- // Start with HV PSU disabled
- HV_EnableOutput(false);
-
- TUSB322_Init();
- PCA9685_Init();
- DS3231_Init(RtcInterruptCallback);
- // Enable HV PSU
- HV_EnableOutput(true);
-
- // Set PCA9685 input voltage to highest possible
- PCA9685_SetVoltage(1.0);
-
- // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
-
- // Bump I2C frequency to 1MHz
- // i2c.frequency(1000000);
- // Setup a ticker to refresh the display at 1kHz
- RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
- // Kick off the RNG ticker at 0.1Hz
- RngRefreshTicker.attach(RngTickerCallback, DIGIT_RNG_REFRESH_RATE);
- // DS3231_SetTime(0, 55, 6, true);
- // DS3231_SetDate(0, 2, 12, 18, 0);
- while(1) {
- // Animate_Cycle_Basic();
- // Animate_Cycle_Analog();
- // Animate_Cycle_Low_Pwm();
- // Animate_Cycle_Pwm();
- // Animate_Cycle_Fade();
- // Animate_Cycle_Fade_Random();
- // Animate_Cycle_Fast();
- // Animate_Cycle_Fast_Random();
- // On every refresh tick, update the display with new values
- if (RefreshTick) {
- RefreshTick = false;
- for (int i = 0; i < NUM_TUBES; i++) {
- for (int j = 0; j < NUM_DIGITS; j++) {
- if (Tubes[i].Digits[j].Updated) {
- PCA9685_SetDigit(i, j, Tubes[i].Digits[j].Value);
- Tubes[i].Digits[j].Updated = false;
- }
- }
- }
- if (Dot.Updated) {
- PCA9685_SetDot(Dot.Value);
- Dot.Updated = false;
- }
- }
- // On every RTC tick, read and process the current time
- if (RtcTick) {
- RtcTick = false;
- // Save the previous time to check if digit(s) has changed
- prevMinute = nextMinute;
- prevHour = nextHour;
- DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
- // Update the display configuration based on the new/previous time
- if (startup || prevHour / 10 != nextHour / 10) {
- FadeInOutDigit(0, (nextHour / 10 != 0) ? nextHour / 10 : -1 , DIGIT_FADE_DURATION_US);
- }
- if (startup || prevHour % 10 != nextHour % 10) {
- FadeInOutDigit(1, nextHour % 10, DIGIT_FADE_DURATION_US);
- }
- if (startup || prevMinute / 10 != nextMinute / 10) {
- FadeInOutDigit(2, nextMinute / 10, DIGIT_FADE_DURATION_US);
- }
- if (startup || prevMinute % 10 != nextMinute % 10) {
- FadeInOutDigit(3, nextMinute % 10, DIGIT_FADE_DURATION_US);
- }
- Dot.CurrentState = (Dot.CurrentState == Decrementing) ? Incrementing : Decrementing;
- DotUpdateTimeout.attach_us(DotUpdateCallback, DOT_FADE_DURATION_US / REFRESH_RATE_US);
- // Clear the startup run-once flag after the first iteration
- startup = false;
- }
- if (RngTick) {
- RngTick = false;
- int refreshTube = rand() % NUM_TUBES;
- Tubes[refreshTube].RefreshLastDigit = Tubes[refreshTube].LastActiveDigit;
- Tubes[refreshTube].Refresh = true;
- RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
- RngUpdateCallback();
- }
- }
- }
- void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
- char buffer[I2C_MAX_BUFFER+1] = {0};
- if (Length > I2C_MAX_BUFFER) LED_Fault(1);
- buffer[0] = RegAddress;
- memcpy(&buffer[1], Data, Length);
- i2c.write(DeviceAddress << 1, buffer, Length + 1);
- }
- void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
-
- i2c.write(DeviceAddress << 1, &RegAddress, 1);
- i2c.read(DeviceAddress << 1, Data, Length);
- }
|