Skip to content

Commit 42b14f0

Browse files
committed
Bring back usage of start char
1 parent 8cb3ff9 commit 42b14f0

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Here lies the code powering my 433mhz RF transmitter. This transmitter supports
66

77
A packet is comprised of seven different bytes:
88

9-
1. `U` - This, and the following char, are our training bytes. The binary value of `U` is `01010101`, which gives any receivers' data slicers a nice square wave to sync up with. This makes them more receptive to the rest of our packet, which is important because that's where our actual data is.
10-
2. `U` - Another literal `U` training character.
9+
1. `U` - This is our training byte. The binary value of `U` is `01010101`, which gives any receivers' data slicers a nice square wave to sync up with. This makes them more receptive to the rest of our packet, which is important because that's where our actual data is.
10+
2. `ª` - This is the start byte, which signals to any receiver that the bytes that follow are data bytes. The binary value of 'ª' is 10101010, which should also hopefully help train the data slicers of our receivers to be more receptive to this sender.
1111
3. `button_byte` - Each bit in this byte corresponds to the status of our 8 non-special buttons that are on the top face of the transmitter. If a bit in this byte is `1`, it means the associated button is currently pressed.
1212
4. `misc_byte` - A byte containing a conglomerate of bits that didn't fit anywhere else. Here we have three bits corresponding to the pressed status of our left shoulder button, right shoulder button, and the button on the analog stick. We also have the two most significant bits of both the x-axis and y-axis analog stick values. The analog values of each axis are of 10-bit resolution, so rather than allocating two whole bytes for each one we instead put these MSBs here.
1313
5. `lsb_analog_stick_x_byte` - A byte containing the 8 least significant bits of the x-analog stick value.
1414
6. `lsb_analog_stick_y_byte` - A byte containing the 8 least significant bits of the y-analog stick values.
15-
7. `checksum` - Finally, we have our checksum byte. The checksum is calculated by adding up all our data bytes (so no training characters). Any overflow is ignored. This gives the receiver a simple (and admittedly not perfect) way to ensure that the data they've received is valid.
15+
7. `checksum` - Finally, we have our checksum byte. The checksum is calculated by adding up all our data bytes (so no training characters). Any overflow is ignored. This gives the receiver a simple (and admittedly not perfect) way to ensure that the data they've received is valid.

src/main.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <avr/interrupt.h>
1010
#include <avr/io.h>
1111

12-
enum Buffer_Status construct_and_store_packet(struct Ring_Buffer* buffer, const char* training_chars, const uint8_t num_training_chars, const char* data, const uint8_t num_data_chars, bool null_terminate);
12+
enum Buffer_Status construct_and_store_packet(struct Ring_Buffer* buffer, const char* training_chars, const uint8_t start_char, const uint8_t num_training_chars, const char* data, const uint8_t num_data_chars, bool null_terminate);
1313

1414
/* Since the ADC in AVRs output 10 bits, and the center of our joystick is represented by 524,
1515
these 8 bits on their own are equivalent to 12 in decimal. To save space versus transmitting
@@ -24,18 +24,21 @@ enum Buffer_Status construct_and_store_packet(struct Ring_Buffer* buffer, const
2424
#define DEFAULT_MISC_BYTE 0b01010000
2525

2626
/* Number of seconds of user inactivity before the AVR should go to sleep. */
27-
#define SECONDS_BEFORE_SLEEP (uint16_t) 300
27+
#define SECONDS_BEFORE_SLEEP (uint16_t) 900
2828

2929
/* Number of times Timer 2 needs to overflow before the AVR should go to sleep. */
3030
#define EIGHT_BIT_TIMER_MAX 255
3131
#define TIMER2_OVERFLOWS_BEFORE_SLEEP (uint32_t) (SECONDS_BEFORE_SLEEP / (float) ((EIGHT_BIT_TIMER_MAX * TIMER2_PRESCALER) / (float) F_CPU))
3232

3333
/* Use UU for our preamble, or training chars. I selected these characters because the binary value of
3434
the 'U' char is 01010101, which supposedly gives the receivers data slicer a nice square wave to sync up with */
35-
const char TRAINING_CHARS[] = "UU";
35+
const char TRAINING_CHARS[] = "U";
3636

3737
/* Number of training chars being used - must match the length of the above variable. */
38-
const uint8_t NUM_TRAINING_CHARS = 2;
38+
const uint8_t NUM_TRAINING_CHARS = 1;
39+
40+
/* This is the char we'll use to tell the receiver that any bytes that follow are actual data bytes. */
41+
const char START_CHAR = 0b10101010;
3942

4043
/* Number of data chars being sent in the packet. This should NOT include the checksum char.
4144
Currently, we have 'misc_byte', 'button_byte', 'lsb_analog_stick_x_byte', and 'lsb_analog_stick_y_byte' */
@@ -151,7 +154,7 @@ int main(void)
151154
packet_data[2] = lsb_analog_stick_x_byte;
152155
packet_data[3] = lsb_analog_stick_y_byte;
153156

154-
construct_and_store_packet(&packet_buffer, TRAINING_CHARS, NUM_TRAINING_CHARS, packet_data, NUM_DATA_CHARS, false);
157+
construct_and_store_packet(&packet_buffer, TRAINING_CHARS, START_CHAR, NUM_TRAINING_CHARS, packet_data, NUM_DATA_CHARS, false);
155158
should_construct_packet = false;
156159
}
157160

@@ -293,20 +296,23 @@ ISR(USART_TX_vect)
293296
294297
@param buffer - The buffer to fill as you construct the packet.
295298
@param training_chars - The chars used for training the receiver to sync up with this transmitter before we start sending actual data.
296-
@param num_training_chars - The number of training chars being passed in
299+
@param num_training_chars - The number of training chars being passed in.
300+
@param start_char - The char used to indicate the start of the data portion of the packet
297301
@param data - The chars representing the data portion of the packet.
298302
@param num_data_chars - The number of data chars being passed in.
299303
@param null_terminated - Whether or not to null terminate this packet.
300304
@return Buffer_Status - Returns the Buffer_Status returned by the most recent write, which allows the caller to handle buffer-related issues, such as an attempted write to a full buffer.
301305
*/
302-
enum Buffer_Status construct_and_store_packet(struct Ring_Buffer* buffer, const char* training_chars, const uint8_t num_training_chars, const char* data, const uint8_t num_data_chars, bool null_terminate)
306+
enum Buffer_Status construct_and_store_packet(struct Ring_Buffer* buffer, const char* training_chars, const uint8_t start_char, const uint8_t num_training_chars, const char* data, const uint8_t num_data_chars, bool null_terminate)
303307
{
304308
enum Buffer_Status status;
305309

306310
for(uint8_t i = 0; i < num_training_chars; i++) {
307311
status = ring_buffer_write(buffer, training_chars[i]);
308312
}
309313

314+
status = ring_buffer_write(buffer, start_char);
315+
310316
uint8_t checksum = 0;
311317
for(uint8_t j = 0; j < num_data_chars; j++) {
312318
status = ring_buffer_write(buffer, data[j]);

0 commit comments

Comments
 (0)