Last active
July 11, 2023 00:35
-
-
Save brandsimon/0cf7166eefb2282bbeaa405f8cac8f8c to your computer and use it in GitHub Desktop.
Digispark Attiny85 example sketches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DigiCDC.h> | |
void setup() { | |
SerialUSB.begin(); | |
} | |
void loop() { | |
if (SerialUSB.available()) { | |
SerialUSB.write(SerialUSB.read()); | |
} | |
SerialUSB.refresh(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DigiCDC.h> | |
// #define BRICK | |
#define LED_PIN 1 // 0: Model B, 1: Model A | |
#define BRICK_AT 4 | |
#define BUFFER_SIZE 512 // 128 works global, 8192 works local | |
size_t CHAR_COUNT = 0; | |
#ifdef BRICK | |
char BUFFER[BUFFER_SIZE]; | |
#endif | |
void setup() { | |
SerialUSB.begin(); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, LOW); | |
} | |
void loop() { | |
#ifndef BRICK | |
char BUFFER[BUFFER_SIZE]; | |
#endif | |
if (SerialUSB.available()) { | |
int c = SerialUSB.read(); | |
SerialUSB.write(c); | |
CHAR_COUNT += 1; | |
if (CHAR_COUNT > BRICK_AT) { | |
digitalWrite(LED_PIN, HIGH); | |
SerialUSB.println("Brick"); | |
SerialUSB.delay(2000); | |
SerialUSB.println("Wait done"); | |
for (size_t t = 0; t < BUFFER_SIZE; t++) { | |
BUFFER[t] = c; | |
} | |
digitalWrite(LED_PIN, LOW); | |
} | |
} | |
SerialUSB.refresh(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DigiCDC.h> | |
void setup() { | |
OSCCAL = 154; | |
SerialUSB.begin(); | |
} | |
void loop() { | |
if (SerialUSB.available()) { | |
int x = SerialUSB.read(); | |
if (x == '0') { | |
SerialUSB.println("OSCCAL: "); | |
SerialUSB.println(OSCCAL + 0); | |
} else if (x == '+') { | |
OSCCAL += 1; | |
} else if (x == '-') { | |
OSCCAL -= 1; | |
} else { | |
SerialUSB.write(x); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ arduino-cli monitor -p /dev/ttyACM0 | |
Connected to /dev/ttyACM0! Press CTRL-C to exit. | |
0 | |
OSCCAL: | |
157 | |
123456789abcdefghijklmnopqrstuvwxyz987654321g | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
++ | |
0 | |
OSCCAL: | |
159 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
++ | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
12345q9abcdgghihijklopqrstuvoxyz191919 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
1234512345.q9abcdgghijklmhijklmhijh111911 | |
-- | |
-- | |
-- | |
0 | |
OSCCAL: | |
155 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
- | |
0 | |
OSCCAL: | |
154 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
12345123456789abcdefghijklmnopqrstuvpx4 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
123456789abcdefghijklmnopqrstuvwxyz94 | |
0 | |
OSCCAL: | |
154 | |
- | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
12345 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 | |
0 | |
OSCCAL: | |
153 | |
123456789abcdefghijklmnopqrstuvwxyz987654321 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DigiCDC.h> | |
#define LED_PIN 1 // 0: Model B, 1: Model A | |
#define BUFFER_SIZE 128 | |
struct readline_buffer { | |
char str[BUFFER_SIZE]; | |
size_t _position; | |
}; | |
struct readline_buffer buffer; | |
void readline_buffer_init(struct readline_buffer *buf) { | |
buf->str[0] = '\0'; | |
buf->_position = 0; | |
} | |
size_t serial_readline(struct readline_buffer *buf) { | |
while (true) { | |
if (!SerialUSB.available()) { | |
SerialUSB.println(""); | |
return 0; | |
} | |
bool full = (buf->_position == BUFFER_SIZE - 2); | |
if (full) { | |
break; | |
} | |
int recv = SerialUSB.read(); | |
if (recv < 0 || recv >= 127) { | |
continue; | |
} | |
if (recv == '\n' || recv == '\0') { | |
break; | |
} else { | |
buf->str[buf->_position] = (char)recv; | |
buf->_position += 1; | |
continue; | |
} | |
} | |
buf->str[buf->_position] = '\0'; | |
return buf->_position; | |
} | |
void setup() { | |
readline_buffer_init(&buffer); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, LOW); | |
SerialUSB.begin(); | |
} | |
void loop() { | |
if (!SerialUSB.available()) { | |
return; | |
} | |
size_t result = serial_readline(&buffer); | |
if (result != 0) { | |
SerialUSB.print("Recv: "); | |
SerialUSB.println(buffer.str); | |
if (0 == strcmp(buffer.str, "activate orange led")) { | |
digitalWrite(LED_PIN, HIGH); | |
} | |
if (0 == strcmp(buffer.str, "deactivate orange led")) { | |
digitalWrite(LED_PIN, LOW); | |
} | |
readline_buffer_init(&buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment