pub struct CatPrinterAsync {
pub transport: Box<dyn TransportAsync + Send + Sync>,
/* private fields */
}Expand description
Asynchronous CatPrinter API for printing text and images.
transport: implements TransportAsync trait (BLE)chunk_size: bytes per data chunk (default: 180)
Fields§
§transport: Box<dyn TransportAsync + Send + Sync>Implementations§
Source§impl CatPrinterAsync
impl CatPrinterAsync
pub fn new(transport: Box<dyn TransportAsync + Send + Sync>) -> Self
pub fn with_chunk_size(self, size: usize) -> Self
Sourcepub async fn get_status(
&self,
timeout: Duration,
) -> Result<PrinterStatus, String>
pub async fn get_status( &self, timeout: Duration, ) -> Result<PrinterStatus, String>
Examples found in repository?
examples/status_test.rs (line 59)
13async fn main() -> Result<(), Box<dyn std::error::Error>> {
14 println!("Scanning for CatPrinter-compatible BLE devices for 3 seconds...");
15 let devices = scan(Duration::from_secs(3)).await?;
16 if devices.is_empty() {
17 println!(
18 "No devices found. Make sure your Bluetooth adapter is up and the printer is powered on and advertising."
19 );
20 return Ok(());
21 }
22
23 println!("Found devices:");
24 for (i, d) in devices.iter().enumerate() {
25 println!(" {}) id={} name={:?}", i + 1, d.id, d.name);
26 }
27
28 // Ask user to pick a device
29 let mut input = String::new();
30 let chosen = loop {
31 print!("Select device number to connect to (1-{}): ", devices.len());
32 io::stdout().flush()?;
33 input.clear();
34 io::stdin().read_line(&mut input)?;
35 if let Ok(n) = input.trim().parse::<usize>() {
36 if n >= 1 && n <= devices.len() {
37 break &devices[n - 1];
38 }
39 }
40 println!("Invalid selection.");
41 };
42
43 println!("Connecting to device id={} name={:?} ...", chosen.id, chosen.name);
44 let printer = match connect(&chosen.id, Duration::from_secs(10)).await {
45 Ok(p) => {
46 println!("Connected successfully.");
47 p
48 }
49 Err(e) => {
50 eprintln!("Failed to connect: {}", e);
51 return Ok(());
52 }
53 };
54
55 // Query status and battery 10 times, 1s apart
56 println!("Starting status/battery query loop (10 iterations, 1s apart)...");
57 for i in 0..10 {
58 println!("Query {}:", i + 1);
59 match printer.get_status(Duration::from_secs(10)).await {
60 Ok(s) => {
61 println!(
62 "Status (0xA1) -> battery: {:?}, temperature: {:?}, state: {:?}",
63 s.battery_percent, s.temperature, s.state
64 );
65 }
66 Err(e) => {
67 eprintln!("Failed to get status: {}", e);
68 }
69 }
70 match printer.get_battery(Duration::from_secs(10)).await {
71 Ok(b) => {
72 println!("Battery (0xAB) -> battery percent: {}", b);
73 }
74 Err(e) => {
75 eprintln!("Failed to get battery: {}", e);
76 }
77 }
78 tokio::time::sleep(Duration::from_secs(1)).await;
79 }
80 println!("Status/battery query loop finished.");
81 Ok(())
82}More examples
examples/test.rs (line 79)
74async fn run_interactive_session(
75 printer: catprinter::ble::CatPrinterAsync,
76) -> Result<(), Box<dyn std::error::Error>> {
77 // Show status
78 println!("Querying printer status...");
79 match printer.get_status(Duration::from_secs(10)).await {
80 Ok(s) => {
81 println!(
82 "Status -> battery: {:?}, temperature: {:?}, state: {:?}",
83 s.battery_percent, s.temperature, s.state
84 );
85 }
86 Err(e) => {
87 eprintln!("Failed to get status: {}", e);
88 }
89 }
90 let mut mode = String::new();
91 // Prompt user for print mode
92 print!("Choose print mode: 1 for text, 2 for image: ");
93 io::stdout().flush()?;
94 io::stdin().read_line(&mut mode)?;
95
96 if mode.trim() == "1" {
97 // Prompt user for text and author
98 let mut main = String::new();
99 let mut author = String::new();
100 // Prompt for text and author
101 print!("Enter the main text to print: ");
102 io::stdout().flush()?;
103 io::stdin().read_line(&mut main)?;
104 print!("Enter the author name: ");
105 io::stdout().flush()?;
106 io::stdin().read_line(&mut author)?;
107 let main = main.trim();
108 let author = author.trim();
109
110 if main.is_empty() {
111 println!("No text entered, aborting.");
112 return Ok(());
113 }
114
115 // Print text
116 println!("Sending print job (text)...");
117 match printer.print_text(main, author).await {
118 Ok(()) => println!("Print job completed (printer reported AA/complete)."),
119 Err(e) => eprintln!("Print job failed: {}", e),
120 }
121 } else if mode.trim() == "2" {
122 let mut img_path = String::new();
123 // Prompt for image path
124 print!("Enter the path to the image: ");
125 io::stdout().flush()?;
126 io::stdin().read_line(&mut img_path)?;
127 let img_path = img_path.trim();
128
129 if img_path.is_empty() {
130 println!("No image path entered, aborting.");
131 return Ok(());
132 }
133
134 // Prompt for dithering mode
135 let mut dithering_mode = String::new();
136 print!("Choose dithering mode: 1=Threshold, 2=Floyd-Steinberg, 3=Atkinson, 4=Halftone: ");
137 io::stdout().flush()?;
138 io::stdin().read_line(&mut dithering_mode)?;
139 let dithering = match dithering_mode.trim() {
140 "2" => catprinter::dithering::ImageDithering::FloydSteinberg,
141 "3" => catprinter::dithering::ImageDithering::Atkinson,
142 "4" => catprinter::dithering::ImageDithering::Halftone,
143 _ => catprinter::dithering::ImageDithering::Threshold,
144 };
145
146 // Print image
147 println!("Sending print job (image)...");
148 match printer.print_image_from_path(img_path, dithering).await {
149 Ok(()) => println!("Print job completed (printer reported AA/complete)."),
150 Err(e) => eprintln!("Print job failed: {}", e),
151 }
152 } else {
153 println!("Invalid selection.");
154 }
155
156 Ok(())
157}Sourcepub async fn get_battery(&self, timeout: Duration) -> Result<u8, String>
pub async fn get_battery(&self, timeout: Duration) -> Result<u8, String>
Examples found in repository?
examples/status_test.rs (line 70)
13async fn main() -> Result<(), Box<dyn std::error::Error>> {
14 println!("Scanning for CatPrinter-compatible BLE devices for 3 seconds...");
15 let devices = scan(Duration::from_secs(3)).await?;
16 if devices.is_empty() {
17 println!(
18 "No devices found. Make sure your Bluetooth adapter is up and the printer is powered on and advertising."
19 );
20 return Ok(());
21 }
22
23 println!("Found devices:");
24 for (i, d) in devices.iter().enumerate() {
25 println!(" {}) id={} name={:?}", i + 1, d.id, d.name);
26 }
27
28 // Ask user to pick a device
29 let mut input = String::new();
30 let chosen = loop {
31 print!("Select device number to connect to (1-{}): ", devices.len());
32 io::stdout().flush()?;
33 input.clear();
34 io::stdin().read_line(&mut input)?;
35 if let Ok(n) = input.trim().parse::<usize>() {
36 if n >= 1 && n <= devices.len() {
37 break &devices[n - 1];
38 }
39 }
40 println!("Invalid selection.");
41 };
42
43 println!("Connecting to device id={} name={:?} ...", chosen.id, chosen.name);
44 let printer = match connect(&chosen.id, Duration::from_secs(10)).await {
45 Ok(p) => {
46 println!("Connected successfully.");
47 p
48 }
49 Err(e) => {
50 eprintln!("Failed to connect: {}", e);
51 return Ok(());
52 }
53 };
54
55 // Query status and battery 10 times, 1s apart
56 println!("Starting status/battery query loop (10 iterations, 1s apart)...");
57 for i in 0..10 {
58 println!("Query {}:", i + 1);
59 match printer.get_status(Duration::from_secs(10)).await {
60 Ok(s) => {
61 println!(
62 "Status (0xA1) -> battery: {:?}, temperature: {:?}, state: {:?}",
63 s.battery_percent, s.temperature, s.state
64 );
65 }
66 Err(e) => {
67 eprintln!("Failed to get status: {}", e);
68 }
69 }
70 match printer.get_battery(Duration::from_secs(10)).await {
71 Ok(b) => {
72 println!("Battery (0xAB) -> battery percent: {}", b);
73 }
74 Err(e) => {
75 eprintln!("Failed to get battery: {}", e);
76 }
77 }
78 tokio::time::sleep(Duration::from_secs(1)).await;
79 }
80 println!("Status/battery query loop finished.");
81 Ok(())
82}Sourcepub async fn print_text(&self, main: &str, author: &str) -> Result<(), String>
pub async fn print_text(&self, main: &str, author: &str) -> Result<(), String>
Examples found in repository?
examples/test.rs (line 117)
74async fn run_interactive_session(
75 printer: catprinter::ble::CatPrinterAsync,
76) -> Result<(), Box<dyn std::error::Error>> {
77 // Show status
78 println!("Querying printer status...");
79 match printer.get_status(Duration::from_secs(10)).await {
80 Ok(s) => {
81 println!(
82 "Status -> battery: {:?}, temperature: {:?}, state: {:?}",
83 s.battery_percent, s.temperature, s.state
84 );
85 }
86 Err(e) => {
87 eprintln!("Failed to get status: {}", e);
88 }
89 }
90 let mut mode = String::new();
91 // Prompt user for print mode
92 print!("Choose print mode: 1 for text, 2 for image: ");
93 io::stdout().flush()?;
94 io::stdin().read_line(&mut mode)?;
95
96 if mode.trim() == "1" {
97 // Prompt user for text and author
98 let mut main = String::new();
99 let mut author = String::new();
100 // Prompt for text and author
101 print!("Enter the main text to print: ");
102 io::stdout().flush()?;
103 io::stdin().read_line(&mut main)?;
104 print!("Enter the author name: ");
105 io::stdout().flush()?;
106 io::stdin().read_line(&mut author)?;
107 let main = main.trim();
108 let author = author.trim();
109
110 if main.is_empty() {
111 println!("No text entered, aborting.");
112 return Ok(());
113 }
114
115 // Print text
116 println!("Sending print job (text)...");
117 match printer.print_text(main, author).await {
118 Ok(()) => println!("Print job completed (printer reported AA/complete)."),
119 Err(e) => eprintln!("Print job failed: {}", e),
120 }
121 } else if mode.trim() == "2" {
122 let mut img_path = String::new();
123 // Prompt for image path
124 print!("Enter the path to the image: ");
125 io::stdout().flush()?;
126 io::stdin().read_line(&mut img_path)?;
127 let img_path = img_path.trim();
128
129 if img_path.is_empty() {
130 println!("No image path entered, aborting.");
131 return Ok(());
132 }
133
134 // Prompt for dithering mode
135 let mut dithering_mode = String::new();
136 print!("Choose dithering mode: 1=Threshold, 2=Floyd-Steinberg, 3=Atkinson, 4=Halftone: ");
137 io::stdout().flush()?;
138 io::stdin().read_line(&mut dithering_mode)?;
139 let dithering = match dithering_mode.trim() {
140 "2" => catprinter::dithering::ImageDithering::FloydSteinberg,
141 "3" => catprinter::dithering::ImageDithering::Atkinson,
142 "4" => catprinter::dithering::ImageDithering::Halftone,
143 _ => catprinter::dithering::ImageDithering::Threshold,
144 };
145
146 // Print image
147 println!("Sending print job (image)...");
148 match printer.print_image_from_path(img_path, dithering).await {
149 Ok(()) => println!("Print job completed (printer reported AA/complete)."),
150 Err(e) => eprintln!("Print job failed: {}", e),
151 }
152 } else {
153 println!("Invalid selection.");
154 }
155
156 Ok(())
157}Sourcepub async fn print_image_from_path(
&self,
path: &str,
dithering: ImageDithering,
) -> Result<(), String>
pub async fn print_image_from_path( &self, path: &str, dithering: ImageDithering, ) -> Result<(), String>
Examples found in repository?
examples/test.rs (line 148)
74async fn run_interactive_session(
75 printer: catprinter::ble::CatPrinterAsync,
76) -> Result<(), Box<dyn std::error::Error>> {
77 // Show status
78 println!("Querying printer status...");
79 match printer.get_status(Duration::from_secs(10)).await {
80 Ok(s) => {
81 println!(
82 "Status -> battery: {:?}, temperature: {:?}, state: {:?}",
83 s.battery_percent, s.temperature, s.state
84 );
85 }
86 Err(e) => {
87 eprintln!("Failed to get status: {}", e);
88 }
89 }
90 let mut mode = String::new();
91 // Prompt user for print mode
92 print!("Choose print mode: 1 for text, 2 for image: ");
93 io::stdout().flush()?;
94 io::stdin().read_line(&mut mode)?;
95
96 if mode.trim() == "1" {
97 // Prompt user for text and author
98 let mut main = String::new();
99 let mut author = String::new();
100 // Prompt for text and author
101 print!("Enter the main text to print: ");
102 io::stdout().flush()?;
103 io::stdin().read_line(&mut main)?;
104 print!("Enter the author name: ");
105 io::stdout().flush()?;
106 io::stdin().read_line(&mut author)?;
107 let main = main.trim();
108 let author = author.trim();
109
110 if main.is_empty() {
111 println!("No text entered, aborting.");
112 return Ok(());
113 }
114
115 // Print text
116 println!("Sending print job (text)...");
117 match printer.print_text(main, author).await {
118 Ok(()) => println!("Print job completed (printer reported AA/complete)."),
119 Err(e) => eprintln!("Print job failed: {}", e),
120 }
121 } else if mode.trim() == "2" {
122 let mut img_path = String::new();
123 // Prompt for image path
124 print!("Enter the path to the image: ");
125 io::stdout().flush()?;
126 io::stdin().read_line(&mut img_path)?;
127 let img_path = img_path.trim();
128
129 if img_path.is_empty() {
130 println!("No image path entered, aborting.");
131 return Ok(());
132 }
133
134 // Prompt for dithering mode
135 let mut dithering_mode = String::new();
136 print!("Choose dithering mode: 1=Threshold, 2=Floyd-Steinberg, 3=Atkinson, 4=Halftone: ");
137 io::stdout().flush()?;
138 io::stdin().read_line(&mut dithering_mode)?;
139 let dithering = match dithering_mode.trim() {
140 "2" => catprinter::dithering::ImageDithering::FloydSteinberg,
141 "3" => catprinter::dithering::ImageDithering::Atkinson,
142 "4" => catprinter::dithering::ImageDithering::Halftone,
143 _ => catprinter::dithering::ImageDithering::Threshold,
144 };
145
146 // Print image
147 println!("Sending print job (image)...");
148 match printer.print_image_from_path(img_path, dithering).await {
149 Ok(()) => println!("Print job completed (printer reported AA/complete)."),
150 Err(e) => eprintln!("Print job failed: {}", e),
151 }
152 } else {
153 println!("Invalid selection.");
154 }
155
156 Ok(())
157}pub async fn print_image( &self, pixels: &[u8], width: usize, height: usize, mode: u8, chunk_size: Option<usize>, ) -> Result<(), String>
Auto Trait Implementations§
impl Freeze for CatPrinterAsync
impl !RefUnwindSafe for CatPrinterAsync
impl Send for CatPrinterAsync
impl Sync for CatPrinterAsync
impl Unpin for CatPrinterAsync
impl !UnwindSafe for CatPrinterAsync
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.