gpt4 book ai didi

c++ - 从 Node.js 运行 C 库

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:30 25 4
gpt4 key购买 nike

我完全不熟悉 C,但有一个小程序(与 RaspberryPi 上的硬件接口(interface))我希望能够从 Node.js 运行。根据我在 Node.js 文档中的了解,我可以通过将程序导出为 NODE_MODULE http://nodejs.org/api/addons.html 来运行 C++ 程序。

我一直在努力弄清楚 C 和 C++ 之间的区别,但我不确定我是否可以将要运行的代码导出为 C++ 文件(也许通过将文件扩展名更改为 .cc?)或者如果还有另一种在 node.js 中使用 C 代码的方法。

此外,我不明白我是否需要“构建”C 文件,或者我是否可以为 node.js 提供 .c 文件扩展名。

我不想使用 Node 的子进程运行 C 代码,尽管我知道这是可能的。正如 Node.js 文档所述,我更愿意将 C 代码导出为模块。

这是我希望在 node.js 中运行的代码

    //  How to access GPIO registers from C-code on the Raspberry-Pi
// Example program
// 15-January-2012
// Dom and Gert
//


// Access from ARM Running Linux

#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */

// WOULD I INCLUDE NODE.js HERE?? ##define BUILDING_NODE_EXTENSION
#include <node.h>

using namespace v8;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <unistd.h>

#define MAXTIMINGS 100

//#define DEBUG

#define DHT11 11
#define DHT22 22
#define AM2302 22

int readDHT(int type, int pin);

int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;

if (argc != 3) {
printf("usage: %s [11|22|2302] GPIOpin#\n", argv[0]);
printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]);
return 2;
}
int type = 0;
if (strcmp(argv[1], "11") == 0) type = DHT11;
if (strcmp(argv[1], "22") == 0) type = DHT22;
if (strcmp(argv[1], "2302") == 0) type = AM2302;
if (type == 0) {
printf("Select 11, 22, 2302 as type!\n");
return 3;
}

int dhtpin = atoi(argv[2]);

if (dhtpin <= 0) {
printf("Please select a valid GPIO pin #\n");
return 3;
}


printf("Using pin #%d\n", dhtpin);
readDHT(type, dhtpin);
return 0;

} // main


int bits[250], data[100];
int bitidx = 0;

int readDHT(int type, int pin) {
int counter = 0;
int laststate = HIGH;
int j=0;

// Set GPIO pin to output
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);

bcm2835_gpio_write(pin, HIGH);
usleep(500000); // 500 ms
bcm2835_gpio_write(pin, LOW);
usleep(20000);

bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);

data[0] = data[1] = data[2] = data[3] = data[4] = 0;

// wait for pin to drop?
while (bcm2835_gpio_lev(pin) == 1) {
usleep(1);
}

// read data!
for (int i=0; i< MAXTIMINGS; i++) {
counter = 0;
while ( bcm2835_gpio_lev(pin) == laststate) {
counter++;
//nanosleep(1); // overclocking might change this?
if (counter == 1000)
break;
}
laststate = bcm2835_gpio_lev(pin);
if (counter == 1000) break;
bits[bitidx++] = counter;

if ((i>3) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > 200)
data[j/8] |= 1;
j++;
}
}


#ifdef DEBUG
for (int i=3; i<bitidx; i+=2) {
printf("bit %d: %d\n", i-3, bits[i]);
printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200);
}
#endif

printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);

if ((j >= 39) &&
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
// yay!
if (type == DHT11)
printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]);
if (type == DHT22) {
float f, h;
h = data[0] * 256 + data[1];
h /= 10;

f = (data[2] & 0x7F)* 256 + data[3];
f /= 10.0;
if (data[2] & 0x80) f *= -1;
printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);
}
return 1;
}

return 0;
}

最佳答案

如果您是 C 语言的新手,那么从 javascript 领域完成这一切实际上可能不会那么令人头疼,而是使用 npm 上的几个模块之一与 GPIO(在 Pi 上)进行交互。一个这样的模块是 onoff模块。

关于c++ - 从 Node.js 运行 C 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com