{"id":207606,"date":"2025-07-08T11:59:35","date_gmt":"2025-07-08T03:59:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207606\/"},"modified":"2025-07-08T11:59:35","modified_gmt":"2025-07-08T03:59:35","slug":"%e5%9c%a8%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e4%b8%ad%e6%a8%a1%e6%8b%9f-%e4%bc%aa%e9%80%a0-%e6%9b%bf%e6%8d%a2%e7%a1%ac%e4%bb%b6%e7%9b%b8%e5%85%b3%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207606\/","title":{"rendered":"\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6a21\u62df\/\u4f2a\u9020\/\u66ff\u6362\u786c\u4ef6\u76f8\u5173\u529f\u80fd"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6a21\u62df\/\u4f2a\u9020\/\u66ff\u6362\u786c\u4ef6\u76f8\u5173\u529f\u80fd<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 22:06:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6a21\u62df\/\u4f2a\u9020\/\u66ff\u6362\u786c\u4ef6\u76f8\u5173\u529f\u80fd\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u76ee\u524d\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u6709\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>func pinexported(pin int) bool {\n    pinpath := fmt.sprintf(\"\/sys\/class\/gpio\/gpio%d\", pin)\n    if file, err := os.stat(pinpath); err == nil &amp;&amp; len(file.name()) &gt; 0 {\n        return true\n    }\n    return false\n}\n<\/pre>\n<p>\u540c\u4e00\u6587\u4ef6\u4e2d\u7684\u53e6\u4e00\u4e2a\u4ee3\u7801\u90e8\u5206\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func isgpiopinexported(gpiopin int) bool {\n    exported := pinexported(gpiopin)\n    for !exported &amp;&amp; (timeout &lt; timeoutforpinexportinmilliseconds) {\n        timeout++\n        time.sleep(1 * time.millisecond)\n        exported = pinexported(gpiopin)\n    }\n     ...\n<\/pre>\n<p>\u6240\u4ee5\u73b0\u5728\u6211\u6b63\u5728\u5bfb\u627e\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u6cd5\u6765\u6a21\u62df\/\u66ff\u6362\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u4e0a\u8ff0 <code>pinexported<\/code> \u51fd\u6570\uff0c\u4ee5\u6d4b\u8bd5 <code>isgpiopinexported<\/code> \u5185\u90e8\u7684\u903b\u8f91\uff0c\u56e0\u4e3a\u51fd\u6570 <code>pinexported<\/code> \u4f9d\u8d56\u4e8e\u786c\u4ef6\uff08raspberry pi\uff09\u3002<\/p>\n<p>\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u53ef\u80fd\u662f\u5c06 <code>pinexported<\/code> \u51fd\u6570\u4f5c\u4e3a <code>isgpiopinexported<\/code> \u7684\u53c2\u6570<\/p>\n<p>\u56e0\u6b64\u5b9a\u4e49\u4e00\u4e2a\u8fd9\u6837\u7684\u51fd\u6570\u7c7b\u578b\uff1a<\/p>\n<pre>type pinexported func(int) int<\/pre>\n<p>\u8fd9\u610f\u5473\u7740\u6211\u5fc5\u987b\u50cf\u8fd9\u6837\u5b9a\u4e49 <code>isgpiopinexported<\/code> \uff1a<\/p>\n<pre>isGpioPinExported(pinExported pinExported, gpioPin int) bool {\n    exported := pinExported(gpioPin)\n    for !exported &amp;&amp; (timeOut &lt; timeOutForPinExportInMilliseconds) {\n    ...\n    }\n    ..\n}<\/pre>\n<p>\u73b0\u5728\u6211\u53ef\u4ee5\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u5e76\u5b9a\u4e49\u6a21\u62df\/\u5047 <code>pinexported<\/code> \uff0c\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4e00\u5207\u90fd\u5f88\u597d\u3002\u4f46\u6211\u6709\u5927\u7ea6\u4e94\u516d\u4e2a\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u5c06\u5bfc\u81f4\u5c06\u4e94\u516d\u4e2a\u8865\u5145\u53c2\u6570\u653e\u5165\u50cf <code>isgpiopinexported<\/code> \u8fd9\u6837\u7684\u51fd\u6570\u4e2d\uff0c\u8fd9\u662f\u5b8c\u5168\u9519\u8bef\u7684\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u95ee\u9898\u662f\u5982\u679c\u6ca1\u6709\u5728\u6d4b\u8bd5\u4e2d\u8fd0\u884c\uff0c\u6211\u5728\u54ea\u91cc\u53ef\u4ee5\u5b9a\u4e49\u4f7f\u7528\u7684\u9ed8\u8ba4\u5b9e\u73b0\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u56e0\u6b64\uff0c\u6839\u636e mkopriva \u7684\u5efa\u8bae\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u754c\u9762\uff08\u73b0\u5728\u5177\u6709\u4e09\u4e2a\u51fd\u6570\u6765\u67e5\u770b\u5176\u5b9e\u9645\u5de5\u4f5c\u539f\u7406\uff09\uff1a<\/p>\n<pre>type raspberry interface {\n    ispinexported(gpiopin int) bool\n    valueexist(gpiopin int) bool\n    directionexist(gpiopin int) bool\n}<\/pre>\n<p>\u8fdb\u4e00\u6b65\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u5b9e\u73b0\u771f\u5b9e\u786c\u4ef6\uff08raspberry\uff09\uff1a<\/p>\n<pre>type rasberry3plus struct {\n}\n\nfunc (raspberry rasberry3plus) valueexist(gpiopin int) bool {\n    pinpath := fmt.sprintf(\"%s%d\/value\", sysclassgpiopin, gpiopin)\n    if file, err := os.stat(pinpath); err == nil &amp;&amp; len(file.name()) &gt; 0 {\n        return true\n    }\n    return false\n}\n\nfunc (raspberry rasberry3plus) directionexist(gpiopin int) bool {\n    pinpath := fmt.sprintf(\"%s%d\/direction\", sysclassgpiopin, gpiopin)\n    if file, err := os.stat(pinpath); err == nil &amp;&amp; len(file.name()) &gt; 0 {\n        return true\n    }\n    return false\n}\n\nfunc (raspberry rasberry3plus) ispinexported(gpiopin int) bool {\n    pinpath := fmt.sprintf(\"%s%d\", sysclassgpiopin, gpiopin)\n    if file, err := os.stat(pinpath); err == nil &amp;&amp; len(file.name()) &gt; 0 {\n        return true\n    }\n    return false\n}<\/pre>\n<p>\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u7684\u51fd\u6570 <code>isgpiopinexported<\/code> \u73b0\u5728\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff08\u8fd9\u53ea\u662f\u4e00\u4e2a\u793a\u4f8b\u5b9e\u73b0\uff0c\u7528\u4e8e\u4e86\u89e3\u6a21\u62df\u6d4b\u8bd5\u5982\u4f55\u5de5\u4f5c\uff09\uff1a<\/p>\n<pre>func isgpiopinexported(raspberry raspberry, gpiopin int) bool {\n    pinexported := raspberry.ispinexported(gpiopin)\n    valueexist := raspberry.valueexist(gpiopin)\n    directionexist := raspberry.directionexist(gpiopin)\n\n    return valueexist &amp;&amp; directionexist &amp;&amp; pinexported\n}<\/pre>\n<p>\u73b0\u5728\u6d4b\u8bd5\u770b\u8d77\u6765\u50cf\u8fd9\u6837\u3002\u9996\u5148\uff0c\u6211\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u578b\uff08\u987a\u4fbf\u8bf4\u4e00\u53e5\uff1a\u6211\u51b3\u5b9a\u4f7f\u7528 mock\uff09\uff1a<\/p>\n<pre>import (\n    \"github.com\/stretchr\/testify\/assert\"\n    \"github.com\/stretchr\/testify\/mock\"\n    \"testing\"\n)\n\ntype mockraspberry struct {\n    mock.mock\n}\n\nfunc (raspmock mockraspberry) ispinexported(gpiopin int) bool {\n    args := raspmock.called(gpiopin)\n    return args.bool(0)\n}\nfunc (raspmock mockraspberry) valueexist(gpiopin int) bool {\n    args := raspmock.called(gpiopin)\n    return args.bool(0)\n}\nfunc (raspmock mockraspberry) directionexist(gpiopin int) bool {\n    args := raspmock.called(gpiopin)\n    return args.bool(0)\n}\n\nfunc test_valuetrue_directionexisttrue(t *testing.t) {\n    testobj := new(mockraspberry)\n\n    testobj.on(\"ispinexported\", 5).return(false)\n    testobj.on(\"valueexist\", 5).return(true)\n    testobj.on(\"directionexist\", 5).return(true)\n\n    exported := isgpiopinexported(testobj, 5)\n    assert.equal(t, false, exported)\n}<\/pre>\n<p>\u73b0\u5728\u53ef\u4ee5\u5f88\u7b80\u5355\u5730\u4f7f\u7528\u9002\u5f53\u7684\u6a21\u62df\u51fd\u6570\u6765\u6d4b\u8bd5\u51fd\u6570 <code>isgpiopinexported<\/code> \u4e2d\u7684\u903b\u8f91\u5e76\u83b7\u5f97\u6240\u9700\u7684\u7ed3\u679c\u3002\u6700\u540e\u4e3b\u7a0b\u5e8f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func main() {\n    rasberry3Plus := gpio.Rasberry3Plus{}\n    gpio.IsGpioPinExported(rasberry3Plus, 23)\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6a21\u62df\/\u4f2a\u9020\/\u66ff\u6362\u786c\u4ef6\u76f8\u5173\u529f\u80fd\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207606","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207606"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207606\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}