经过我自己的学习努力,我终于能够通过电子控制orangepi lite的gpio,但是我在尝试使用按钮时遇到问题,像fs.watch这样的线路在所有重新读取值时都不起作用或者也许我的回调函数不起作用。我需要一些建议和指导
我也尝试使用 setInterval 但这导致应用程序停止工作,按钮仍然可以与 PinRead 功能正常工作,但我无法连续使用它
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('node:path')
const { Gpio } = require('./user_modules/userGpio');
const led = new Gpio(10, 'out');
const button = new Gpio(20, 'in');
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 350,
height: 320,
webPreferences: {
// NodeIntegration : true,
// contextIsolation: false,
preload: path.join(__dirname, 'preload.js')
},
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
//button on html
ipcMain.on('HTML_BTN', (_, msg) => {
console.log(msg);
led.PinWrite(led.PinRead() ^ 1);
console.log('pin20 are :' + button.PinRead() + '\n');
})
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
//---------------------------------------------------
// ctrl +c
process.on('SIGINT', _ => {
//user code
led.UnExportGpio();
button.UnExportGpio();
});
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
//USER FUNCTION
//button on hardware
button.PinWatch((value) => {
console.log('Button state changed:', value + '\n');
ipcMain.send('HARDWARE_BTN', value);
});
哦,伙计,看起来你的 Electron 和 GPIO 组合遇到了一些问题。 fs.watch 的东西可能有点丢失——确保它正在查看正确的 GPIO 文件,是吗?将其指向正前方并允许其继续关注事物。
那个 setInterval 槽导致应用程序崩溃?也许是聚会太辛苦了。调低一点,比如,只有在距离上次查看已经有一段时间后才检查按钮。避免应用程序烧毁。根据您的需要调整时间内容(setInterval)。
此外,请确保您的 GPIO 库已在本次 Electron 之旅中全部安装完毕。如果其他一切都失败了,请访问图书馆社区或深入研究文档。继续编码,伙计!