lukedukeus / esp32-bacnet-master-v5.01

9 stars 8 forks source link

gpio definitions #1

Closed crappyrules closed 9 months ago

crappyrules commented 1 year ago

I can't seem to find where in the code where the gpio pins are assigned to the inputs and outputs. could you point me in the direction of which of the gpio would be for the binary_outputs?

lukedukeus commented 1 year ago

It doesn't assign any GPIO to inputs/ ouputs. It starts up a bacnet server with a device description, saying how many binary inputs, binary outputs, etc. there are, but they arent linked to any GPIO pins. You can write your own code to interface withe IO, then call Binary_Input_Present_Value_Set(binary_input_num, binary_input_value); to set a value, or Binary_Input_Present_Value(binary_input_num) to read a value.

The first arg to Device_Init(&Object_Table[0]); is the object table, which defines the number of binary inputs/ outputs, etc. This sample might make it make sense:

server.c:

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>

#include "config.h"
#include "address.h"
#include "bacdef.h"
#include "handlers.h"
#include "client.h"
#include "dlenv.h"
#include "bacdcode.h"
#include "npdu.h"
#include "apdu.h"
#include "iam.h"
#include "tsm.h"
#include "datalink.h"
#include "dcc.h"
#include "getevent.h"
#include "net.h"
#include "txbuf.h"
#include "version.h"
/* include the device object */
#include "device.h"
#include "bi.h"
#include "bo.h"
#include "bv.h"

#include "server.h"
#include "relay.h"
#include "esp_log.h"

#define TAG "SERVER"
#define OBJECT_ID 200005

// Buffer used for receiving
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };

// definition of what functions we have
static object_functions_t Object_Table[] = {
    {OBJECT_DEVICE,
            NULL /* Init - don't init Device or it will recourse! */ ,
            Device_Count,
            Device_Index_To_Instance,
            Device_Valid_Object_Instance_Number,
            Device_Object_Name,
            Device_Read_Property_Local,
            Device_Write_Property_Local,
            Device_Property_Lists,
            DeviceGetRRInfo,
            NULL /* Iterator */ ,
            NULL /* Value_Lists */ ,
            NULL /* COV */ ,
            NULL /* COV Clear */ ,
        NULL /* Intrinsic Reporting */ },
   {OBJECT_BINARY_INPUT,
            Binary_Input_Init,
            Binary_Input_Count,
            Binary_Input_Index_To_Instance,
            Binary_Input_Valid_Instance,
            Binary_Input_Object_Name,
            Binary_Input_Read_Property,
            Binary_Input_Write_Property,
            Binary_Input_Property_Lists,
            NULL /* ReadRangeInfo */ ,
            NULL /* Iterator */ ,
            Binary_Input_Encode_Value_List,
            Binary_Input_Change_Of_Value,
            Binary_Input_Change_Of_Value_Clear,
        NULL /* Intrinsic Reporting */ },
};

static void Init_Service_Handlers(void)
{
    Device_Init(&Object_Table[0]);
    /* we need to handle who-is to support dynamic device binding */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has);
    /* handle i-am to support binding to other devices */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind);
    /* set the handler for all the services we don't implement */
    /* It is required to send the proper reject message... */
    apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service);
    /* Set the handlers for any confirmed services that we support. */
    /* We must implement read property - it's required! */
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROP_MULTIPLE, handler_write_property_multiple);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_RANGE, handler_read_range);
#if defined(BACFILE)
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE, handler_atomic_read_file);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_WRITE_FILE, handler_atomic_write_file);
#endif
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device);
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, handler_timesync_utc);
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, handler_timesync);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_SUBSCRIBE_COV, handler_cov_subscribe);
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_COV_NOTIFICATION, handler_ucov_notification);
    /* handle communication so we can shutup when asked */
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
        handler_device_communication_control);
    /* handle the data coming back from private requests */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_PRIVATE_TRANSFER, handler_unconfirmed_private_transfer);
#if defined(INTRINSIC_REPORTING)
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM, handler_alarm_ack);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_GET_EVENT_INFORMATION, handler_get_event_information);
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_GET_ALARM_SUMMARY, handler_get_alarm_summary);
#endif /* defined(INTRINSIC_REPORTING) */
}

void task_server(void *pvParam)
{
    BACNET_ADDRESS src = {0};  /* address where message came from */
    uint16_t pdu_len = 0;

    /* allow the device ID to be set */
    Device_Set_Object_Instance_Number(OBJECT_ID);

    ESP_LOGI(TAG, "BACnet Stack Version %s", BACnet_Version);
    ESP_LOGI(TAG, "BACnet Device ID: %lu", Device_Object_Instance_Number());
    ESP_LOGI(TAG, "Max APDU: %d", MAX_APDU);

    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);

    uint32_t tickcount = xTaskGetTickCount();

    while (1) {
        vTaskDelay(10 / portTICK_PERIOD_MS); 
        uint32_t newtick = xTaskGetTickCount();

            if ((newtick < tickcount) ||
                ((newtick - tickcount) >= configTICK_RATE_HZ)) {
                tickcount = newtick;
                dcc_timer_seconds(1);
                bvlc_maintenance_timer(1);
                handler_cov_timer_seconds(1);
                tsm_timer_milliseconds(1000);

                // Read analog values from internal sensors
                Binary_Input_Present_Value_Set(0, relay_status());
            }

        pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, 1000);

        if (pdu_len) {
            npdu_handler(&src, &Rx_Buf[0], pdu_len);

            if (Binary_Input_Present_Value(0) == BINARY_ACTIVE)
            {
                relay_on();
            }
            else
            {
                relay_off();
            }
        }

        handler_cov_task();

    }
}

relay.c:

#include <stdio.h>
#include "driver/gpio.h"
#include "esp_log.h"
#include "relay.h"
#include "led.h"

#define TAG "RELAY"
#define RELAY_ON_GPIO 5
#define RELAY_OFF_GPIO 6

static int is_relay_on = 0;

void relay_initialize(void)
{
    ESP_LOGI(TAG, "Relay initialize");

    gpio_reset_pin(RELAY_OFF_GPIO);
    gpio_set_direction(RELAY_OFF_GPIO, GPIO_MODE_OUTPUT);
    gpio_set_level(RELAY_OFF_GPIO, 0);

    gpio_reset_pin(RELAY_ON_GPIO);
    gpio_set_direction(RELAY_ON_GPIO, GPIO_MODE_OUTPUT);
    gpio_set_level(RELAY_ON_GPIO, 0);
}

void relay_on(void)
{
    // set the off pin low
    gpio_set_level(RELAY_OFF_GPIO, 0);

    // set the on pin high
    gpio_set_level(RELAY_ON_GPIO, 1);

    is_relay_on = 1;

    led_on();
}

void relay_off(void)
{
    ESP_LOGI(TAG, "Relay off");

    // set the on pin low
    gpio_set_level(RELAY_ON_GPIO, 0);

    // set the off pin high
    gpio_set_level(RELAY_OFF_GPIO, 1);

    is_relay_on = 0;

    led_off();
}

void relay_toggle(void)
{
    ESP_LOGI(TAG, "Relay toggle");

    if (is_relay_on == 1)
    {
        relay_off();
    }
    else if (is_relay_on == 0)
    {
        relay_on();
    }
}

int relay_status(void)
{
    return is_relay_on;
}