Changeset - a6d6bff160f8
[Not reviewed]
default
0 4 0
matthewreed - 8 years ago 2017-03-30 13:39:18

Minor changes
4 files changed with 9 insertions and 23 deletions:
0 comments (0 inline, 0 general)
inc/can.h
Show inline comments
 
#ifndef __CAN_H
 
#define __CAN_H
 
#ifndef _CAN_H_
 
#define _CAN_H_
 
 
#include <stdbool.h>
 
#include <string.h>
 
#include "stm32f0xx_hal.h"
 
 
#include "config.h"
 
#include "led.h"
 
#include "protocol.h"
 
 
extern CAN_HandleTypeDef can_handle;
 
 
void can_init(void);
 
void can_send_test(uint16_t id);
 
bool can_send(uint32_t id, uint32_t ide, uint8_t dlc, uint8_t data[8]);
 
void can_set_receive_mask(uint32_t mask);
 
void can_set_receive_id(uint32_t id);
 
bool can_silence_bus(bool value);
 
 
#endif //__CAN_H
 
#endif //_CAN_H_
inc/led.h
Show inline comments
 
/*
 
 * led.h
 
 *
 
 *  Created on: Mar 30, 2017
 
 *      Author: Matthew Reed
 
 */
 
 
#ifndef INC_LED_H_
 
#define INC_LED_H_
 
#ifndef _LED_H_
 
#define _LED_H_
 
 
#include "stm32f0xx_hal.h"
 
#include <stdbool.h>
 
#include <string.h>
 
 
typedef enum {
 
    LED_STATUS = 0,
 
    LED_CAN,
 
    LED_ERROR,
 
} led_name_t;
 
 
void led_init(void);
 
void led_start(led_name_t led);
 
void led_start_time(led_name_t led, uint16_t time);
 
void led_update(led_name_t led);
 
void led_update_all(void);
 
void led_set(led_name_t led, bool value);
 
void led_toggle(led_name_t led);
 
 
__weak bool gpio_set_led(led_name_t led, bool value);
 
__weak bool gpio_toggle_led(led_name_t led);
 
 
#endif /* INC_LED_H_ */
 
#endif /* _LED_H_ */
inc/protocol.h
Show inline comments
 
#ifndef _PROTOCOL_H
 
#define _PROTOCOL_H
 
#ifndef _PROTOCOL_H_
 
#define _PROTOCOL_H_
 
 
#include <stdbool.h>
 
 
#include "config.h"
 
#include "can.h"
 
 
typedef struct {
 
    bool command;
 
    uint8_t id;
 
    uint16_t key;
 
    uint8_t sensor;
 
    union {
 
    	float float_data;
 
		uint8_t byte_data[4];
 
    } data;
 
}  protocol_message_t;
 
 
typedef enum {
 
    ESTOP = 0x00,
 
    SILENCE_BUS = 0x01,
 
    SET_OUTPUT = 0X10,
 
    GET_DATA = 0x11,
 
    CONFIG = 0x12,
 
    CALIBRATE = 0x13,
 
} protocol_command_t;
 
 
typedef enum {
 
    NONE = 0x0000,
 
    DIGITAL_INPUT = 0x0001,
 
    FREQ_INPUT = 0x0002,
 
    ANALOG_INPUT = 0x0003,
 
    DIGITAL_OUTPUT = 0x0004,
 
    FREQ_OUTPUT = 0x0005,
 
    ANALOG_OUTPUT = 0x0006,
 
    AIR_TEMP = 0x0007,
 
    AIR_HUMIDITY = 0x0008,
 
    AIR_PRESSURE = 0x0009,
 
    AMBIENT_LIGHT = 0x000A,
 
    WATER_TEMP = 0x000B,
 
    WATER_LEVEL = 0x000C,
 
    WATER_CONDUCTIVITY = 0x000D,
 
    WATER_PH = 0x000E,
 
    CAN_ID = 0x0100,
 
    DATA_RATE = 0x0101,
 
    GPIO = 0x0102,
 
    LED_BRIGHTNESS = 0x0103,
 
} protocol_data_key_t;
 
 
 
bool protocol_receive_message(CanRxMsgTypeDef* can_message);
 
bool protocol_send_message(protocol_message_t* message);
 
bool protocol_process_message(protocol_message_t* message);
 
 
__weak bool protocol_estop(bool value);
 
__weak bool protocol_set_output(protocol_message_t* message);
 
__weak bool protocol_get_data(protocol_message_t* message);
 
__weak bool protocol_config(protocol_message_t* message);
 
 
#endif //_PROTOCOL_H
 
#endif //_PROTOCOL_H_
src/led.c
Show inline comments
 
/*
 
 * led.c
 
 *
 
 *  Created on: Mar 30, 2017
 
 *      Author: Matthew Reed
 
 */
 
 
#include "led.h"
 
 
#define LED_CYCLE_STATUS 2000
 
#define LED_CYCLE_CAN 0
 
#define LED_CYCLE_ERROR 0
 
 
uint16_t led_timers[3];
 
uint16_t led_thresholds[3];
 
uint16_t led_cycles[3];
 
 
void led_init(void)
 
{
 
    memset(led_timers, 0, sizeof led_timers);
 
    memset(led_thresholds, 0, sizeof led_thresholds);
 
    memset(led_cycles, 0, sizeof led_cycles);
 
 
    led_cycles[LED_STATUS] = LED_CYCLE_STATUS;
 
    led_cycles[LED_CAN] = LED_CYCLE_CAN;
 
    led_cycles[LED_ERROR] = LED_CYCLE_ERROR;
 
}
 
 
void led_start(led_name_t led)
 
{
 
    led_timers[led] = led_cycles[led];
 
    led_thresholds[led] = led_cycles[led]/2;
 
}
 
 
void led_start_time(led_name_t led, uint16_t time)
 
{
 
    if (led_timers[led] == 0)
 
    {
 
        led_timers[led] = time;
 
        led_thresholds[led] = time/2;
 
    }
 
}
 
 
void led_update(led_name_t led)
 
{
 
    if (led_timers[led] > led_thresholds[led])
 
    {
 
        led_set(led, 1);
 
        led_timers[led]--;
 
    }
 
    else if (led_timers[led] > 0)
 
    {
 
        led_set(led, 0);
 
        led_timers[led]--;
 
    }
 
    else {
 
        led_set(led, 0);
 
        led_timers[led] = led_cycles[led];
 
    }
 
}
 
 
void led_update_all(void)
 
{
 
    led_update(LED_STATUS);
 
    led_update(LED_CAN);
 
    led_update(LED_ERROR);
 
}
 
 
void led_set(led_name_t led, bool value)
 
{
 
    gpio_set_led(led, value);
 
}
 
 
void led_toggle(led_name_t led)
 
{
 
    gpio_toggle_led(led);
 
}
 
 
__weak bool gpio_set_led(led_name_t led, bool value)
 
{
 
    return false;
 
}
 
 
__weak bool gpio_toggle_led(led_name_t led)
 
{
 
    return false;
 
}
 
0 comments (0 inline, 0 general)