I am using C++Builder Enterprise and need some ideas.
I have a Form with a bunch of TButton and TSpeedButton controls on it. What I want to have happen is that, when a given button is pressed, I want to disable it and several others, while enabling several other buttons, i.e. state changes.
The issue is that I am duplicating the enable/disable code in a bunch of places. I've thought about somehow using the TNotifyEvent delegation to alert the buttons of a state change, but I don't think that delegation technique will work in this situation. I want to avoid creating a bunch of sub-classes of TButton/TSpeedButton.
I also would like to try to use techniques that are available from the VCL, as in each component carries an observer list and wonder if I could leverage that somehow.
My 2 cents worth..
I have done a proof of concept for your problem, would need a lot more work but I think it is doable and should do what you want.
I will do more work on it and post if there is interest.
To find all controls of a class type on a form
--- TControlAgent->FindTControl(this, "TButton");
I added 4 buttons on a form, added code bellow to button1 click event,
TControlAgent->SetControlProperty("Enabled", "TButton", false, true, "Button3");
in this case, want controls with Enabled property whose class name is TButton, state = true or false, execute the action but but exclude control named TButton3
There result was all TButtons on the form were set to disable except for button 3
(note: the exclude option would be a lookup list of controls to be excluded from any action or be able to set a state that is not the state eg Enabled = !true or !false )
In the click event of button3 I placed the following code;
TControlAgent->SetControlProperty("Enabled", "TButton", true, true, "");
The result of this is to re-enable all the controls, this is just an idea but with with the extra work it could be possible to execute any action on any control from a single parent form for any form in a collection of forms.
I also think it could be possible to fire events, just a crazy idea...
.h file using berlin 10.1
//---------------------------------------------------------------------------
#ifndef TControlAgentH
#define TControlAgentH
//---------------------------------------------------------------------------
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <System.TypInfo.hpp>
#include <vector>
#include <algorithm>
//---------------------------------------------------------------------------
class PACKAGE TControlAgent : public TComponent
{
private:
std::vector<TControl*> FControls;
protected:
public:
std::vector<UnicodeString> excludeControlNames;
__fastcall TControlAgent(TComponent* Owner);
TControl * __fastcall GetControl(TControl* ctrl, UnicodeString property);
void __fastcall FindTControl(TForm *f, UnicodeString className);
TControl* __fastcall SetControlProperty(UnicodeString property, UnicodeString className, bool state, bool exec, UnicodeString excludeControlName );
__published:
};
//---------------------------------------------------------------------------
#endif
.cpp file
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TControlAgent.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
//---------------------------------------------------------------------------
// component to act as agent for other components derived from TControl
//
// Components have properties and events, is it possible to get all TControls into a vector
// then be able to get a property of a component then fire an event or carry out an action
// on the component!!!!
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TControlAgent *)
{
new TControlAgent(NULL);
}
//---------------------------------------------------------------------------
__fastcall TControlAgent::TControlAgent(TComponent* Owner)
: TComponent(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::FindTControl(TForm *f, UnicodeString className)
{
FControls.clear();
for(int i = 0; i < f->ControlCount; i++)
{
if(f->Controls[i]->ClassName() == className) //control classes that to set as a group
FControls.push_back(f->Controls[i]);
}
//note: could have a function that appends other class names
}
//---------------------------------------------------------------------------
TControl* __fastcall TControlAgent::SetControlProperty(UnicodeString property, UnicodeString className, bool state, bool exec, UnicodeString excludeControlName )
{
PPropInfo propinfo;
for(int i = 0; i < FControls.size(); i++)
{
if(FControls[i]->ClassName() == className)
{
propinfo = GetPropInfo(FControls[i], property);
if (!propinfo)
continue;
if(exec && FControls[i]->Name != excludeControlName )
{
if(property == "Enabled")
FControls[i]->Enabled = state;
}
}
}
}
//---------------------------------------------------------------------------
namespace Tcontrolagent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TControlAgent)};
RegisterComponents(L"SQLite", classes, 0);
}
}
//---------------------------------------------------------------------------
**** UPDATED *****
Progress of TControlAgent
Obviously still work in progress but this code works, create a form with a TPanel, to this add 6 TButtons
Add a TControl Agent, set the OnButtonClick event as per Form code bellow
TControlAgent code
.h
//---------------------------------------------------------------------------
#ifndef TControlAgentH
#define TControlAgentH
//---------------------------------------------------------------------------
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <System.TypInfo.hpp>
#include <vector>
#include <algorithm>
class TControls;
class TControlGroup;
typedef void __fastcall (__closure *TControlAgentOnClick)(System::TObject *Sender, TControls *ctrl);
typedef void __fastcall (__closure *TButtonClickEvent)(System::TObject *Sender);
enum action {aEnabled, aOnClick, aChange};
enum tcontrols {tbutton, tedit, tcombo, tcheckbox};
//---------------------------------------------------------------------------
class PACKAGE TControlAgent : public TComponent
{
private:
std::vector<TControls*> FControls;
std::vector<TControlGroup*> FControlGroups;
TControlAgentOnClick FClickSupliment;
TButtonClickEvent FOnButtonClick;
bool FButtonClickRedirect;
protected:
public:
std::vector<UnicodeString> excludeControlNames; // not implemented yet
std::vector<TControlGroup*> __fastcall Groups();
std::vector<TControls*> __fastcall Controls(int GroupIndex);
__fastcall TControlAgent(TComponent* Owner);
TControl * __fastcall GetControl(TControl* ctrl, UnicodeString property);
void __fastcall FindTControl(TForm *f, UnicodeString className);
void __fastcall FindTControl(TPanel *p, UnicodeString GroupName, bool ClearIfControlsExists);
void __fastcall SetControlProperty(TControl *ctrl, bool state, int Action);
int __fastcall GetGroup(String name);
void __fastcall SetControlClickEvent(String ClassName, String GroupName, String ExcludeGroup, int tControlType);
void __fastcall SetButtonPropValue(TButton* b, String Property, String Value, bool v);
int __fastcall AddTControl(TControl* c, String Group);
void __fastcall SetButtonPropValue(String Group, String ExcludeGroup,int TControlType,String Property,String GroupValue,bool GroupV,String excludeGroupValue,bool excludeGroupV);
__published:
__property bool TButtonClick = {read = FButtonClickRedirect, write = FButtonClickRedirect };
__property TControlAgentOnClick OnClickSuppliment={read=FClickSupliment, write=FClickSupliment};
__property TButtonClickEvent OnButtonClick = {read = FOnButtonClick, write = FOnButtonClick };
};
//---------------------------------------------------------------------------
class TControlGroup
{
public:
UnicodeString GroupName;
std::vector<TControls*> GroupControls;
__fastcall TControlGroup();
};
//---------------------------------------------------------------------------
class TControls
{
public:
TControl* ctrl;
bool WantButtonOnClick;
bool WantControlOnChange;
bool FireStateChange;
bool WantOnClickSupliment;
bool state;
__fastcall TControls();
};
#endif
.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TControlAgent.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
//---------------------------------------------------------------------------
// component to act as agent for other components derived from TControl
//
// Components have properties and events, is it possible to get all TControls into a vector
// then be able to get a property of a component then fire an event or carry out an action
// on the component!!!!
//---------------------------------------------------------------------------
struct IsCtrlGroup {
String _name;
IsCtrlGroup(String name) : _name(name)
{
}
bool operator()(const TControlGroup * item) const
{
return (item->GroupName == _name);
}
};
//---------------------------------------------------------------------------
struct IsClassName {
String _name;
IsClassName(String name) : _name(name)
{
}
bool operator()(const TControl * item) const
{
return (item->ClassName() == _name);
}
};
//---------------------------------------------------------------------------
struct IsCtrl {
TControl* _ctrl;
IsCtrl(TControl* ctrl) : _ctrl(ctrl) {
}
bool operator()(const TControls * item) const {
return (item->ctrl->Name == _ctrl->Name);
}
};
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TControlAgent *)
{
new TControlAgent(NULL);
}
//---------------------------------------------------------------------------
__fastcall TControlAgent::TControlAgent(TComponent* Owner)
: TComponent(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::FindTControl(TForm *f, UnicodeString className)
{
FControls.clear();
TControls* ctrl;
for(int i = 0; i < f->ControlCount; i++)
{
if(f->Controls[i]->ClassName() == className) //control classes that to set as a group
{
TControls* ctrl = new TControls();
ctrl->ctrl = f->Controls[i];
ctrl->FireStateChange = false;
ctrl->WantOnClickSupliment = false;
FControls.push_back(ctrl);
}
}
//note: could have a function that appends other class names
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::FindTControl(TPanel *p, UnicodeString GroupName, bool ClearIfControlsExists)
{
/*
group vector[]
--> controlsindex vector[index]
*/
TControlGroup* g;
TControls* ctrl;
int i = -1;
int group_index = -1, controls_index = -1;
// check if group name exists in group vector
group_index = GetGroup(GroupName);
//clear controls vector if exists, controls will not exist if group does not exist...
if(ClearIfControlsExists && group_index > 0)
FControlGroups[group_index]->GroupControls.clear();
// if group does not exist, push new group onto vector
if(group_index == -1)
{
g = new TControlGroup();
g->GroupName = GroupName;
FControlGroups.push_back(g);
group_index = GetGroup(GroupName);
}
//group must bnow exist
for(i = 0; i < p->ControlCount; i++)
{
TControls* ctrl = new TControls();
ctrl->ctrl = p->Controls[i];
FControlGroups[group_index]->GroupControls.push_back(ctrl);
controls_index = FControlGroups[group_index]->GroupControls.size() -1;
FControlGroups[group_index]->GroupControls[controls_index]->ctrl = p->Controls[i];
}
}
//---------------------------------------------------------------------------
int __fastcall TControlAgent::AddTControl(TControl* c, String Group)
{
int index;
TControlGroup* g;
int group_index = GetGroup(Group);
if(group_index == -1)
{
g = new TControlGroup();
g->GroupName = Group;
FControlGroups.push_back(g);
group_index = GetGroup(Group);
}
TControls* ctrl = new TControls();
ctrl->ctrl = c;
FControlGroups[group_index]->GroupControls.push_back(ctrl);
index = FControlGroups[group_index]->GroupControls.size()-1;
return(index);
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::SetControlClickEvent(String ClassName, String GroupName, String ExcludeGroup, int tControlType)
{
int group_index = GetGroup(GroupName);
for(int i = 0; i < FControlGroups[group_index]->GroupControls.size(); i++)
{
if(FControlGroups[group_index]->GroupControls[i]->ctrl->ClassName() == ClassName)
{
switch(tControlType)
{
case tbutton:
dynamic_cast<TButton*>(FControlGroups[group_index]->GroupControls[i]->ctrl)->OnClick = FOnButtonClick;
break;
case tedit:
break;
case tcombo:
break;
case tcheckbox:
break;
}
}
}
}
//---------------------------------------------------------------------------
std::vector<TControlGroup*> __fastcall TControlAgent::Groups()
{
return(FControlGroups);
}
//---------------------------------------------------------------------------
std::vector<TControls*> __fastcall TControlAgent::Controls(int GroupIndex)
{
return(FControlGroups[GroupIndex]->GroupControls);
}
//---------------------------------------------------------------------------
int __fastcall TControlAgent::GetGroup(String name)
{
int group_index =-1;
std::vector<TControlGroup*>::iterator found = std::find_if(FControlGroups.begin(), FControlGroups.end(), IsCtrlGroup(name));
if(found != FControlGroups.end())
group_index = std::distance(FControlGroups.begin(), found);
return(group_index);
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::SetButtonPropValue(TButton* b, String Property, String Value, bool v)
{
PPropInfo propinfo;
propinfo = GetPropInfo(b, Property);
if (!propinfo)
return;
if(Value.IsEmpty())
SetPropValue(b, propinfo, v);
else
SetPropValue(b, propinfo, Value);
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::SetButtonPropValue(String Group,
String ExcludeGroup,
int TControlType,
String Property,
String GroupValue,
bool GroupV,
String excludeGroupValue,
bool excludeGroupV)
{
// Group can hold all TControls on a form
// ExcludeGroup contains TControls that will be excluded from an action on the Group controls
// Group is an existing group of TControls found on a container
// ExcludGroup is a group that can be found on a container or added to a Group
// then parsed to this method in ExcludeGroup param
int i;
PPropInfo propinfo;
TControl *c;
int group_index = GetGroup(Group);
int exclude_Group_index = GetGroup(ExcludeGroup);
TControl* ctrl;
for(i = 0; i < FControlGroups[group_index]->GroupControls.size(); i++)
{
c = FControlGroups[group_index]->GroupControls[i]->ctrl;
//check if TControl is to be excluded
std::vector<TControls*>::iterator found = std::find_if(FControlGroups[exclude_Group_index]->GroupControls.begin(),
FControlGroups[exclude_Group_index]->GroupControls.end(),
IsCtrl(c));
// if found, control is in the exclude list so continue, do not apply
// property value change
if(found != FControlGroups[exclude_Group_index]->GroupControls.end())
{
c = (*found)->ctrl;
//set property value for exclude group controls
propinfo = GetPropInfo(c, Property);
if(propinfo)
{
if(excludeGroupValue.IsEmpty())
SetPropValue(c, propinfo, excludeGroupV);
else
SetPropValue(c, propinfo, excludeGroupValue);
}
continue;
}
//if it gets here, c is not in exclude list
propinfo = GetPropInfo(c, Property);
if (!propinfo)
return;
if(GroupValue.IsEmpty())
SetPropValue(c, propinfo, GroupV);
else
SetPropValue(c, propinfo, GroupValue);
}
}
//---------------------------------------------------------------------------
namespace Tcontrolagent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TControlAgent)};
RegisterComponents(L"SQLite", classes, 0);
}
}
//---------------------------------------------------------------------------
__fastcall TControls::TControls(){}
//---------------------------------------------------------------------------
__fastcall TControlGroup::TControlGroup(){}
Form Code
.cpp
Form has a TPanel with 6 TButtons placed on it
#run time TButton3 is disabled
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "frmTControlTest.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TControlAgent"
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
int i;
std::vector<TControls*> group_controls;
//get ALL controls on a container
ControlAgent1->FindTControl(p, "Group1", true);
//create the exclude (for want of a better name) groups
/*
add
edit
save
delete
cancel
exit
*/
ControlAgent1->AddTControl(bnSave, "Group2");
ControlAgent1->AddTControl(bnCancel, "Group2");
ControlAgent1->AddTControl(bnAdd, "Group3");
ControlAgent1->AddTControl(bnEdit, "Group3");
ControlAgent1->AddTControl(bnDelete, "Group3");
ControlAgent1->AddTControl(bnExit, "Group3");
i = ControlAgent1->GetGroup("Group1");
group_controls = ControlAgent1->Controls(i);
ControlAgent1->SetControlClickEvent("TButton", "Group1", "", tbutton);
}
//---------------------------------------------------------------------------
void __fastcall TForm3::ControlAgent1ButtonClick(TObject *Sender)
{
TButton* b;
if((b = dynamic_cast<TButton*>(Sender)) == bnAdd )
ControlAgent1->SetButtonPropValue("Group1", "Group2", tbutton, "Enabled", "", false, "", true);
if((b = dynamic_cast<TButton*>(Sender)) == bnCancel )
ControlAgent1->SetButtonPropValue("Group1", "Group3", tbutton, "Enabled", "", false, "", true);
if((b = dynamic_cast<TButton*>(Sender)) == bnSave )
{
ControlAgent1->SetButtonPropValue("Group1", "Group3", tbutton, "Enabled", "", false, "", true);
//Do Stuff
}
}
//---------------------------------------------------------------------------
i'm new to vulkan.
when i call vkCreateInstance() , it crash , but i can't figure out what's the problem.
vkenumerateExtensionProperties return 0 extensoin count,and only VKLayer_LUNARG_api_dump found , it is weird .
core code are following , irrelevant code are removed.
Instance_Vulkan
#pragma once
#ifndef INSTANCE_VULKAN_HPP
#define INSTANCE_VULKAN_HPP
#include <vulkan/vulkan.h>
#include <vector>
#include <string>
#include <exception>
#include <iostream>
using namespace std;
namespace LB
{
class Instance_Vulkan
{
public:
bool isCreated = false;
VkInstance instance;
VkInstanceCreateInfo createInfo;
std::vector<const char*> extensions;
std::vector<const char*> layers;
Instance_Vulkan()
{
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.flags = 0;
createInfo.pNext = NULL;
createInfo.pApplicationInfo = NULL;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = NULL;
createInfo.enabledExtensionCount = 0;
createInfo.ppEnabledExtensionNames = NULL;
}
VkResult creat(const VkAllocationCallbacks* pAllocator=NULL)
{
if (isCreated)
return VK_SUCCESS;
else
{
VkResult result;
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
if(createInfo.enabledExtensionCount)
createInfo.ppEnabledExtensionNames = extensions.data();
createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
if(createInfo.enabledLayerCount)
createInfo.ppEnabledLayerNames = layers.data();
if((result = vkCreateInstance(&createInfo, nullptr, &instance))!=VK_SUCCESS)
cout << "Error: fail to create Vulkan Instance ! " << endl;
if (result == VK_SUCCESS)
isCreated = true;
return result;
}
}
void destroy()
{
if (isCreated)
vkDestroyInstance(instance,nullptr);
isCreated = false;
}
};
}
#endif // !INSTANCE_VULKAN_HPP
following code invoke above class
#pragma once
#ifndef APPLICATION_HPP
#define APPLICATION_HPP
#include<vulkan/vulkan.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "Item.hpp"
#include"Window.hpp"
#include "Instance_Vulkan.hpp"
using namespace std;
namespace LB
{
class Application
{
public:
bool shouldClose = false;
virtual void mainLoop()
{
}
void init()
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API,GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
initVulkan();
}
void initVulkan()
{
// set the application info
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hellp Triangle";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName= "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
appInfo.pNext = NULL;
// set papplicatoinInfo
instance.createInfo.pApplicationInfo = &appInfo;
if (instance.creat()!= VK_SUCCESS)
throw std::runtime_error("Error: fail to create vulkan instance ");
}
void cleanUp()
{
glfwTerminate();
instance.destroy();
}
int run(int argc=0,char* argv[]=0)
{
init();
try {
for (;!shouldClose;)
mainLoop();
}
catch (const std::exception e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
cleanUp();
return EXIT_SUCCESS;
}
private:
Instance_Vulkan instance;
};
}
#endif
there are main function
#include "pch.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "Application.hpp"
#include <iostream>
#include <cstdlib>
using namespace LB;
using namespace std;
int main()
{
Application app;
return app.run();
}
when i run the code,it crash
'result image'
My Environment:
Win10 debug 64bit
vs2017
Gtx 940m driver: 416.94
i install lunarg sdk 1.1.85.0 , run the cube.exe and also compile the demo successfully
I created a GUI to play two videos from two cameras, one camera is a USB camera, and another one is integrated in my laptop. My code is simple, I have three files:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraImageCapture>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QList <QCameraInfo> camList;
QCamera *camera1;
QCamera *camera2;
private slots:
void onCameraChanged(int);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
camera1 = NULL;
camera2 = NULL;
connect(ui->cameraComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,
&MainWindow::onCameraChanged);
camList = QCameraInfo::availableCameras();
for(QList <QCameraInfo>::iterator it = camList.begin();it!=camList.end();++it) {
ui->cameraComboBox->addItem(it->description());
}
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onCameraChanged(int idx) {
if(camera1 != NULL) {
camera1->stop();
}
qDebug() << idx;
camera1 = new QCamera(camList.at(idx),this);
camera1->setViewfinder(ui->mainView);
camera1->setCaptureMode(QCamera::CaptureStillImage);
camera1->start();
if(camera2 != NULL) {
camera2->stop();
}
camera2 = new QCamera(camList.at(idx+1),this);
camera2->setViewfinder(ui->submainView);
camera2->setCaptureMode(QCamera::CaptureStillImage);
camera2->start();
}
I have deleted irrelevant code. Of course I have this in the *.pro file:
QT += multimedia multimediawidgets
My code compiles correctly, but it can only stream one video (camera1), and the other one shows nothing. The error I got is
CameraBin error: "Error starting streaming on device '/dev/video1'."
CameraBin error: "Could not negotiate format"
Any help will be appreciated.
I am trying to learn how to write a device driver in linux, following some reference from google and ldd3. i am able to insert the module below but when i tried to open the device in an application the kernel crashed.
The code and build steps followed as below :
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/ioport.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/param.h>
#include <linux/fs.h>
/* =============== Constant Definitions ============ */
#define SERIAL_IRQ 4
/* =============== Variable Definitions ============ */
static int SER_MAJOR = 0;
int ser_open(struct inode *inode, struct file *filp);
int ser_release(struct inode *inode, struct file *filp);
irqreturn_t my_ser_dev_isr(int irq,void *ser_data,struct pt_regs * pt_reg_var)
{
printk("\n\n ------- INTR raised -----------\n\n");
return 0;
}
int ser_open(struct inode *inode, struct file *filp)
{
if(request_irq(SERIAL_IRQ,&my_ser_dev_isr,1,"my_ser_dev_intr",NULL))
{
printk("\n interrupt req failed\n");
}
else
{
enable_irq(SERIAL_IRQ);
printk("\n!!!! ..obtained the requested interrupt and enabled\n");
}
}
int ser_release(struct inode *inode, struct file *filp)
{
disable_irq(SERIAL_IRQ);
free_irq(SERIAL_IRQ,NULL) ;
}
static struct file_operations ser_fops = {
open: ser_open,
release: ser_release
};
void *p = NULL;
irqreturn_t my_ser_dev_isr (int, void *, struct pt_regs *);
static int __init hello_start(void)
{
int ret_val=-1;
int result;
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
result = register_chrdev(SER_MAJOR,"SER_DEV",&ser_fops);
if(result < 0)
{
printk(KERN_WARNING"Can't get major %d\n",SER_MAJOR);
return result;
}
if(SER_MAJOR == 0)
{
SER_MAJOR = result;
printk("SER DEV Major Number : %d",SER_MAJOR );
}
return 0;
}
static void __exit hello_end(void)
{
// free_irq(SERIAL_IRQ,NULL);
//release_region(0x0031,1);
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);
Makefile for module :
obj-m := hello.o
default:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
The application used for accesing is as follows :
#include <stdio.h> /* test.c */
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
static int dev;
int main(void)
{
char buff[40];
dev = open("/dev/my_ser_dev",O_RDONLY);
if(dev < 0)
{
printf( "Device Open ERROR!\n");
exit(1);
}
printf("Please push the GPIO_16 port!\n");
//read(dev,buff,40);
// scanf("%s",buff);
printf("%s\n",buff);
close(dev);
return 0;
}
insmod gave
[ 3837.312140] Loading hello module...
[ 3837.312147] Hello world
[ 3837.312218] SER DEV Major Number : 251
Then I created the special file using mknod /dev/my_ser_dev c 251 0
Executing the application caused kernel crash. I am using UBUNTU 3.2.0-23-generic-pae.
The function you are registering as your IRQ handler has the wrong prototype - it should be like
irqreturn_t irq_handler(int, void *);
Maybe you are referring to old documentation.