summaryrefslogtreecommitdiff
path: root/irmpextlog.c
blob: 78537e9b92e411285b3b9bb1784dd98e3f05be67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * irmpextlog.c - external logging
 *
 * $Id: irmpextlog.c,v 1.1 2012/02/16 10:40:08 fm Exp $
 *
 * If you cannot use the internal UART logging routine, adapt the
 * source below for your application. The following implementation
 * is an example for a PIC 18F2550 with USB interface.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *---------------------------------------------------------------------------------------------------------------------------------------------------
 */
#include "irmpconfig.h"

#if IRMP_EXT_LOGGING == 1

#include "irmpextlog.h"
#include "system/usb/usb.h"

#define bit_set(var,bitnr)      ((var) |= 1 << (bitnr))         // set single bit in byte
#define loglen                  50                              // byte length for saving the data from irmp.c
                                                                // check your USB settings for max length of USB interface used for this FW!
static unsigned char            logdata[loglen];                // array for saveing the data from irmp.c
static unsigned char            logindex = 3;                   // start index of logdata
static char                     bitindex = 7;                   // bit position in current byte
static unsigned int             bitcount;

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * Initialize external logging
 *---------------------------------------------------------------------------------------------------------------------------------------------------
 */
void
initextlog (void)                                               // reset all data to default, only during init
{
    unsigned char i;
 
    for (i = 0; i < loglen; i++)
    {
        logdata[i] = 0;
    }

    bitcount = 0;
    logindex = 3;
    bitindex = 7;
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * Send logging data via device (e.g. USB on PIC)
 *---------------------------------------------------------------------------------------------------------------------------------------------------
 */
void
sendextlog (unsigned char data)
{
    if (logindex == loglen || data == '\n')                     // buffer full or \n  -->  send to application
    {
        if (mUSBUSARTIsTxTrfReady())                            // check USB, if ready send
        {
            logdata[0] = 0x01;                                  // indicator for logging, depends on your application
            logdata[1] = logindex;                              // save how much bytes are send from irmp.c
            logdata[2] = 0;                                     // set \n Index to 0;  0=buffer full;  0xA=\n received

            if (data == '\n')
            {
                logdata[2] = data;                              // \n --> save \n=0xA in to check in app that \n was received
            }

            mUSBUSARTTxRam((unsigned char *) &logdata, loglen); // send all Data to main Seoftware
      
            logindex = 3;                                       // reset index
            bitindex = 7;                                       // reset bit position
            logdata[logindex] = 0;                              // reset value of new logindex to 0
        }
    }
    else
    {
        bitcount++;

        if (data == '1')
        {
            bit_set(logdata[logindex], bitindex);               // all logdata[logindex] on start = 0 --> only 1 must be set
        }

        bitindex--;                                             // decrease bitposition, wirte from left to right

        if (bitindex == -1)                                     // byte complete Bit 7->0 --> next one
        {
            bitindex = 7;
            logindex++;

            if (logindex < loglen)
            {
                logdata[logindex] = 0;                          // set next byte to 0
            }
        }
    }
}

#endif //IRMP_EXT_LOGGING