leds-user-defind.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/of.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/timer.h>
  6. struct my_device_data {
  7. struct timer_list my_timer;
  8. struct platform_device *pdev; // 添加指向 platform_device 的指针
  9. };
  10. void my_timer_callback(struct timer_list *t) {
  11. struct my_device_data *data = from_timer(data, t, my_timer);
  12. dev_info(&data->pdev->dev, "hello world\n"); // 通过指针访问设备
  13. // 重新启动定时器
  14. mod_timer(&data->my_timer, jiffies + msecs_to_jiffies(10000));
  15. }
  16. static int my_device_probe(struct platform_device *pdev) {
  17. struct my_device_data *data;
  18. struct device_node *np = pdev->dev.of_node;
  19. // 分配设备数据结构
  20. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  21. if (!data)
  22. return -ENOMEM;
  23. // 存储 platform_device 指针
  24. data->pdev = pdev;
  25. // 初始化定时器
  26. timer_setup(&data->my_timer, my_timer_callback, 0);
  27. // 启动定时器
  28. mod_timer(&data->my_timer, jiffies + msecs_to_jiffies(10000));
  29. // 存储设备数据指针
  30. platform_set_drvdata(pdev, data);
  31. dev_info(&pdev->dev, "设备匹配: %s\n", np->name);
  32. return 0;
  33. }
  34. static int my_device_remove(struct platform_device *pdev) {
  35. struct my_device_data *data = platform_get_drvdata(pdev);
  36. // 删除定时器
  37. del_timer_sync(&data->my_timer);
  38. dev_info(&pdev->dev, "设备移除\n");
  39. return 0;
  40. }
  41. static const struct of_device_id my_device_ids[] = {
  42. { .compatible = "mynode", },
  43. { /* sentinel */ }
  44. };
  45. MODULE_DEVICE_TABLE(of, my_device_ids);
  46. static struct platform_driver my_device_driver = {
  47. .probe = my_device_probe,
  48. .remove = my_device_remove,
  49. .driver = {
  50. .name = "my_device_driver",
  51. .of_match_table = my_device_ids,
  52. .owner = THIS_MODULE,
  53. },
  54. };
  55. module_platform_driver(my_device_driver);
  56. MODULE_LICENSE("GPL");
  57. MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
  58. MODULE_DESCRIPTION("示例设备树驱动,每10秒打印一次hello world");
  59. private fun addAndUploadPhoto(path: String, context: Context, activity: Activity, ok: (String) -> Unit) {
  60. "addAndUploadPhoto path:${path}".logDebug()
  61. uploadTag = path
  62. val file = File(path)
  63. val requestBody = RequestBody.create(MediaType.parse("image/*"), file)
  64. val body = MultipartBody.Part.createFormData("file", file.name, requestBody)
  65. val client = OkHttpClient()
  66. val url = "https://your-api-endpoint/admin/sys-file/upload"
  67. val request = Request.Builder()
  68. .url(url)
  69. .post(body)
  70. .build()
  71. client.newCall(request).enqueue(object : Callback {
  72. override fun onFailure(call: Call, e: IOException) {
  73. activity.runOnUiThread {
  74. "图片上传失败".toastShort()
  75. mLoadingDialog?.dismiss()
  76. mLoadingDialog = null
  77. }
  78. }
  79. override fun onResponse(call: Call, response: Response) {
  80. if (response.isSuccessful) {
  81. val responseBody = response.body()?.string()
  82. val jsonObject = JSONObject(responseBody)
  83. val filePath = jsonObject.getString("filePath")
  84. activity.runOnUiThread {
  85. "https://your-api-endpoint/$filePath".logDebug()
  86. ok.invoke("https://your-api-endpoint/$filePath")
  87. "上传成功".toastShort()
  88. mLoadingDialog?.dismiss()
  89. mLoadingDialog = null
  90. }
  91. } else {
  92. activity.runOnUiThread {
  93. "图片上传失败".toastShort()
  94. mLoadingDialog?.dismiss()
  95. mLoadingDialog = null
  96. }
  97. }
  98. }
  99. })
  100. }