src/hello.c
author Tero Marttila <terom@paivola.fi>
Sun, 20 Apr 2014 21:55:44 +0300
changeset 77 975a2dffdcda
parent 57 ee412c5be8b1
permissions -rw-r--r--
whitespacefix
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"
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
     4
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
     5
// XXX
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
     6
#include "timer.c"
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
     7
#include "serial.c"
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
     8
#include "spi.c"
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
     9
#include "led7.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
    10
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    11
static enum state {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    12
    START       = '\n',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    13
    CMD_SET     = 's',
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    14
    CMD_INC     = '+',
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    15
    CMD_DEC     = '-',
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    16
    CMD_READ    = 'r',
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    17
    ARG_CHAR    = '0',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    18
    ERROR       = '!',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    19
} state = START, cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    20
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    21
static enum {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    22
    CMD_LED_1   = 0,
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    23
} cmd_led = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    24
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    25
static char arg_char;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    26
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    27
static unsigned char led_state = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    28
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    29
int command (enum state cmd)
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    30
{
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    31
    switch (cmd) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    32
        case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    33
            led_state = arg_char;
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
            return 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    36
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    37
        case CMD_INC:
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    38
            if (led_state == 0xff)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    39
                return 1;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    40
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    41
            led_state++;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    42
            return 0;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    43
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    44
        case CMD_DEC:
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    45
            if (!led_state)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    46
                return 1;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    47
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    48
            led_state--;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    49
            return 0;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    50
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    51
        case CMD_READ:
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    52
            led_state = spi_rx[1]; // XXX
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    53
            return 0;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    54
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    55
        default:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    56
            return 1;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    57
    }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    58
}
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    59
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    60
char input (char c)
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    61
{
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    62
    if (c == '\r') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    63
        char ret = '?';
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
        // command state
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    66
        switch (state) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    67
            case START:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    68
                cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    69
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    70
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    71
            case CMD_SET:
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    72
            case CMD_INC:
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
    73
            case CMD_DEC:
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    74
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    75
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    76
            case ARG_CHAR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    77
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    78
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    79
            case ERROR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    80
                cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    81
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    82
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    83
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    84
        // command
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    85
        if (!cmd) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    86
            ret = ' ';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    87
        } else if (command(cmd)) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    88
            ret = '!';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    89
        } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    90
            ret = '\n';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    91
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    92
        
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    93
        // return to START with response
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    94
        state = START;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    95
        return ret;
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
    } else if (31 < c && c < 128) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    98
        // process input char
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    99
        switch (state) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   100
            case START:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   101
                if (c == 's') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   102
                    cmd = CMD_SET;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   103
                    state = ARG_CHAR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   104
                    arg_char = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   105
                } else if (c == 'S') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   106
                    cmd = state = CMD_SET;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   107
                    arg_char = 255;
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   108
                } else if (c == '+') {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   109
                    cmd = state = CMD_INC;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   110
                } else if (c == '-') {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   111
                    cmd = state = CMD_DEC;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   112
                } else if (c == 'r') {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   113
                    cmd = state = CMD_READ;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   114
                } else {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   115
                    break;
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   116
                }
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   117
                
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   118
                return state;
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   119
            break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   120
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   121
            /* 
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   122
            case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   123
                if (c == '1') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   124
                    cmd_led = CMD_LED_1;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   125
                    state = ARG_CHAR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   126
                    arg_char = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   127
                } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   128
                    state = ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   129
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   130
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   131
                break;
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
            case ARG_CHAR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   134
                if (c >= '0' && c <= '9') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   135
                    arg_char *= 10;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   136
                    arg_char += (c - '0');
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   137
                } else {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   138
                    break;
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   139
                }
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   140
                    
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   141
                return c;
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   142
            break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   143
        }
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
        // reject
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   146
        state = ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   147
        return ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   148
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   149
    } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   150
        // ignore
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   151
        return ' ';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   152
    }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   153
}
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   154
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   155
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   156
#define DEBUG_DDR   DDRB
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   157
#define DEBUG_PORT  PORTB
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   158
#define DEBUG_LED   0
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   159
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
   160
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
   161
{
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   162
    led7_init();
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   163
    timer_init();
52
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
   164
    serial_init();
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   165
    spi_init();
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   166
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   167
    // led_init();
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   168
    sbi(&DEBUG_DDR, DEBUG_LED);
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   169
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   170
    sei();
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   171
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   172
    // start
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   173
    char c;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   174
    unsigned short timeout = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   175
        
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   176
    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
   177
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
   178
    while (true) {
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   179
        // TODO: SPI timer
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents: 56
diff changeset
   180
        led7_update(led_state);
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   181
        spi_update();
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   182
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   183
        // toggle
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   184
        if (led_state == 0) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   185
            timeout = 0;
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   186
            cbi(&DEBUG_PORT, DEBUG_LED);
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   187
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   188
        } else if (led_state == 255) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   189
            timeout = 0;
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   190
            sbi(&DEBUG_PORT, DEBUG_LED);
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   191
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   192
        } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   193
            // as Hz
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   194
            timeout = (16000 / led_state);
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   195
            xbi(&DEBUG_PORT, DEBUG_LED);
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   196
        }
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
   197
        
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   198
        // sleep
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   199
        if (timer_sleep(timeout)) {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   200
            c = '.';
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   201
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   202
        } else if ((c = serial_read())) {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   203
            // got serial data
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   204
            c = input(c);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   205
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   206
        } else {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   207
            c = '?';
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   208
        }
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   209
        
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   210
        // output...
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   211
        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
   212
    }
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
   213
}