Skip to content

Commit

Permalink
Addea fancy LEDs (analog pins A3-A5) for status, CAN activity and err…
Browse files Browse the repository at this point in the history
…or situations.

By default the filter does not pass anything except Volvo diagnostic messages. Uncomment #define PASS_ALL_MSGS to change that
Added possibility to send keepalive CAN msgs (disabled by default).
More verbuse debug messaging added
More robust error handling
  • Loading branch information
hackingvolvo committed Jan 25, 2015
1 parent 4e4ff3d commit 2663511
Show file tree
Hide file tree
Showing 8 changed files with 544 additions and 30 deletions.
134 changes: 134 additions & 0 deletions led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* Sardine CAN (Open Source J2534 device) - Arduino firmware - version 0.4 alpha
**
** Copyright (C) 2012 Olaf @ Hacking Volvo blog (hackingvolvo.blogspot.com)
** Author: Olaf <[email protected]>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation, either version 3 of the License, or (at
** your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, <http://www.gnu.org/licenses/>.
**
*/
#include <Arduino.h>
#include "led.h"

void SetLED( led * targetLED, unsigned int duration )
{
#ifdef ENABLE_LEDS
// printf("SetLED (%d): dur %d\n",targetLED->pin, duration);
targetLED->mode = LED_SINGLE_BLINK;
targetLED->nextSwitch = millis() + duration;
targetLED->duration = duration;
targetLED->enabled = targetLED->pwr = true;
// digitalWrite( targetLED->pin, HIGH );
analogWrite( targetLED->pin, 128); // PWM
#endif
}

void SetBlinkLED( led * targetLED, unsigned int duration, unsigned int interval )
{
#ifdef ENABLE_LEDS
// printf("SetBlinkLED (%d): dur %d, interval %d\n",targetLED->pin, duration, interval);
targetLED->mode = LED_BLINK_LOOP;
// if (duration>0)
targetLED->nextSwitch = millis() + duration;
// else
// targetLED->nextSwitch = 0;
targetLED->duration = duration;
targetLED->interval = targetLED->multipleInterval = interval;
targetLED->enabled = targetLED->pwr = true;
targetLED->blinkCount = 1;
targetLED->blinkIndex = 0;
// digitalWrite( targetLED->pin, HIGH );
analogWrite( targetLED->pin, 128); // PWM
#endif
}

void SetMultipleBlinkLED( led * targetLED, unsigned int blinkCount, unsigned int duration, unsigned multipleInterval, unsigned int seriesInterval )
{
#ifdef ENABLE_LEDS
// printf("SetMultipleBlinkLED (%d): blinkcount %d, dur %d, mul_interval %d, series_interval %d\n",targetLED->pin, blinkCount, duration, multipleInterval, seriesInterval );
targetLED->mode = LED_BLINK_LOOP;
targetLED->nextSwitch = millis() + duration;
targetLED->duration = duration;
targetLED->interval = seriesInterval;
targetLED->multipleInterval = multipleInterval;
targetLED->enabled = targetLED->pwr = true;
targetLED->blinkCount = blinkCount;
targetLED->blinkIndex = 0;
// digitalWrite( targetLED->pin, HIGH );
analogWrite( targetLED->pin, 128); // PWM
#endif
}

void ClearLED( led * targetLED )
{
#ifdef ENABLE_LEDS
targetLED->enabled = false;
targetLED->pwr = false;
#endif
}


void HandleLED( led * targetLED )
{
#ifdef ENABLE_LEDS
unsigned long currTime = millis();
if (targetLED->enabled)
{
switch (targetLED->mode)
{
case LED_SINGLE_BLINK:
{
// duration == 0 -> led stays on indefinetely
if ( (targetLED->duration != 0) && (currTime > targetLED->nextSwitch) )
{
targetLED->enabled = false;
digitalWrite( targetLED->pin, LOW );
}
}
break;
case LED_BLINK_LOOP:
{
if (currTime > targetLED->nextSwitch)
{

targetLED->pwr = !targetLED->pwr;

if (targetLED->pwr)
{
// digitalWrite( targetLED->pin, HIGH );
analogWrite( targetLED->pin, 128); // PWM
targetLED->nextSwitch = currTime + targetLED->duration;
}
else
{
digitalWrite( targetLED->pin, LOW );
targetLED->blinkIndex++;
if (targetLED->blinkIndex >= targetLED->blinkCount)
{
targetLED->blinkIndex = 0;
targetLED->nextSwitch = currTime + targetLED->interval;
} else
{
targetLED->nextSwitch = currTime + targetLED->multipleInterval;
}
}
}
}
break;
default:
break;
}
}
#endif
}

51 changes: 51 additions & 0 deletions led.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Sardine CAN (Open Source J2534 device) - Arduino firmware - version 0.4 alpha
**
** Copyright (C) 2012 Olaf @ Hacking Volvo blog (hackingvolvo.blogspot.com)
** Author: Olaf <[email protected]>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation, either version 3 of the License, or (at
** your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, <http://www.gnu.org/licenses/>.
**
*/
#ifndef _SARDINE_LED_H
#define _SARDINE_LED_H

#define ENABLE_LEDS
#define LED_PIN_STATUS A5
#define LED_PIN_CAN A4
#define LED_PIN_ERROR A3

typedef struct {
bool enabled;
bool pwr; // is led now lit or off
uint8_t mode;
unsigned long nextSwitch;
unsigned int duration; // duration of one/multiple blinks
unsigned int multipleInterval; // duration between multiple blinks
unsigned int interval; // duration between blink (or series of blinks)
uint8_t blinkCount;
uint8_t blinkIndex;
unsigned int pin;
} led;

#define LED_SINGLE_BLINK 0
#define LED_BLINK_LOOP 1

void SetLED( led * targetLED, unsigned int duration );
void SetBlinkLED( led * targetLED, unsigned int duration, unsigned int interval );
void SetMultipleBlinkLED( led * targetLED, unsigned int blinkCount, unsigned int duration, unsigned multipleInterval, unsigned int seriesInterval );
void ClearLED( led * targetLED );
void HandleLED( led * targetLED );

#endif

22 changes: 21 additions & 1 deletion sardine.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Sardine CAN (Open Source J2534 device) - Arduino firmware - version 0.3 alpha
/* Sardine CAN (Open Source J2534 device) - Arduino firmware - version 0.4 alpha
**
** Copyright (C) 2012 Olaf @ Hacking Volvo blog (hackingvolvo.blogspot.com)
** Author: Olaf <[email protected]>
Expand Down Expand Up @@ -27,6 +27,24 @@
#define MODE_LISTEN 3
#define MODE_CONFIG 4

#define HW_VER 0x01 // hardware version
#define SW_VER 0x00 // software version
#define SW_VER_MAJOR 0x00 // software major version
#define SW_VER_MINOR 0x04 // software minor version

#define STATUS_INIT 0
#define STATUS_CONFIG 1
#define STATUS_READY 2
#define STATUS_UNRECOVERABLE_ERROR 3

#define ERRSTATUS_NONE 0
#define ERRSTATUS_OUT_OF_MEMORY 1
#define ERRSTATUS_CAN_INIT_ERROR 2
#define ERRSTATUS_CAN_TX_BUFFER_OVERFLOW 4
#define ERRSTATUS_CAN_RX_BUFFER_OVERFLOW 8

#define MCP2551_STANDBY_PIN A1

#define send_to_host(...) {\
printf("{"); \
printf(__VA_ARGS__); \
Expand All @@ -38,6 +56,8 @@ int convert_string_to_int( char * src, unsigned long * dest, int byteCount );
int convert_string_to_int( char * src, unsigned long * dest );
int convert_ascii_to_nibble(char c);

void SetErrorStatus( unsigned int errStatus );


int send_CAN_msg(tCAN * msg);
void set_keepalive_timeout( unsigned long timeout );
Expand Down
Loading

0 comments on commit 2663511

Please sign in to comment.