Ignore:
Timestamp:
Jan 7, 2011, 4:49:07 AM (9 years ago)
Author:
sven
Message:

Anelex-Interface: Finalized the Builtin memory text printing (which actually was a lot of work...).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bull-anelex-project/trunk/anelex-interface/src/frontend.c

    r62 r63  
    3232#include "data.h"
    3333
    34 
    35 // das hier ist write_advance fuer den print_buffer (pbuf)
    36 // nicht ISR, sondern INTERRUPTABLE machen! also selbst unterbrechbar!
     34// weil wir kein math.h haben...
     35#define min(a,b) ((a)<(b) ? (a) : (b))
     36
     37/**
     38 * The USART recieve interrupt vector.
     39 * This function is the write_advance for the pbuf input ring buffer.
     40 * TODO: Not ISR but make INTERRUPTABLE! (selbst unterbrechbar!)
     41 **/
    3742ISR(USART0_RX_vect) {
    3843        char c = UDR0;
     
    8287}
    8388
    84 
    85 
     89/**
     90 * builtin_readline: Does the work when the builtin reading system
     91 * is active. Function is called by readline().
     92 **/
     93void builtin_readline() {
     94        // read text from builtin memory (data.c).
     95        //printf("Builtin system runnig, index=%d, roffset=%d\n", builtin_data.index, builtin_data.read_offset);
     96        // just a shorthand:
     97        PGM_P string = builtin_strings[builtin_data.index - 1];
     98        // search next newline char (returns NULL if not found):
     99        PGM_P nlpos = strchr_P(string + builtin_data.read_offset, '\n');
     100        if(nlpos == NULL) {
     101                //puts("Reached end of string! (No more newline)");
     102                builtin_data.index = 0;
     103                builtin_data.read_offset = 0;
     104                return;
     105        }
     106
     107        uint8_t nloffset = min(nlpos - string - builtin_data.read_offset, LINE_LENGTH);
     108        //printf("Detected NL at offset %d. Currrent Line:\n", nloffset);
     109        strncpy_P(current_line, string + builtin_data.read_offset, nloffset);
     110        // RS232 feedback: Print text out (like readline() gives feedback)
     111        puts(current_line);
     112
     113        builtin_data.read_offset += nloffset + 1; // one behind the newline
     114        // important: read offset counts from 0, strlen from 1.
     115        if(builtin_data.read_offset + 1 >= strlen_P(string)) {
     116                //puts("Reached end of string!");
     117                builtin_data.index = 0;
     118                builtin_data.read_offset = 0;
     119        }
     120}
     121
     122/**
     123 * The readline function reads one line from RS232 and puts it into the driver
     124 * output text buffer current_line[] (anelex.h). The real work is done asynchronously
     125 * by the USART RX ISR. Alternatively the next line from the builtin program memory text
     126 * is copied to current_line[] by builtin_readline().
     127 *
     128 * This function is called by main().
     129 **/
    86130void readline() {
    87131        memset(&current_line, 0, LINE_LENGTH); // null the output buffer (important)
    88132
     133        printf("Readline!\n");
     134        // check if we get the text from ROM
    89135        if(builtin_data.index) {
    90                 // read text from builtin memory (data.c).
    91                 PGM_P string = builtin_strings[builtin_data.index - 1];
    92 
    93                 int nlpos = strchr_P(string[builtin_data.read_offset], '\n');
    94                 if(nlpos > LINE_LENGTH) nlpos = LINE_LENGTH;
    95                 strncpy_P(current_line, string[builtin_data.read_offset], nlpos - builtin_data.read_offset);
    96 
    97                 builtin_data.read_offset += nlpos + 1; // one behind the newline
    98                 if(builtin_data.read_offset >= strlen_P(string)) {
    99                         puts("Reached end of string!");
    100                         builtin_data.index = 0;
    101                         builtin_data.read_offset = 0;
    102                 }
     136                builtin_readline();
    103137                return;
    104138        }
     
    163197
    164198
     199/**
     200 * Parses the current_line[] and executes a command. Expects current_line[] to be
     201 * a command, which is checked in the main() loop.
     202 * Function is called by main().
     203 **/
    165204void exec_command() {
    166205        char* command = current_line + 1; // strip leading '?'
     
    189228                        return; // read new line, all work is done in readline()
    190229                }
     230        } else if(strcasecmp(command, "ping") == 0) {
     231                puts("Pong :-)");
    191232        } else {
    192233                puts("! Command not understood. Please type ?help");
     
    194235} // exec_command
    195236
     237/**
     238 * USART TX function for avr-libc stdio.h toolchain. This function
     239 * prints out exactly one character and converts on the fly NL to
     240 * CR NL. See uart_transmit() for the function without conversion.
     241 **/
    196242int stdout_transmit(char c, FILE *stream) {
    197243        if (c == '\n')
     
    201247}
    202248
     249/**
     250 * UART initialisation, called at main() start. In this program we also
     251 * set up the RX interrupt enable flag.
     252 **/
    203253void uart_init(uint32_t Baud) {
    204254    int BaudRate = F_CPU / (16 * Baud) - 1;
     
    222272}
    223273
    224 
     274/**
     275 * Helper function to print out a byte as binary representation to STDOUT.
     276 **/
    225277void print_bits(byte_t byte) {
    226278        char str[10];
Note: See TracChangeset for help on using the changeset viewer.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License