Changeset - e0a38751daed
[Not reviewed]
default
0 3 0
Ethan Zonca - 10 years ago 2014-08-23 23:01:14
ez@ethanzonca.com
Display moved to states, refactor test to displaylogo
3 files changed with 88 insertions and 45 deletions:
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -46,12 +46,62 @@ char* itoa(int16_t i, char b[]){
 
        *--p = digit[i%10];
 
        i = i/10;
 
    }while(i);
 
    return b;
 
}
 
 
char* itoa_fp(int16_t i, uint8_t frac, char b[]){
 
    char const digit[] = "0123456789";
 
 
    // set p to beginning of char array
 
    char* p = b;
 
 
    // If negative, set current char to '-' and inc, unnegate number
 
    if(i<0){
 
        *p++ = '-';
 
        i *= -1;
 
    }
 
 
    // Init shifter to numeric value
 
    uint16_t shifter = i;
 
    uint16_t frac_shifter = frac;
 
 
    // Iterate through 10s places, incrementing text pointer as we go
 
    do{ 
 
        ++p;
 
        shifter = shifter/10;
 
    }while(shifter);
 
    
 
    ++p; // increment for decimal point
 
 
    do{
 
        ++p;
 
        frac_shifter = frac_shifter/10;
 
    }while(frac_shifter);
 
        
 
 
    // Null-terminate the string
 
    *p = '\0';
 
 
    // Go backwards and write out fractional digits 
 
    do{ 
 
        *--p = digit[frac%10];
 
        frac = frac/10;
 
    }while(frac);
 
 
    *--p = '.'; // insert decimal point
 
 
    // Go backwards and write out remaining digits 
 
    do{ 
 
        *--p = digit[i%10];
 
        i = i/10;
 
    }while(i);
 
    return b;
 
}
 
 
 
static __IO uint32_t TimingDelay;
 
 
// Move to header file
 
void init_gpio();
 
void init_spi();
 
void process();
 
@@ -86,15 +136,13 @@ int main(void)
 
 
    init_spi();
 
 
    ssd1306_Init();
 
    ssd1306_block_write();
 
    ssd1306_DrawString("therm 0.1", 0, 40);
 
    ssd1306_DrawString("Status: Idle", 2, 40);
 
  //  ssd1306_block_write();
 
    ssd1306_test();
 
 
 
    uint8_t toggle = 0;
 
 
    int16_t temp = -231;
 
 
    while(1)
 
@@ -106,47 +154,31 @@ int main(void)
 
 
        // Run state machine [TODO: 50hz?]
 
        machine(); 
 
        // probably just passed the actual port
 
 
        // TODO: Grab buttonpresses with interrupts
 
        uint8_t sw_btn = GPIO_ReadInputDataBit(SW_BTN);
 
        //uint8_t sw_btn = GPIO_ReadInputDataBit(SW_BTN);
 
        //uint8_t sw_up = GPIO_ReadInputDataBit(SW_UP);
 
        //uint8_t sw_down = GPIO_ReadInputDataBit(SW_DOWN);
 
        //uint8_t sw_left = GPIO_ReadInputDataBit(SW_LEFT);
 
        //uint8_t sw_right = GPIO_ReadInputDataBit(SW_RIGHT);
 
 
        if(!sw_btn) {
 
            GPIO_ToggleBits(LED_STAT);
 
            if(!toggle) {
 
                GPIO_SetBits(GPIOB, GPIO_Pin_13);
 
                toggle = ! toggle;
 
            }
 
            else  {
 
                GPIO_ResetBits(GPIOB, GPIO_Pin_13);
 
                toggle = ! toggle;
 
            }    
 
        }
 
 
        GPIO_SetBits(LED_POWER);
 
        Delay(50);
 
        GPIO_ResetBits(LED_POWER);
 
        Delay(50);
 
    }
 
}
 
 
int32_t temp = 0;
 
int32_t setpoint = 0;
 
int32_t p = 1;
 
int32_t i = 1;
 
int32_t d = 1;
 
 
 
// Process things
 
void process()
 
{
 
// Read temperature and update global temp vars
 
int16_t temp = 0;
 
uint8_t temp_frac = 0;
 
void update_temp() {
 
    // Assert CS
 
    GPIO_ResetBits(MAX_CS);
 
    Delay(1);
 
 
    // This may not clock at all... might need to send 16 bits first
 
    SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data
 
@@ -156,51 +188,49 @@ void process()
 
    if(temp_pre & 0b0000000000000010) {
 
        ssd1306_DrawString("Fatal Error", 3, 35);
 
    }
 
    else if(temp_pre & 0b0000000000000001) {
 
        ssd1306_DrawString("TC Fault", 3, 35);
 
    }
 
    else {
 
        ssd1306_DrawString("TempSense OK", 3, 35);
 
    }
 
 
    uint8_t sign = temp >> 15;// top bit is sign
 
 
 
    temp_pre = temp_pre >> 2; // Drop 2 lowest bits
 
    uint8_t frac = temp_pre & 0b11; // get fractional part
 
    frac *= 25; // each bit is .25 a degree, up to fixed point
 
    temp_frac = temp_pre & 0b11; // get fractional part
 
    temp_frac *= 25; // each bit is .25 a degree, up to fixed point
 
    temp_pre = temp_pre >> 2; // Drop 2 fractional bits 
 
 
    int16_t temp = 0;
 
    if(sign) {
 
        temp = -temp_pre;
 
    }
 
    else {
 
        temp = temp_pre;
 
    }
 
 
    // Deassert CS
 
    Delay(1);
 
    GPIO_SetBits(MAX_CS);
 
}
 
 
    if(temp > 0) {
 
        GPIO_SetBits(LED_STAT);
 
    }
 
    char tempstr[9];
 
    itoa(temp_pre, tempstr);
 
    ssd1306_DrawString("Temp: ", 1, 40);
 
    ssd1306_DrawString("    ", 1, 70);
 
    ssd1306_DrawString(tempstr, 1, 70);
 
    itoa(frac, tempstr);
 
    ssd1306_DrawString("  ", 1, 90);
 
    ssd1306_DrawString(tempstr, 1, 90);
 
 
int32_t setpoint = 0;
 
int32_t p = 1;
 
int32_t i = 1;
 
int32_t d = 1;
 
 
 
// Process things
 
void process()
 
{
 
    update_temp(); // Read MAX31855
 
 
    // TODO: Add calibration offset (linear)
 
 
    // Perform PID calculations
 
    //if(
 
    GPIO_SetBits(LED_STAT);
 
 
    // Write output to SSR
 
}
 
 
 
 
@@ -219,22 +249,30 @@ enum state {
 
 
uint8_t state = STATE_IDLE;
 
 
// State machine
 
void machine()
 
{
 
    
 
    uint8_t last_state = state;
 
 
    switch(state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
            ssd1306_DrawString("therm :: idle ", 0, 40);
 
 
            ssd1306_drawlogo();
 
            char tempstr[9];
 
            itoa_fp(temp, temp_frac, tempstr);
 
            ssd1306_DrawString("Temp: ", 2, 40);
 
            ssd1306_DrawString("    ", 2, 70);
 
            ssd1306_DrawString(tempstr, 2, 72);
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETP;
 
            }
 
 
            // Event Handler
 
@@ -373,12 +411,17 @@ void machine()
 
            ssd1306_DrawString("therm :: BAD BAD", 0, 40);
 
            state = STATE_IDLE;
 
 
        } break;
 
            
 
    }
 
 
    if(last_state != state) {
 
        // Clear screen on state change
 
        ssd1306_block_write();
 
    }
 
}
 
 
 
/**
 
  * @brief  Inserts a delay time.
 
  * @param  nTime: specifies the delay time length, in 1 ms.
ssd1306.c
Show inline comments
 
@@ -649,13 +649,13 @@ const uint8_t row[4][32] = {
 
 
 
 
 
};
 
 
void ssd1306_test()
 
void ssd1306_drawlogo()
 
{
 
    uint8_t i = 0;
 
    setStartPage(3);
 
    setStartColumn(0);
 
    for(i = 0; i<32; i++)
 
    {
ssd1306.h
Show inline comments
 
@@ -47,9 +47,9 @@ unsigned char ssd1306_SetContrast(unsign
 
unsigned char ssd1306_GetContrast();
 
unsigned char* ssd1306_GetBuffer();
 
unsigned long ssd1306_DrawPoint(Pos_t x, Pos_t y, Color_t color);
 
void ssd1306_block_write(void);
 
void ssd1306_DrawChar(char ascii, unsigned char row, unsigned char xPos);
 
void ssd1306_DrawString(const char *dataPtr, unsigned char row, unsigned char xPos);
 
void ssd1306_test();
 
void ssd1306_drawlogo();
 
 
#endif
0 comments (0 inline, 0 general)