src/hello.c
author Tero Marttila <terom@paivola.fi>
Thu, 03 Apr 2014 22:24:46 +0300
changeset 54 ec42f36d8614
parent 53 dfe67409fbcd
child 55 04c625712e35
permissions -rw-r--r--
hello: interactive console
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#include <avr/io.h>
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
     2
52
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
     3
#include "stdlib.h"
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
     4
#include "timer.c" // XXX
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
     5
#include "serial.c" // XXX
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
     7
static enum state {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
     8
    START       = '\n',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
     9
    CMD_SET     = 's',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    10
    ARG_CHAR    = '0',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    11
    ERROR       = '!',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    12
} state = START, cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    13
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    14
static enum {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    15
    CMD_LED_1   = 0,
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    16
} cmd_led = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    17
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    18
static char arg_char;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    19
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    20
static unsigned char led_state = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    21
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    22
int command (enum state cmd)
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    23
{
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    24
    switch (cmd) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    25
        case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    26
            led_state = arg_char;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    27
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    28
            return 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    29
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    30
        default:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    31
            return 1;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    32
    }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    33
}
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    34
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    35
char input (char c)
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    36
{
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    37
    if (c == '\r') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    38
        char ret = '?';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    39
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    40
        // command state
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    41
        switch (state) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    42
            case START:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    43
                cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    44
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    45
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    46
            case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    47
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    48
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    49
            case ARG_CHAR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    50
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    51
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    52
            case ERROR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    53
                cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    54
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    55
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    56
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    57
        // command
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    58
        if (!cmd) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    59
            ret = ' ';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    60
        } else if (command(cmd)) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    61
            ret = '!';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    62
        } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    63
            ret = '\n';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    64
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    65
        
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    66
        // return to START with response
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    67
        state = START;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    68
        return ret;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    69
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    70
    } else if (31 < c && c < 128) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    71
        // process input char
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    72
        switch (state) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    73
            case START:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    74
                if (c == 's') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    75
                    cmd = CMD_SET;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    76
                    state = ARG_CHAR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    77
                    arg_char = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    78
                    return state;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    79
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    80
                } else if (c == 'S') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    81
                    cmd = state = CMD_SET;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    82
                    arg_char = 255;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    83
                    return state;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    84
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    85
            break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    86
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    87
            /* 
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    88
            case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    89
                if (c == '1') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    90
                    cmd_led = CMD_LED_1;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    91
                    state = ARG_CHAR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    92
                    arg_char = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    93
                } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    94
                    state = ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    95
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    96
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    97
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    98
            */
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    99
            case ARG_CHAR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   100
                if (c >= '0' && c <= '9') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   101
                    arg_char *= 10;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   102
                    arg_char += (c - '0');
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   103
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   104
                    return c;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   105
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   106
            break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   107
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   108
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   109
        // reject
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   110
        state = ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   111
        return ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   112
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   113
    } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   114
        // ignore
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   115
        return ' ';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   116
    }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   117
}
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   118
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   119
int main (void)
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   120
{
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   121
    timer_init();
52
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
   122
    serial_init();
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   123
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   124
    // led_init();
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   125
    sbi(&DDRB, DDB5);
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   126
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   127
    sei();
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   128
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   129
    // start
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   130
    char c;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   131
    unsigned short timeout = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   132
        
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   133
    serial_write(state);
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   134
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   135
    while (true) {
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   136
        // toggle
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   137
        if (led_state == 0) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   138
            timeout = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   139
            cbi(&PORTB, PORTB5);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   140
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   141
        } else if (led_state == 255) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   142
            timeout = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   143
            sbi(&PORTB, PORTB5);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   144
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   145
        } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   146
            // as Hz
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   147
            timeout = (16000 / led_state);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   148
            xbi(&PORTB, PORTB5);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   149
        }
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   150
        
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   151
        // sleep
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   152
        if (timer_sleep(timeout)) {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   153
            c = '.';
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   154
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   155
        } else if ((c = serial_read())) {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   156
            // got serial data
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   157
            c = input(c);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   158
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   159
        } else {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   160
            c = '?';
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   161
        }
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   162
        
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   163
        // output...
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   164
        serial_write(c);
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   165
    }
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   166
}