3 Star 0 Fork 3

上海炘璞电子科技有限公司/detonator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
channel.selector.go 726 Bytes
一键复制 编辑 原始数据 按行查看 历史
package coder
import "sync"
const (
ChannelSize = 4 * 8
)
// Channel 通道选择
type Channel struct {
channels []bool
sync.Once
}
// Enable 选择指定通道, 通道号从1开始,最大32(4字节) 只可选择不可取消. 如需取消,需要重新创建Channel对象
func (ch *Channel) Enable(n int) {
if n <= 0 || n > ChannelSize {
return
}
ch.Do(func() {
ch.channels = make([]bool, ChannelSize)
})
ch.channels[n-1] = true
}
func (ch *Channel) build() (r []byte) {
r = make([]byte, ChannelSize/8)
if len(ch.channels) == 0 { // 没有打开的通道
return
}
for i := range r {
for j := 0; j < (i+1)*8; j++ {
r[i] <<= 1
if ch.channels[ChannelSize-1-j] {
r[i] ^= 1
}
}
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/thingple/detonator.git
git@gitee.com:thingple/detonator.git
thingple
detonator
detonator
v0.0.19

搜索帮助