Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/i2c.h>
- #include <linux/delay.h>
- /* Add your code here */
- static int nunchuk_probe(struct i2c_client *client, const struct i2c_device_id *id);
- static int nunchuk_remove(struct i2c_client *client);
- MODULE_LICENSE("GPL");
- static const struct i2c_device_id nunchuk_id[] = {
- { "nunchuk", 0 },
- { }
- };
- MODULE_DEVICE_TABLE(i2c, nunchuk_id);
- #ifdef CONFIG_OF
- static const struct of_device_id nunchuk_dt_ids[] = {
- { .compatible = "nintendo,nunchuk", },
- { }
- };
- MODULE_DEVICE_TABLE(of, nunchuk_dt_ids);
- #endif
- static struct i2c_driver nunchuk_driver = {
- .probe = nunchuk_probe,
- .remove = nunchuk_remove,
- .id_table = nunchuk_id,
- .driver = {
- .name = "nunchuk",
- .owner = THIS_MODULE,
- .of_match_table = of_match_ptr(nunchuk_dt_ids),
- },
- };
- module_i2c_driver(nunchuk_driver);
- static char nunchuk_read_registers(struct i2c_client *client)
- {
- const char read_buffer[6];
- mdelay(10);
- i2c_smbus_write_byte(client,0x00);
- mdelay(10);
- i2c_master_recv(client,&read_buffer,6);
- return read_buffer[5];
- }
- static int nunchuk_probe(struct i2c_client *client, const struct i2c_device_id *id)
- {
- int ret;
- int i=0,zpressed,cpressed,nulti_bit,prvi_bit;
- char prvi,drugi;
- const char buffer[4] = {0xf0,0x55,0xfb,0x00};
- pr_info();
- pr_alert("usao");
- ret = i2c_master_send(client,&buffer[i],2);
- udelay(1000);
- if (ret < 0)
- {
- dev_err(&client->dev, "I2C write error\n");
- return ret;
- }
- ret = i2c_master_send(client,&buffer[i+2],2);
- if (ret < 0)
- {
- dev_err(&client->dev, "I2C write error\n");
- return ret;
- }
- //citanje
- prvi = nunchuk_read_registers(client);
- drugi = nunchuk_read_registers(client);
- nulti_bit = drugi & 0b00000001;
- prvi_bit = drugi & 0b00000010;
- if (nulti_bit == 0)
- {
- zpressed = 1;
- }else
- {
- zpressed = 0;
- }
- if (prvi_bit == 0)
- {
- cpressed = 1;
- }else
- {
- cpressed = 0;
- }
- if (cpressed==1)
- pr_alert("Stisnut c");
- if (cpressed==0)
- pr_alert("Pusten c");
- if (zpressed==1)
- pr_alert("Stisnut z");
- if (zpressed==0)
- pr_alert("Pusten z");
- return 0;
- }
- static int nunchuk_remove(struct i2c_client *client)
- {
- pr_info();
- pr_alert("izasao");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement