123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #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);
- bool RtcTick, RefreshTick, RngTick;
- typedef struct {
- char Value;
- ushort Reg;
- bool Active;
- bool Increment;
- } DIGIT;
- typedef struct {
- DIGIT Digits[MAX_FADE_DIGITS];
- DIGIT *NextDigit;
- DIGIT *PrevDigit;
- bool Updated;
- bool RefreshActive;
- } TUBE;
- TUBE Tube[NUM_TUBES] = {0};
- ushort DotValue = PCA9685_Min_Brightness;
- bool DotUpdated = false;
- bool DotIncrement = true;
- Ticker RuntimeUpdateTicker, RngUpdateTicker;
- int RuntimeTickerIter, RngTickerIter;
- void RtcTickCallback(void) {
- RtcTick = true;
- }
- void RefreshTickCallback(void) {
- RefreshTick = true;
- }
- void RngTickCallback(void) {
- RngTick = true;
- }
- void RuntimeTickerUpdate(void) {
- for (int i = 0; i < NUM_TUBES; i++) {
- if(!Tube[i].RefreshActive) {
- for (int j = 0; j < MAX_FADE_DIGITS; j++) {
- DIGIT *digit = &Tube[i].Digits[j];
- if (digit->Active) {
- if (digit->Increment && digit->Reg < PCA9685_Max_Brightness) {
- digit->Reg += FADE_TICK_STEP;
- } else if (digit->Reg > PCA9685_Min_Brightness) {
- digit->Reg -= FADE_TICK_STEP;
- }
- Tube[i].Updated = true;
- }
- }
- }
- }
- DotValue = DotIncrement ? DotValue + FADE_TICK_STEP : DotValue - FADE_TICK_STEP;
- DotUpdated = true;
- if (--RuntimeTickerIter == 0) {
- RuntimeUpdateTicker.detach();
- DotIncrement = !DotIncrement;
- }
- }
- void RngTickerUpdate(void) {
- if (--RngTickerIter == 0) {
- RngUpdateTicker.detach();
- }
- }
- int main() {
-
- for (int i = 0; i < NUM_TUBES; i++) {
- Tube[i].NextDigit = &Tube[i].Digits[0];
- }
- RtcTick = false;
- RefreshTick = false;
- RngTick = false;
- int nextSecond, nextMinute, nextHour;
- int prevMinute, prevHour;
- bool startup = true;
-
-
- i2c.frequency(400000);
-
-
- HV_EnableOutput(false);
-
- TUSB322_Init();
- PCA9685_Init();
- DS3231_Init(RtcTickCallback);
-
- HV_EnableOutput(true);
-
-
- PCA9685_SetVoltage(1.0);
-
-
-
-
-
-
- Ticker refreshTicker;
- refreshTicker.attach_us(RefreshTickCallback, DIGIT_REFRESH_RATE_US);
-
-
-
-
-
- while(1) {
-
-
-
-
-
-
-
-
-
- if (RefreshTick) {
- RefreshTick = false;
- for (int i = 0; i < NUM_TUBES; i++) {
- if (Tube[i].Updated) {
- Tube[i].Updated = false;
- for (int j = 0; j < MAX_FADE_DIGITS; j++) {
- DIGIT *digit = &Tube[i].Digits[j];
- if (digit->Active) {
- PCA9685_SetDigit(i, digit->Value, digit->Reg);
- if (digit->Reg == 0 && !digit->Increment) {
- digit->Active = false;
- }
- }
- }
- }
- }
- if (DotUpdated) {
- PCA9685_SetDot(DotValue);
- DotUpdated = false;
- }
- }
-
- if (RtcTick) {
- RtcTick = false;
-
- prevMinute = nextMinute;
- prevHour = nextHour;
- DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
-
- for (int i = 0; i < NUM_TUBES; i++) {
- bool Update = false;
- if (i == 0 && (startup || prevHour / 10 != nextHour / 10)) {
- if (nextHour / 10 != 0) {
- Tube[i].NextDigit->Value = nextHour / 10;
- Tube[i].NextDigit->Increment = true;
- Tube[i].NextDigit->Active = true;
- }
- Update = true;
- } else if (i == 1 && (startup || prevHour % 10 != nextHour % 10)) {
- Tube[i].NextDigit->Value = nextHour % 10;
- Tube[i].NextDigit->Increment = true;
- Tube[i].NextDigit->Active = true;
- Update = true;
- } else if (i == 2 && (startup || prevMinute / 10 != nextMinute / 10)) {
- Tube[i].NextDigit->Value = nextMinute / 10;
- Tube[i].NextDigit->Increment = true;
- Tube[i].NextDigit->Active = true;
- Update = true;
- } else if (i == 3 && (startup || prevMinute % 10 != nextMinute % 10)) {
- Tube[i].NextDigit->Value = nextMinute % 10;
- Tube[i].NextDigit->Increment = true;
- Tube[i].NextDigit->Active = true;
- Update = true;
- }
-
- if (Update) {
- if (!startup) {
- Tube[i].PrevDigit->Increment = false;
- }
- Tube[i].PrevDigit = Tube[i].NextDigit;
- Tube[i].NextDigit = Tube[i].NextDigit == &Tube[i].Digits[0] ?
- &Tube[i].Digits[1] : &Tube[i].Digits[0];
- }
- }
-
- startup = false;
-
- RuntimeTickerIter = FADE_TICKS;
- RuntimeUpdateTicker.attach(RuntimeTickerUpdate, (float)(DIGIT_REFRESH_RATE_US / 1000) / RuntimeTickerIter);
- }
- if (RngTick) {
-
- Tube[rand() % 4].RefreshActive = true;
- RngTickerIter = DIGIT_RNG_REFRESH_ITER * FADE_TICKS;
- RngUpdateTicker.attach(RngTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/FADE_TICKS);
- RngTick = false;
- }
- }
- }
- 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);
- }
|