Giter Site home page Giter Site logo

libgpiod-frequency-counter's Introduction

libgpiod-frequency-counter

license

Overview

GPIO input frequency counter.

Requirements

Installation

sudo apt-get install debhelper build-essential libgpiod-dev python3-dev
git clone https://github.com/dead-beef/libgpiod-frequency-counter.git
cd libgpiod-frequency-counter
make pkg
sudo dpkg -i ../libgpiod-frequency-counter_0.4.0-1_*.deb ../libgpiod-frequency-counter-dev_0.4.0-1_*.deb ../gpiod-frequency_0.4.0-1_*.deb ../python3-libgpiod-frequency-counter_0.4.0-1_*.deb

Usage

CLI

> gpio-get-frequency -h
Usage: gpio-get-frequency [-h] [-i <time>] [-b <size>] <chip name/number> <offset>

Options:
    -h, --help               print this help text and exit
    -i, --interval <time>    maximum time in seconds (default: none)
    -b, --buf-size <size>    period buffer size (default: 32)

C

#include <stdio.h>
#include <time.h>
#include <gpiod.h>
#include <gpiod_frequency_counter.h>

int main() {
    enum {
        INPUT_PIN = 4,
        BUF_SIZE = 32
    };

    struct gpiod_chip *chip = NULL;
    struct gpiod_line *input_line = NULL;
    gpiod_frequency_counter counter;
    struct timespec interval;

    interval.tv_sec = 0;
    interval.tv_nsec = 500000000;

    chip = gpiod_chip_open("/dev/gpiochip0");
    input_line = gpiod_chip_get_line(chip, INPUT_PIN);

    gpiod_frequency_counter_init(&counter, input_line, BUF_SIZE);

    while (1) {
        gpiod_frequency_counter_count(&counter, BUF_SIZE, &interval);
        double period = gpiod_frequency_counter_get_period(&counter);
        double frequency = gpiod_frequency_counter_get_frequency(&counter);
        double duty_cycle = gpiod_frequency_counter_get_duty_cycle(&counter);

        fputs("\x1b[2K\r", stdout);
        printf(
            "period=%.06lfs frequency=%.06lfHz duty_cycle=%.02lf ",
            period,
            frequency,
            duty_cycle
        );
        fflush(stdout);
    }

    gpiod_frequency_counter_destroy(&counter);
    gpiod_chip_close(chip);
    return 0;
}

Python

#!/usr/bin/env python3

import sys

from gpiod import Chip
from gpiod_frequency_counter import FrequencyCounter

chip = 'gpiochip0'
line = 4

with Chip(chip, Chip.OPEN_BY_NAME) as chip:
    line = chip.get_line(line)
    buf_size = 32
    timeout_sec = 1
    timeout_nsec = 0
    counter = FrequencyCounter(line, buf_size)
    try:
        while True:
            counter.count(buf_size, timeout_sec, timeout_nsec)
            print(
                '\x1b[2K\r'
                f'period={counter.period:.04f}s '
                f'frequency={counter.frequency:.04f}Hz '
                f'duty_cycle={counter.duty_cycle:.02f} ',
                end=''
            )
            sys.stdout.flush()
    except KeyboardInterrupt:
        pass

Licenses

libgpiod-frequency-counter's People

Contributors

dead-beef avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.