123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- #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");
- #ifdef REVISION_B
- Serial serial(PB_6, PB_7);
- #endif
- typedef enum {
- Decrementing = 0,
- Incrementing = 1,
- } DigitState;
- typedef struct {
- DigitState CurrentState;
- int Value;
- int PwmStart;
- int PwmEnd;
- bool Updated;
- } Digit;
- typedef struct {
- Digit Digits[NUM_DIGITS];
- int LastActiveDigit;
- int RefreshLastDigit;
- bool RefreshActive;
- int FadeDuration;
- } Tube;
- Tube Tubes[NUM_TUBES]; // Active per-tube configuration
- Digit Dot;
- Timeout DotUpdateTimeout;
- void DotUpdateCallback(void) {
- int ticks = DOT_FADE_DURATION_US / REFRESH_RATE_US;
- int step = ((DOT_MAX - DOT_MIN) + ticks - 1) / ticks;
- if (Dot.CurrentState == Incrementing && Dot.Value < DOT_MAX) {
- Dot.Value = (Dot.Value + step >= DOT_MAX) ? DOT_MAX : Dot.Value + step;
- } else if (Dot.CurrentState == Decrementing && Dot.Value > DOT_MIN) {
- Dot.Value = (Dot.Value - step <= DOT_MIN) ? DOT_MIN : Dot.Value - 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 = ((DIGIT_MAX - DIGIT_MIN) + 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 >= DIGIT_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 UpdatePwm(Digit *digit, int *lastPwm, bool *increment) {
- for (int i = 0; i < NUM_TUBES; i++) {
- for (int j = 0; j < NUM_DIGITS; j++) {
- if (digit->Value == PCA9685_Min_Brightness) {
- digit->PwmStart = 0;
- digit->PwmEnd = 0;
- }
- else if (digit->Value == PCA9685_Max_Brightness) {
- digit->PwmStart = 0;
- digit->PwmEnd = PCA9685_Max_Brightness;
- }
- else {
- if (*increment) {
- if (*lastPwm + digit->Value > PCA9685_Max_Brightness) {
- digit->PwmStart = PCA9685_Max_Brightness - digit->Value;
- digit->PwmEnd = PCA9685_Max_Brightness;
- *lastPwm = digit->PwmStart;
- *increment = false;
- } else {
- digit->PwmStart = *lastPwm;
- digit->PwmEnd = digit->PwmStart + digit->Value;
- *lastPwm = digit->PwmEnd;
- }
- } else {
- if (*lastPwm - PCA9685_Min_Brightness < digit->Value) {
- digit->PwmStart = PCA9685_Min_Brightness;
- digit->PwmEnd = digit->PwmStart + digit->Value;
- *lastPwm = digit->PwmEnd;
- *increment = true;
- } else {
- digit->PwmEnd = *lastPwm;
- digit->PwmStart = digit->PwmEnd - digit->Value;
- *lastPwm = digit->PwmStart;
- }
- }
- digit->Updated = true;
- }
- }
- }
- }
- // In the event that there are multiple PWM outputs at less than 100% duty cycle,
- // stagger the start time of each PWM to reduce the switch on surge current. If the
- // duty cycle is greater than 100%, distribute the PWM outputs as much as possible
- // to keep the current consumption at a minimum.
- void ComputePwmOffsets() {
- int lastPwm = 0;
- bool increment = true;
- for (int i = 0; i < NUM_TUBES; i++) {
- for (int j = 0; j < NUM_DIGITS; j++) {
- UpdatePwm(&Tubes[i].Digits[j], &lastPwm, &increment);
- }
- }
- UpdatePwm(&Dot, &lastPwm, &increment);
- }
- void FadeInOutDigit(int T, int D, int Duration, bool RngUpdate = false) {
- // If the tube is in the middle of a refresh sequence and a call comes
- // in to update the tube digit (for time), override the last value of
- // the refresh sequence with the new digit.
- if (Tubes[T].RefreshActive && !RngUpdate) {
- Tubes[T].RefreshLastDigit = D;
- }
- // Dont update if actively refreshing tube unless RngUpdate is set
- if ((!RngUpdate && !Tubes[T].RefreshActive) || (RngUpdate)) {
- 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;
- Timeout RngRefreshTimeout;
- // 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) {
- int newfadeDuration = DIGIT_FADE_DURATION_US - RngUpdateIteration * DIGIT_RNG_FADE_DURATION_US;
- if (newfadeDuration <= DIGIT_RNG_FADE_DURATION_US) newfadeDuration = DIGIT_RNG_FADE_DURATION_US;
- for (int i = 0; i < NUM_TUBES; i++) {
- if (Tubes[i].RefreshActive) {
- if (RngUpdateIteration) {
- int nextDigit;
- #ifdef REFRESH_SEQUENTIAL
- nextDigit = (Tubes[i].LastActiveDigit >= 9 || Tubes[i].LastActiveDigit < 0) ?
- 0 : Tubes[i].LastActiveDigit + 1;;
- #endif
- #ifdef REFRESH_RANDOM
- do {
- nextDigit = rand() % NUM_DIGITS;
- } while (nextDigit == Tubes[i].LastActiveDigit ||
- (RngUpdateIteration == 1 && nextDigit == Tubes[i].RefreshLastDigit));
- #endif
- FadeInOutDigit(i, nextDigit, newfadeDuration, true);
- } else {
- FadeInOutDigit(i, Tubes[i].RefreshLastDigit, newfadeDuration, true);
- Tubes[i].RefreshActive = false;
- }
- }
- }
- if (RngUpdateIteration-- != 0) {
- RngUpdateTimeout.attach_us(RngUpdateCallback, newfadeDuration);
- }
- }
- int main() {
- #ifdef REVISION_B
- serial.baud(115200);
- #endif
- // 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 = DIGIT_MIN;
- Tubes[i].Digits[j].Updated = false;
- }
- Tubes[i].LastActiveDigit = -1;
- Tubes[i].RefreshLastDigit = -1;
- Tubes[i].RefreshActive = true;
- Tubes[i].FadeDuration = 0;
- }
- Dot.CurrentState = Decrementing;
- Dot.Value = DOT_MIN;
- Dot.Updated = false;
- RtcTick = false;
- RefreshTick = false;
- RngTick = false;
- int nextSecond, nextMinute, nextHour;
- int prevMinute, prevHour;
- int day, date, month, year;
- bool startup = true;
-
- // Start I2C at 400kHz for DS3231
- i2c.frequency(400000);
-
- // Start with HV PSU disabled
- HV_EnableOutput(false);
-
- IO_Init();
- 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);
- // Animate_Cycle_Basic(10);
- // Animate_Cycle_Analog(10);
- // Animate_Cycle_Low_Pwm(10);
- // Animate_Cycle_Pwm(10);
- // Animate_Cycle_Fade(10);
- // Animate_Cycle_Fade_Random(10);
- // Animate_Cycle_Fast(10);
- // Animate_Cycle_Fast_Random(100);
- // wait(3);
-
- // Set RTC time through the following steps:
- // 1. Build and flash firmware that sets time on boot
- // 2. Reset board (non-DFU) at programmed time to set RTC time
- // 3. Reset board in DFU mode
- // 4. Build and flash firmware that does not set time on boot
- // DS3231_SetTime(00, 13, 17);
- // DS3231_SetDate(SUNDAY, 1, 11, 20, 0);
- // Setup a ticker to refresh the display at 1kHz
- RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
- // Kick off the RNG timeout animation on startup
- int nextRngTimeout = DIGIT_RNG_REFRESH_INTERVAL + (rand() % DIGIT_RNG_REFRESH_VARIANCE);
- RngRefreshTimeout.attach(RngTickerCallback, nextRngTimeout);
- RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
- RngUpdateCallback();
- while(1) {
- // On every refresh tick, update the display with new values
- if (RefreshTick) {
- RefreshTick = false;
- ComputePwmOffsets();
- for (int i = 0; i < NUM_TUBES; i++) {
- for (int j = 0; j < NUM_DIGITS; j++) {
- if (Tubes[i].Digits[j].Updated) {
- PCA9685_SetDigitPwm(i, j, Tubes[i].Digits[j].PwmStart, Tubes[i].Digits[j].PwmEnd);
- Tubes[i].Digits[j].Updated = false;
- }
- }
- }
- if (Dot.Updated) {
- PCA9685_SetDotPwm(Dot.PwmStart, Dot.PwmEnd);
- 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);
- DS3231_GetDate(&day, &date, &month, &year);
- // Compensate for daylight savings time
- nextHour = IsDst(day, date, month, nextHour) ? (nextHour + 1) % 12 : nextHour % 12;
- if (nextHour == 0) nextHour = 12;
- // 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].RefreshActive = true;
- RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
- RngUpdateCallback();
- nextRngTimeout = DIGIT_RNG_REFRESH_INTERVAL + (rand() % DIGIT_RNG_REFRESH_VARIANCE);
- RngRefreshTimeout.attach(RngTickerCallback, nextRngTimeout);
- }
- }
- }
- 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);
- }
|