Skip to content

Commit

Permalink
Kebabify, and other cleanups. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Oct 4, 2024
1 parent b191f99 commit 036d3f4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "github-actions" # Necessary to update action hashs.
directory: "/"
schedule:
interval: "weekly"
# Allow up to 3 opened pull requests for github-actions versions.
open-pull-requests-limit: 3
20 changes: 18 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# Zero-Clause BSD License

# Copyright (C) 2023 Toitware ApS.

# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

name: Publish package
on:
push:
tags:
- 'v*'
- 'v[0-9]+.[0-9]+.[0-9]+'
- 'v[0-9]+.[0-9]+.[0-9]+-*'
jobs:
create-release:
name: Create new release
runs-on: ubuntu-latest
steps:
- name: Publish
uses: toitlang/pkg-publish@v1.0.2
uses: toitlang/pkg-publish@v1.5.0
2 changes: 0 additions & 2 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ main:
--sda=gpio.Pin 21
--scl=gpio.Pin 22
device := bus.device cat24c32.I2C_ADDRESS
device := bus.device cat24c32.I2C-ADDRESS
eeprom := cat24c32.Cat24c32 device
eeprom[0] = 0x63
Expand Down
2 changes: 1 addition & 1 deletion examples/main.toit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ main:
--sda=gpio.Pin 21
--scl=gpio.Pin 22

device := bus.device cat24c32.I2C_ADDRESS
device := bus.device cat24c32.I2C-ADDRESS
eeprom := cat24c32.Cat24c32 device

eeprom[0] = 0x63
Expand Down
42 changes: 21 additions & 21 deletions src/cat24c32.toit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import math
import i2c

/// The start I2C address is 0x50, but alts go up to 0x57.
I2C_ADDRESS ::= 0x50
I2C-ADDRESS ::= 0x50

/**
Driver for the CAT24C32 EEPROM.
Expand All @@ -21,12 +21,12 @@ class Cat24c32:
/** Size in bytes. */
static SIZE ::= 4096

static MAX_WRITE_OFFLINE_US_ ::= 5_000
static PAGE_SIZE_ ::= 32
static PAGE_MASK_ ::= ~0x1F
static MAX-WRITE-OFFLINE-US_ ::= 5_000
static PAGE-SIZE_ ::= 32
static PAGE-MASK_ ::= ~0x1F

device_ /i2c.Device ::= ?
last_write_us_ /int := -MAX_WRITE_OFFLINE_US_
last-write-us_ /int := -MAX-WRITE-OFFLINE-US_

constructor .device_:

Expand All @@ -38,11 +38,11 @@ class Cat24c32:
return (read at --size=1)[0]

/**
Writes a single byte $new_value at address $at.
Writes a single byte $new-value at address $at.
The address must satisfy 0 <= $at < $SIZE.
*/
operator[]= at/int new_value/int -> none:
write at #[new_value]
operator[]= at/int new-value/int -> none:
write at #[new-value]

/**
Reads $size bytes starting at $address.
Expand All @@ -52,7 +52,7 @@ class Cat24c32:
if size < 0: throw "INVALID_ARGUMENT"
if not 0 <= address << address + size <= SIZE: throw "OUT_OF_RANGE"
if size == 0: return #[]
return try_: return device_.read_address #[ address >> 8, address & 0xFF ] size
return try_: return device_.read-address #[ address >> 8, address & 0xFF ] size

/**
Writes the given $bytes starting at $address.
Expand All @@ -63,31 +63,31 @@ class Cat24c32:
if bytes.size == 0: return
// The device is split into pages of 32 bytes.
// Never write across page boundaries. The device would just wrap around.
if address & PAGE_MASK_ == (address + bytes.size - 1) & PAGE_MASK_:
write_page_ address bytes
if address & PAGE-MASK_ == (address + bytes.size - 1) & PAGE-MASK_:
write-page_ address bytes
return

// The address bits of the first page.
first_page_offset := address - (address & PAGE_MASK_)
first_page_bytes := PAGE_SIZE_ - first_page_offset
List.chunk_up 0 bytes.size first_page_bytes PAGE_SIZE_: | from to |
write_page_ (address + from) bytes[from..to]
first-page-offset := address - (address & PAGE-MASK_)
first-page-bytes := PAGE-SIZE_ - first-page-offset
List.chunk-up 0 bytes.size first-page-bytes PAGE-SIZE_: | from to |
write-page_ (address + from) bytes[from..to]

/**
Writes the given $bytes to the $address.
The $bytes must only write to one page.
*/
write_page_ address/int bytes/ByteArray -> none:
assert: address & PAGE_MASK_ == (address + bytes.size - 1) & PAGE_MASK_
write-page_ address/int bytes/ByteArray -> none:
assert: address & PAGE-MASK_ == (address + bytes.size - 1) & PAGE-MASK_
try_:
device_.write_address #[ address >> 8, address & 0xFF ] bytes
last_write_us_ = Time.monotonic_us
device_.write-address #[ address >> 8, address & 0xFF ] bytes
last-write-us_ = Time.monotonic-us

try_ [block]:
// After a write the device is offline for up to MAX_WRITE_OFFLINE_MS_.
// Try the operation, but be ok if it fails.
now := Time.monotonic_us
while last_write_us_ + MAX_WRITE_OFFLINE_US_ >= now:
now := Time.monotonic-us
while last-write-us_ + MAX-WRITE-OFFLINE-US_ >= now:
catch: return block.call
sleep --ms=1
// If the device should have written, just do the operation.
Expand Down

0 comments on commit 036d3f4

Please sign in to comment.