b.liu | 8583dce | 2024-04-03 13:30:08 +0800 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <lynq/lynq-gpio.h> |
| 6 | |
| 7 | static void printUsage(void) |
| 8 | { |
| 9 | printf("-ds [direction] --lynq_gpio_direction_set\n"); |
| 10 | printf("-vs [value] --lynq_gpio_value_set\n"); |
| 11 | printf("-ps [pullsel] --lynq_gpio_pullsel_set\n"); |
| 12 | printf("-vg --lynq_gpio_value_get\n"); |
| 13 | printf("-pg --lynq_gpio_pullsel_get\n"); |
| 14 | printf("-quit\n"); |
| 15 | printf("Please input an cmd:"); |
| 16 | } |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | int main(int argc,char** argv) |
| 22 | { |
| 23 | int ret; |
| 24 | int gpio; |
| 25 | int direction; |
| 26 | int value; |
| 27 | int pullsel; |
| 28 | char cmd[16]; |
| 29 | char *cmd2; |
| 30 | char *cmd1; |
| 31 | |
| 32 | if(argc != 2) |
| 33 | { |
| 34 | printf("wrong input format, please inout lynq-irq-demo <gpio>\n"); |
| 35 | return -1; |
| 36 | } |
| 37 | gpio = atoi(argv[1]); |
| 38 | printf("gpio is %d\n", gpio); |
| 39 | ret = lynq_gpio_init(gpio, 0, 0, 0); |
| 40 | if(ret != 0) |
| 41 | { |
| 42 | printf("lynq_gpio_init fail\n"); |
| 43 | return -1; |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | printf("lynq_gpio_init success\n"); |
| 48 | } |
| 49 | |
| 50 | while(1) |
| 51 | { |
| 52 | printUsage(); |
| 53 | memset(cmd,0,sizeof(cmd)); |
| 54 | fgets(cmd, sizeof(cmd), stdin); |
| 55 | printf("cmd:%s\n",cmd); |
| 56 | cmd1 = strtok(cmd, " "); |
| 57 | cmd2 = strtok(NULL, " "); |
| 58 | if(strcmp(cmd1,"-ds") == 0) |
| 59 | { |
| 60 | if(cmd2 == NULL) |
| 61 | { |
| 62 | printf("direction is NULL\n"); |
| 63 | continue; |
| 64 | } |
| 65 | direction = atoi(cmd2); |
| 66 | ret = lynq_gpio_direction_set(gpio, direction); |
| 67 | if(ret != 0) |
| 68 | { |
| 69 | printf("lynq_gpio_direction_set fail\n"); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | printf("lynq_gpio_direction_set success\n"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | else if(strcmp(cmd1,"-vs") == 0) |
| 78 | { |
| 79 | if(cmd2 == NULL) |
| 80 | { |
| 81 | printf("wake_state is NULL\n"); |
| 82 | continue; |
| 83 | } |
| 84 | value = atoi(cmd2); |
| 85 | ret = lynq_gpio_value_set(gpio, value); |
| 86 | if(ret < 0) |
| 87 | { |
| 88 | printf("lynq_gpio_value_set fail\n"); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | printf("lynq_gpio_value_set success\n"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | else if (strcmp(cmd1,"-ps") == 0) |
| 97 | { |
| 98 | if(cmd2 == NULL) |
| 99 | { |
| 100 | printf("pullsel is NULL\n"); |
| 101 | continue; |
| 102 | } |
| 103 | pullsel = atoi(cmd2); |
| 104 | ret = lynq_gpio_pullsel_set(gpio, pullsel); |
| 105 | if(ret != 0) |
| 106 | { |
| 107 | printf("lynq_gpio_pullsel_set fail\n"); |
| 108 | printf("ret=%d\n", ret); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | printf("lynq_gpio_pullsel_set success\n"); |
| 113 | } |
| 114 | } |
| 115 | else if (strcmp(cmd1,"-vg\n") == 0) |
| 116 | { |
| 117 | ret = lynq_gpio_value_get(gpio); |
| 118 | if(ret < 0) |
| 119 | { |
| 120 | printf("lynq_gpio_value_get fail\n"); |
| 121 | printf("ret=%d\n", ret); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | printf("lynq_gpio_value_get success\n"); |
| 126 | printf("ret=%d\n", ret); |
| 127 | } |
| 128 | } |
| 129 | else if(strcmp(cmd1,"-pg\n") == 0) |
| 130 | { |
| 131 | ret = lynq_gpio_pullsel_get(gpio); |
| 132 | if(ret < 0) |
| 133 | { |
| 134 | printf("lynq_gpio_pullsel_get fail\n"); |
| 135 | printf("ret=%d\n", ret); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | printf("lynq_gpio_pullsel_get success\n"); |
| 140 | printf("ret=%d\n", ret); |
| 141 | } |
| 142 | } |
| 143 | else if(strcmp(cmd1,"-quit\n") == 0) |
| 144 | { |
| 145 | break; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | printf("wrong input format\n"); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | } |
| 154 | |
| 155 | |
| 156 | ret = lynq_gpio_deinit(gpio); |
| 157 | if(ret != 0) |
| 158 | { |
| 159 | printf("lynq_gpio_deinit fail\n"); |
| 160 | printf("ret=%d\n", ret); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | printf("lynq_gpio_deinit success\n"); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | |
| 169 | } |